View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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.io.Serializable;
42  import java.util.Collection;
43  import java.util.Iterator;
44  import java.util.Map;
45  import java.util.Set;
46  
47  import javax.security.auth.Subject;
48  
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 properties.
67       */
68      private Map<String, String> properties;
69  
70      /**
71       * User roles.
72       */
73      private RoleList roleList;
74  
75      /**
76       * User groups.
77       */
78      private GroupList groupList;
79  
80      /**
81       * @param subject as created by login module
82       * @deprecated since 4.5 use ExternalUser(java.util.Map, info.magnolia.cms.security.auth.GroupList,
83       *              info.magnolia.cms.security.auth.RoleList) instead
84       */
85      @Deprecated
86      protected ExternalUser(Subject subject) {
87          final Set<Entity> principalDetails = subject.getPrincipals(Entity.class);
88          final Iterator<Entity> entityIterator = principalDetails.iterator();
89          this.userDetails = entityIterator.next();
90  
91          final Set<RoleList> principalRoles = subject.getPrincipals(RoleList.class);
92          final Iterator<RoleList> roleListIterator = principalRoles.iterator();
93          this.roleList = roleListIterator.next();
94  
95          final Set<GroupList> principalGroups = subject.getPrincipals(GroupList.class);
96          final Iterator<GroupList> groupListIterator = principalGroups.iterator();
97          this.groupList = groupListIterator.next();
98      }
99  
100     protected ExternalUser(Map<String, String> properties, GroupList groupList, RoleList roleList) {
101         this.properties = properties;
102         this.groupList = groupList;
103         this.roleList = roleList;
104     }
105 
106     @Override
107     public boolean hasRole(String roleName) {
108         return this.roleList.has(roleName);
109     }
110 
111     @Override
112     public void removeRole(String roleName) {
113         throw new UnsupportedOperationException("not implemented for this ExternalUser");
114     }
115 
116     @Override
117     public void addRole(String roleName) {
118         throw new UnsupportedOperationException("not implemented for this ExternalUser");
119     }
120 
121     /**
122      * Is this user in a specified group?
123      * @return true if in group
124      */
125     @Override
126     public boolean inGroup(String groupName) {
127         return this.groupList.has(groupName);
128     }
129 
130     /**
131      * Remove a group. Implementation is optional
132      */
133     @Override
134     public void removeGroup(String groupName) throws UnsupportedOperationException {
135         throw new UnsupportedOperationException("not implemented for this ExternalUser");
136     }
137 
138     /**
139      * Adds this user to a group. Implementation is optional
140      */
141     @Override
142     public void addGroup(String groupName) throws UnsupportedOperationException {
143         throw new UnsupportedOperationException("not implemented for this ExternalUser");
144     }
145 
146     @Override
147     public boolean isEnabled() {
148         return true;
149     }
150 
151     @Override
152     public void setEnabled(boolean enabled) {
153     }
154 
155     @Override
156     public String getLanguage() {
157         String language = this.properties.get(Entity.LANGUAGE);
158         if (null == language) {
159             language = MgnlContext.getSystemContext().getLocale().getLanguage();
160         }
161         return language;
162     }
163 
164     @Override
165     public String getName() {
166         return this.properties.get(Entity.NAME);
167     }
168 
169     @Override
170     public String getPassword() {
171         return this.properties.get(Entity.PASSWORD);
172     }
173 
174     public String getEmail() {
175         return this.properties.get(Entity.EMAIL);
176     }
177 
178     @Override
179     public String getProperty(String propertyName) {
180         String property = this.properties.get(propertyName);
181         if(null == property){
182             log.debug("Unable to retrieve property " + propertyName + " for user " + getName());
183         }
184         return property;
185     }
186 
187     @Override
188     public void setProperty(String propertyName, String value) {
189         this.properties.put(propertyName, value);
190     }
191 
192     @Override
193     public String getIdentifier() {
194         throw new UnsupportedOperationException("not implemented for this ExternalUser");
195     }
196 
197     @Override
198     public Collection<String> getGroups() {
199         return this.groupList.getList();
200     }
201 
202     @Override
203     public Collection<String> getAllGroups() {
204         return this.getGroups();
205     }
206 
207     @Override
208     public Collection<String> getRoles() {
209         return this.roleList.getList();
210     }
211 
212     @Override
213     public Collection<String> getAllRoles() {
214         return this.getRoles();
215     }
216 }