bbec005525182f1e277a17c035ad6a68c7a2c058
[utils] / security / impl / src / main / java / org / wamblee / security / authentication / UserAdministrationImpl.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.security.authentication;
17
18 import static org.wamblee.security.authentication.UserMgtException.Reason.*;
19
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.wamblee.security.authentication.UserMgtException.Reason;
26
27 /**
28  * Administration of users and groups.
29  * 
30  * @author Erik Brakkee
31  */
32 public class UserAdministrationImpl implements UserAdministration {
33     /**
34      * All known users.
35      */
36     private UserSet users;
37
38     /**
39      * All known groups.
40      */
41     private GroupSet groups;
42
43     /**
44      * Validator for user names.
45      */
46     private NameValidator userValidator;
47
48     /**
49      * Validator for group names.
50      */
51     private NameValidator groupValidator;
52
53     /**
54      * Constructs empty user administration.
55      * 
56      */
57     public UserAdministrationImpl(UserSet aUsers, GroupSet aGroups,
58         NameValidator aUserValidator, NameValidator aGroupValidator) {
59         users = aUsers;
60         groups = aGroups;
61         userValidator = aUserValidator;
62         groupValidator = aGroupValidator;
63     }
64
65     /*
66      * (non-Javadoc)
67      * 
68      * @see org.wamblee.usermgt.UserAdministration#createUser(java.lang.String,
69      * java.lang.String)
70      */
71     @Override
72     public void createUser(String aUser, String aPassword) {
73         if (!userValidator.validate(aUser)) {
74             throw new UserMgtException(Reason.INVALID_USERNAME, aUser);
75         }
76
77         users.createUser(aUser, aPassword);
78     }
79
80     /*
81      * (non-Javadoc)
82      * 
83      * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
84      */
85     @Override
86     public void createGroup(String aName) {
87         if (!groupValidator.validate(aName)) {
88             throw new UserMgtException(Reason.INVALID_GROUPNAME, aName);
89         }
90
91         Group group = new Group(aName);
92
93         if (groups.contains(group)) {
94             throw new UserMgtException(DUPLICATE_GROUP, group);
95         }
96
97         groups.add(group);
98     }
99
100     @Override
101     public boolean checkUser(String aUser) {
102         return users.find(aUser) != null;
103     }
104
105     @Override
106     public boolean checkGroup(String aGroup) {
107         return groups.find(aGroup) != null;
108     }
109
110     @Override
111     public boolean checkPassword(String aUser, String aPassword) {
112         User user = requireUser(aUser, "While checking password");
113         return user.checkPassword(aPassword);
114     }
115
116     private User requireUser(String aUser, String aMsg) {
117         User user = users.find(aUser);
118         if (user == null) {
119             throw new UserMgtException(Reason.UNKNOWN_USER, aMsg);
120         }
121         return user;
122     }
123
124     private Group requireGroup(String aGroup, String aMsg) {
125         Group group = groups.find(aGroup);
126         if (group == null) {
127             throw new UserMgtException(Reason.UNKNOWN_GROUP, aMsg);
128         }
129         return group;
130     }
131
132     @Override
133     public boolean changePassword(String aUser, String aOldPassword,
134         String aNewPassword) {
135         User user = requireUser(aUser, "While checking password");
136         boolean res = user.changePassword(aOldPassword, aNewPassword);
137         users.userModified(user);
138         return res;
139     }
140
141     @Override
142     public void setPassword(String aUser, String aPassword) {
143         User user = requireUser(aUser, "While setting password");
144         user.setPassword(aPassword);
145         users.userModified(user);
146     }
147
148     @Override
149     public boolean isInGroup(String aUser, String aGroup) {
150         User user = requireUser(aUser,
151             "While checking whether user belongs to group");
152         return user.isInGroup(aGroup);
153     }
154
155     @Override
156     public int getUserCount() {
157         return users.size();
158     }
159
160     @Override
161     public int getGroupCount() {
162         return groups.size();
163     }
164
165     @Override
166     public List<String> getUsers() {
167         List<String> res = new ArrayList<String>();
168         for (User user : users.list()) {
169             res.add(user.getName());
170         }
171         return res;
172     }
173
174     @Override
175     public List<String> getUsers(String aGroup) {
176         Group group = requireGroup(aGroup, "While finding users for group");
177         List<String> res = new ArrayList<String>();
178         for (User user : users.list(group)) {
179             res.add(user.getName());
180         }
181         return res;
182     }
183
184     @Override
185     public List<String> getGroups() {
186         List<String> res = new ArrayList<String>();
187         for (Group group : groups.list()) {
188             res.add(group.getName());
189         }
190         return res;
191     }
192
193     @Override
194     public List<String> getGroups(String aUser) {
195         User user = requireUser(aUser, "While getting list of groups");
196         List<String> res = new ArrayList<String>();
197         for (Group g : user.getGroups()) {
198             res.add(g.getName());
199         }
200         return res;
201     }
202
203     @Override
204     public void renameUser(String aOldUsername, String aNewUsername) {
205         User user = requireUser(aOldUsername, "While renaming user");
206
207         if (aOldUsername.equals(aNewUsername)) {
208             return; // nothing to do.
209         }
210
211         if (users.find(aNewUsername) != null) {
212             throw new UserMgtException(DUPLICATE_USER, aNewUsername);
213         }
214
215         if (!userValidator.validate(aNewUsername)) {
216             throw new UserMgtException(Reason.INVALID_USERNAME, aNewUsername);
217         }
218
219         user.setName(aNewUsername);
220         users.userModified(user);
221     }
222
223     @Override
224     public void renameGroup(String aOldGroup, String aNewGroup) {
225         Group group = requireGroup(aOldGroup, "While renaming group");
226
227         if (aOldGroup.equals(aNewGroup)) {
228             return; // OK.
229         }
230
231         if (groups.find(aNewGroup) != null) {
232             throw new UserMgtException(DUPLICATE_GROUP, aNewGroup);
233         }
234
235         if (!groupValidator.validate(aNewGroup)) {
236             throw new UserMgtException(Reason.INVALID_GROUPNAME, aNewGroup);
237         }
238
239         group.setName(aNewGroup);
240         groups.groupModified(group);
241         // Because a group has changed, a cached user could contain reference to an old copy of the 
242         // group. Therefore, we clear the cache in this case. 
243         users.clearCache();
244     }
245
246     @Override
247     public void removeUser(String aUser) {
248         User user = requireUser(aUser, "While removing user");
249         boolean removed = users.remove(user);
250         if (!removed) {
251             throw new UserMgtException(Reason.UNKNOWN_USER, aUser);
252         }
253     }
254
255     @Override
256     public void removeGroup(String aGroup) {
257         Group group = requireGroup(aGroup, "While removing group");
258
259         if (getUsers(aGroup).size() > 0) {
260             throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
261         }
262
263         boolean removed = groups.remove(group);
264         if (!removed) {
265             throw new UserMgtException(Reason.UNKNOWN_GROUP, aGroup);
266         }
267     }
268
269     @Override
270     public void addUserToGroup(String aUser, String aGroup) {
271         User user = requireUser(aUser, "While adding user to group");
272         Group group = requireGroup(aGroup, "While adding user to group");
273         user.addGroup(group);
274         users.userModified(user);
275     }
276
277     @Override
278     public void removeUserFromGroup(String aUser, String aGroup) {
279         User user = requireUser(aUser, "While removing user from group");
280         Group group = requireGroup(aGroup, "While removing user from group");
281         user.removeGroup(group);
282         users.userModified(user);
283     }
284 }