(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.HashSet;
21 import java.util.Set;
22
23 /**
24  * Administration of users and groups.
25  * 
26  * @author Erik Brakkee
27  */
28 public class UserAdministrationImpl implements UserAdministration {
29     /**
30      * All known users.
31      */
32     private UserSet users;
33
34     /**
35      * All known groups.
36      */
37     private GroupSet groups;
38
39     /**
40      * Validator for user names.
41      */
42     private NameValidator userValidator;
43
44     /**
45      * Validator for group names.
46      */
47     private NameValidator groupValidator;
48
49     /**
50      * Constructs empty user administration.
51      * 
52      */
53     public UserAdministrationImpl(UserSet aUsers, GroupSet aGroups,
54         NameValidator aUserValidator, NameValidator aGroupValidator) {
55         users = aUsers;
56         groups = aGroups;
57         userValidator = aUserValidator;
58         groupValidator = aGroupValidator;
59     }
60
61     /*
62      * (non-Javadoc)
63      * 
64      * @see org.wamblee.usermgt.UserAdministration#createUser(java.lang.String,
65      * java.lang.String)
66      */
67     public User createUser(String aUser, String aPassword, Group aGroup)
68         throws UserMgtException {
69         userValidator.validate(aUser);
70         checkGroup(aGroup);
71
72         User user = users.createUser(aUser, aPassword, aGroup);
73
74         return new User(user);
75     }
76
77     /*
78      * (non-Javadoc)
79      * 
80      * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
81      */
82     public Group createGroup(String aName) throws UserMgtException {
83         groupValidator.validate(aName);
84
85         Group group = new Group(aName);
86
87         if (groups.contains(group)) {
88             throw new UserMgtException(DUPLICATE_GROUP, group);
89         }
90
91         groups.add(group);
92
93         return new Group(group);
94     }
95
96     /*
97      * (non-Javadoc)
98      * 
99      * @see
100      * org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt
101      * .User)
102      */
103     public void userModified(User aUser) {
104         users.userModified(aUser);
105     }
106
107     /*
108      * (non-Javadoc)
109      * 
110      * @see
111      * org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt
112      * .Group)
113      */
114     public void groupModified(Group aGroup) {
115         groups.groupModified(aGroup);
116     }
117
118     /*
119      * (non-Javadoc)
120      * 
121      * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
122      */
123     public User getUser(String aName) {
124         User user = users.find(aName);
125         if (user == null) {
126             return user;
127         }
128         return new User(user);
129     }
130
131     /*
132      * (non-Javadoc)
133      * 
134      * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
135      */
136     public Group getGroup(String aName) {
137         Group group = groups.find(aName);
138         if ( group == null ) {
139             return group;
140         }
141         return new Group(group);
142     }
143
144     /*
145      * (non-Javadoc)
146      * 
147      * @see org.wamblee.usermgt.UserAdministration#getUsers()
148      */
149     public Set<User> getUsers() {
150         Set<User> res = new HashSet<User>(); 
151         for (User user: users.list()) { 
152             res.add(new User(user));
153         }
154         return res; 
155     }
156
157     /*
158      * (non-Javadoc)
159      * 
160      * @see
161      * org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group
162      * )
163      */
164     public Set<User> getUsers(Group aGroup) {
165         Set<User> res = new HashSet<User>(); 
166         for (User user: users.list(aGroup)) { 
167             res.add(new User(user));
168         }
169         return res;
170     }
171
172     /*
173      * (non-Javadoc)
174      * 
175      * @see org.wamblee.usermgt.UserAdministration#getGroups()
176      */
177     public Set<Group> getGroups() {
178         Set<Group> res = new HashSet<Group>(); 
179         for (Group group: groups.list()) { 
180             res.add(new Group(group));
181         }
182         return res; 
183     }
184
185     /*
186      * (non-Javadoc)
187      * 
188      * @see
189      * org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt
190      * .User)
191      */
192     public void removeUser(User aUser) throws UserMgtException {
193         checkUser(aUser);
194         users.remove(aUser);
195     }
196
197     /*
198      * (non-Javadoc)
199      * 
200      * @see
201      * org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt
202      * .Group)
203      */
204     public void removeGroup(Group aGroup) throws UserMgtException {
205         checkGroup(aGroup);
206
207         if (getUsers(aGroup).size() > 0) {
208             throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
209         }
210
211         groups.remove(aGroup);
212     }
213
214     /*
215      * (non-Javadoc)
216      * 
217      * @see
218      * org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt
219      * .User, java.lang.String)
220      */
221     public void renameUser(User aUser, String aUserName)
222         throws UserMgtException {
223         checkUser(aUser);
224
225         if (aUser.getName().equals(aUserName)) {
226             throw new UserMgtException(TRIVIAL_RENAME, aUser);
227         }
228
229         if (users.find(aUserName) != null) {
230             throw new UserMgtException(DUPLICATE_USER, aUser);
231         }
232
233         userValidator.validate(aUserName);
234
235         aUser.setName(aUserName);
236         users.userModified(aUser);
237     }
238
239     /*
240      * (non-Javadoc)
241      * 
242      * @see
243      * org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt
244      * .Group, java.lang.String)
245      */
246     public void renameGroup(Group aGroup, String aGroupName)
247         throws UserMgtException {
248         checkGroup(aGroup);
249
250         if (aGroup.getName().equals(aGroupName)) {
251             throw new UserMgtException(TRIVIAL_RENAME, aGroup);
252         }
253
254         if (groups.find(aGroupName) != null) {
255             throw new UserMgtException(DUPLICATE_GROUP, aGroup);
256         }
257
258         groupValidator.validate(aGroupName);
259
260         aGroup.setName(aGroupName);
261         groups.groupModified(aGroup);
262     }
263
264     /*
265      * (non-Javadoc)
266      * 
267      * @see
268      * org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt
269      * .User, org.wamblee.usermgt.Group)
270      */
271     public void addUserToGroup(User aUser, Group aGroup)
272         throws UserMgtException {
273         checkUser(aUser);
274         checkGroup(aGroup);
275         aUser.addGroup(aGroup);
276         users.userModified(aUser);
277     }
278
279     /*
280      * (non-Javadoc)
281      * 
282      * @see
283      * org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee
284      * .usermgt.User, org.wamblee.usermgt.Group)
285      */
286     public void removeUserFromGroup(User aUser, Group aGroup)
287         throws UserMgtException {
288         checkUser(aUser);
289         checkGroup(aGroup);
290         aUser.removeGroup(aGroup);
291         users.userModified(aUser);
292     }
293
294     /**
295      * 
296      * @param aUser
297      * 
298      * @throws UserMgtException
299      */
300     private void checkUser(User aUser) throws UserMgtException {
301         if (!users.contains(aUser)) {
302             throw new UserMgtException(UNKNOWN_USER, aUser);
303         }
304     }
305
306     /**
307      * 
308      * @param aGroup
309      * 
310      * @throws UserMgtException
311      */
312     private void checkGroup(Group aGroup) throws UserMgtException {
313         if (!groups.contains(aGroup)) {
314             throw new UserMgtException(UNKNOWN_GROUP, aGroup);
315         }
316     }
317
318     /*
319      * (non-Javadoc)
320      * 
321      * @see org.wamblee.usermgt.UserAdministration#getUserCount()
322      */
323     public int getUserCount() {
324         return users.size();
325     }
326
327     /*
328      * (non-Javadoc)
329      * 
330      * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
331      */
332     public int getGroupCount() {
333         return groups.size();
334     }
335 }