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