View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.security;
35  
36  import info.magnolia.cms.security.auth.Entity;
37  import info.magnolia.cms.security.auth.GroupList;
38  import info.magnolia.cms.security.auth.RoleList;
39  import info.magnolia.context.MgnlContext;
40  
41  import java.util.Collection;
42  import java.util.Iterator;
43  import java.util.Set;
44  import java.io.Serializable;
45  
46  import javax.security.auth.Subject;
47  
48  import org.apache.commons.lang.ObjectUtils;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  
53  /**
54   * A user which is not stored in Magnolia. For instance a user stored in LDAP or AD.
55   * @version $Revision:2558 $ ($Author:scharles $)
56   */
57  public class ExternalUser extends AbstractUser implements Serializable {
58      private static final Logger log = LoggerFactory.getLogger(ExternalUser.class);
59  
60      /**
61       * User properties.
62       */
63      private Entity userDetails;
64  
65      /**
66       * User roles.
67       */
68      private RoleList roleList;
69  
70      /**
71       * User groups.
72       */
73      private GroupList groupList;
74  
75      /**
76       * @param subject as created by login module
77       */
78      protected ExternalUser(Subject subject) {
79          final Set<Entity> principalDetails = subject.getPrincipals(Entity.class);
80          final Iterator<Entity> entityIterator = principalDetails.iterator();
81          this.userDetails = entityIterator.next();
82  
83          final Set<RoleList> principalRoles = subject.getPrincipals(RoleList.class);
84          final Iterator<RoleList> roleListIterator = principalRoles.iterator();
85          this.roleList = roleListIterator.next();
86  
87          final Set<GroupList> principalGroups = subject.getPrincipals(GroupList.class);
88          final Iterator<GroupList> groupListIterator = principalGroups.iterator();
89          this.groupList = groupListIterator.next();
90      }
91  
92      @Override
93      public boolean hasRole(String roleName) {
94          return this.roleList.has(roleName);
95      }
96  
97      @Override
98      public void removeRole(String roleName) {
99          throw new UnsupportedOperationException("not implemented for this ExternalUser");
100     }
101 
102     @Override
103     public void addRole(String roleName) {
104         throw new UnsupportedOperationException("not implemented for this ExternalUser");
105     }
106 
107     /**
108      * Is this user in a specified group?
109      * @return true if in group
110      */
111     @Override
112     public boolean inGroup(String groupName) {
113         return this.groupList.has(groupName);
114     }
115 
116     /**
117      * Remove a group. Implementation is optional
118      */
119     @Override
120     public void removeGroup(String groupName) throws UnsupportedOperationException {
121         throw new UnsupportedOperationException("not implemented for this ExternalUser");
122     }
123 
124     /**
125      * Adds this user to a group. Implementation is optional
126      */
127     @Override
128     public void addGroup(String groupName) throws UnsupportedOperationException {
129         throw new UnsupportedOperationException("not implemented for this ExternalUser");
130     }
131 
132     @Override
133     public boolean isEnabled() {
134         return true;
135     }
136 
137     @Override
138     public void setEnabled(boolean enabled) {
139     }
140 
141     @Override
142     public String getLanguage() {
143         String language = (String) this.userDetails.getProperty(Entity.LANGUAGE);
144         if (null == language) {
145             language = MgnlContext.getSystemContext().getLocale().getLanguage();
146         }
147         return language;
148     }
149 
150     @Override
151     public String getName() {
152         return (String) this.userDetails.getProperty(Entity.NAME);
153     }
154 
155     @Override
156     public String getPassword() {
157         return (String) this.userDetails.getProperty(Entity.PASSWORD);
158     }
159 
160     @Override
161     public String getProperty(String propertyName) {
162         return ObjectUtils.toString(this.userDetails.getProperty(propertyName), null);
163     }
164 
165     @Override
166     public void setProperty(String propertyName, String value) {
167         // can't set properties in userDetails
168         throw new UnsupportedOperationException("not implemented for this ExternalUser");
169     }
170 
171     @Override
172     public Collection<String> getGroups() {
173         return this.groupList.getList();
174     }
175 
176     @Override
177     public Collection<String> getAllGroups() {
178         return this.getGroups();
179     }
180 
181     @Override
182     public Collection<String> getRoles() {
183         return this.roleList.getList();
184     }
185 
186     @Override
187     public Collection<String> getAllRoles() {
188         return this.getRoles();
189     }
190 }