View Javadoc
1   /**
2    * This file Copyright (c) 2003-2015 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.ACL;
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.ArrayList;
42  import java.util.Collection;
43  import java.util.Map;
44  
45  import javax.jcr.Value;
46  import javax.security.auth.Subject;
47  
48  /**
49   * Manages the JAAS users.
50   */
51  public class ExternalUserManager implements UserManager {
52  
53      @Override
54      public User getUser(String name) throws UnsupportedOperationException {
55          // we only support accessing current User object
56          // - implement source specific UserManager if needed
57          if (name.equalsIgnoreCase(MgnlContext.getUser().getName())) {
58              return MgnlContext.getUser();
59          }
60          throw new UnsupportedOperationException("not implemented yet");
61      }
62  
63      @Override
64      public User getUserById(final String id) throws UnsupportedOperationException {
65          throw new UnsupportedOperationException("not implemented yet");
66      }
67  
68      /**
69       * Initialize new user using JAAS authenticated/authorized subject.
70       */
71      @Override
72      public User getUser(Subject subject) throws UnsupportedOperationException {
73          return new ExternalUser(subject);
74      }
75  
76      public User getUser(Map<String, String> properties, GroupList groupList, RoleList roleList) {
77          return new ExternalUser(properties, groupList, roleList);
78      }
79  
80      @Override
81      public Collection<User> getAllUsers() throws UnsupportedOperationException {
82          throw new UnsupportedOperationException("not implemented yet");
83      }
84  
85      @Override
86      public User createUser(String name, String pw) throws UnsupportedOperationException {
87          throw new UnsupportedOperationException("not implemented yet");
88      }
89  
90      @Override
91      public User createUser(String path, String name, String pw) throws UnsupportedOperationException {
92          throw new UnsupportedOperationException("not implemented yet");
93      }
94  
95      @Override
96      public User changePassword(User user, String newPassword) throws UnsupportedOperationException {
97          throw new UnsupportedOperationException("not implemented yet");
98      }
99  
100     /**
101      * SystemUserManager does this.
102      */
103     @Override
104     public User getSystemUser() throws UnsupportedOperationException {
105         throw new UnsupportedOperationException();
106     }
107 
108     /**
109      * SystemUserManager does this.
110      */
111     @Override
112     public User getAnonymousUser() throws UnsupportedOperationException {
113         throw new UnsupportedOperationException();
114     }
115 
116     @Override
117     public void updateLastAccessTimestamp(User user) throws UnsupportedOperationException {
118         throw new UnsupportedOperationException();
119     }
120 
121     @Override
122     public boolean hasAny(String name, String roleName, String nodeRoles) throws UnsupportedOperationException {
123         throw new UnsupportedOperationException("not implemented yet");
124     }
125 
126     @Override
127     public Map<String, ACL> getACLs(User user) {
128         throw new UnsupportedOperationException("not implemented yet");
129     }
130 
131     @Override
132     public User addRole(User user, String roleName) {
133         throw new UnsupportedOperationException("not implemented yet");
134     }
135 
136     @Override
137     public User addGroup(User user, String groupName) {
138         throw new UnsupportedOperationException("not implemented yet");
139     }
140 
141     @Override
142     public int getLockTimePeriod() {
143         throw new UnsupportedOperationException("Not supported by this user manager.");
144     }
145 
146     @Override
147     public int getMaxFailedLoginAttempts() {
148         throw new UnsupportedOperationException("Not supported by this user manager.");
149     }
150 
151     @Override
152     public void setLockTimePeriod(int lockTimePeriod) {
153         throw new UnsupportedOperationException("Not supported by this user manager.");
154     }
155 
156     @Override
157     public void setMaxFailedLoginAttempts(int maxFailedLoginAttempts) {
158         throw new UnsupportedOperationException("Not supported by this user manager.");
159     }
160 
161     @Override
162     public User setProperty(User user, String propertyName, Value propertyValue) {
163         throw new UnsupportedOperationException("Not supported by this user manager.");
164     }
165 
166     @Override
167     public User setProperty(User user, String propertyName, String propertyValue) {
168         throw new UnsupportedOperationException("Not supported by this user manager.");
169     }
170 
171     @Override
172     public User removeGroup(User user, String groupName) {
173         throw new UnsupportedOperationException("not implemented yet");
174     }
175 
176     @Override
177     public User removeRole(User user, String roleName) {
178         throw new UnsupportedOperationException("not implemented yet");
179     }
180 
181     @Override
182     public Collection<String> getUsersWithGroup(String groupName, boolean transitive) {
183         throw new UnsupportedOperationException("Not supported by this user manager.");
184     }
185 
186     /**
187      * Default implementation for external userManagers. Please overwrite in case it can be implemented in a more efficient way.
188      */
189     @Override
190     public Collection<String> getUsersWithGroup(final String groupName) {
191         Collection<User> users = getAllUsers();
192         Collection<String> usersWithProvidedGroup = new ArrayList<String>();
193         for (User current: users) {
194             if (current.inGroup(groupName)) {
195                 usersWithProvidedGroup.add(current.getName());
196             }
197         }
198 
199         return usersWithProvidedGroup;
200     }
201 
202     /**
203      * Default implementation for external userManagers. Please overwrite in case it can be implemented in a more efficient way.
204      */
205     @Override
206     public Collection<String> getUsersWithRole(final String roleName) {
207         Collection<User> users = getAllUsers();
208         Collection<String> usersWithProvidedGroup = new ArrayList<String>();
209         for (User current: users) {
210             if (current.hasRole(roleName)) {
211                 usersWithProvidedGroup.add(current.getName());
212             }
213         }
214 
215         return usersWithProvidedGroup;
216     }
217 }