Removed DOCUMENT ME comments that were generated and applied source code
[utils] / security / src / main / java / org / wamblee / usermgt / UserAdministrationImpl.java
1 /*
2  * Copyright 2005 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.usermgt;
17
18 import static org.wamblee.usermgt.UserMgtException.Reason.DUPLICATE_GROUP;
19 import static org.wamblee.usermgt.UserMgtException.Reason.DUPLICATE_USER;
20 import static org.wamblee.usermgt.UserMgtException.Reason.GROUP_STILL_OCCUPIED;
21 import static org.wamblee.usermgt.UserMgtException.Reason.TRIVIAL_RENAME;
22 import static org.wamblee.usermgt.UserMgtException.Reason.UNKNOWN_GROUP;
23 import static org.wamblee.usermgt.UserMgtException.Reason.UNKNOWN_USER;
24
25 import java.util.Set;
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     public User createUser(String aUser, String aPassword, Group aGroup)
72         throws UserMgtException {
73         userValidator.validate(aUser);
74         checkGroup(aGroup);
75
76         User user = users.createUser(aUser, aPassword, aGroup);
77
78         return new User(user);
79     }
80
81     /*
82      * (non-Javadoc)
83      * 
84      * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
85      */
86     public Group createGroup(String aName) throws UserMgtException {
87         groupValidator.validate(aName);
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         return new Group(group);
98     }
99
100     /*
101      * (non-Javadoc)
102      * 
103      * @see
104      * org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt
105      * .User)
106      */
107     public void userModified(User aUser) {
108         users.userModified(aUser);
109     }
110
111     /*
112      * (non-Javadoc)
113      * 
114      * @see
115      * org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt
116      * .Group)
117      */
118     public void groupModified(Group aGroup) {
119         groups.groupModified(aGroup);
120     }
121
122     /*
123      * (non-Javadoc)
124      * 
125      * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
126      */
127     public User getUser(String aName) {
128         return users.find(aName);
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         return groups.find(aName);
138     }
139
140     /*
141      * (non-Javadoc)
142      * 
143      * @see org.wamblee.usermgt.UserAdministration#getUsers()
144      */
145     public Set<User> getUsers() {
146         return users.list();
147     }
148
149     /*
150      * (non-Javadoc)
151      * 
152      * @see
153      * org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group
154      * )
155      */
156     public Set<User> getUsers(Group aGroup) {
157         return users.list(aGroup);
158     }
159
160     /*
161      * (non-Javadoc)
162      * 
163      * @see org.wamblee.usermgt.UserAdministration#getGroups()
164      */
165     public Set<Group> getGroups() {
166         return groups.list();
167     }
168
169     /*
170      * (non-Javadoc)
171      * 
172      * @see
173      * org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt
174      * .User)
175      */
176     public void removeUser(User aUser) throws UserMgtException {
177         checkUser(aUser);
178         users.remove(aUser);
179     }
180
181     /*
182      * (non-Javadoc)
183      * 
184      * @see
185      * org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt
186      * .Group)
187      */
188     public void removeGroup(Group aGroup) throws UserMgtException {
189         checkGroup(aGroup);
190
191         if (getUsers(aGroup).size() > 0) {
192             throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
193         }
194
195         groups.remove(aGroup);
196     }
197
198     /*
199      * (non-Javadoc)
200      * 
201      * @see
202      * org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt
203      * .User, java.lang.String)
204      */
205     public void renameUser(User aUser, String aUserName)
206         throws UserMgtException {
207         checkUser(aUser);
208
209         if (aUser.getName().equals(aUserName)) {
210             throw new UserMgtException(TRIVIAL_RENAME, aUser);
211         }
212
213         if (users.find(aUserName) != null) {
214             throw new UserMgtException(DUPLICATE_USER, aUser);
215         }
216
217         userValidator.validate(aUserName);
218         // we are modifying the user so we should re-insert it into the set
219         // after renaming it.
220         users.remove(aUser);
221         aUser.setName(aUserName);
222         users.add(aUser);
223     }
224
225     /*
226      * (non-Javadoc)
227      * 
228      * @see
229      * org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt
230      * .Group, java.lang.String)
231      */
232     public void renameGroup(Group aGroup, String aGroupName)
233         throws UserMgtException {
234         checkGroup(aGroup);
235
236         if (aGroup.getName().equals(aGroupName)) {
237             throw new UserMgtException(TRIVIAL_RENAME, aGroup);
238         }
239
240         if (groups.find(aGroupName) != null) {
241             throw new UserMgtException(DUPLICATE_GROUP, aGroup);
242         }
243
244         groupValidator.validate(aGroupName);
245         // we are renaming the group so we should re-insert it into the set
246         // after renaming it.
247         groups.remove(aGroup);
248         aGroup.setName(aGroupName);
249         groups.add(aGroup);
250     }
251
252     /*
253      * (non-Javadoc)
254      * 
255      * @see
256      * org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt
257      * .User, org.wamblee.usermgt.Group)
258      */
259     public void addUserToGroup(User aUser, Group aGroup)
260         throws UserMgtException {
261         checkUser(aUser);
262         checkGroup(aGroup);
263         aUser.addGroup(aGroup);
264         users.userModified(aUser);
265     }
266
267     /*
268      * (non-Javadoc)
269      * 
270      * @see
271      * org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee
272      * .usermgt.User, org.wamblee.usermgt.Group)
273      */
274     public void removeUserFromGroup(User aUser, Group aGroup)
275         throws UserMgtException {
276         checkUser(aUser);
277         checkGroup(aGroup);
278         aUser.removeGroup(aGroup);
279         users.userModified(aUser);
280     }
281
282     /**
283      * 
284      * @param aUser
285      * 
286      * @throws UserMgtException
287      */
288     private void checkUser(User aUser) throws UserMgtException {
289         if (!users.contains(aUser)) {
290             throw new UserMgtException(UNKNOWN_USER, aUser);
291         }
292     }
293
294     /**
295      * 
296      * @param aGroup
297      * 
298      * @throws UserMgtException
299      */
300     private void checkGroup(Group aGroup) throws UserMgtException {
301         if (!groups.contains(aGroup)) {
302             throw new UserMgtException(UNKNOWN_GROUP, aGroup);
303         }
304     }
305
306     /*
307      * (non-Javadoc)
308      * 
309      * @see org.wamblee.usermgt.UserAdministration#getUserCount()
310      */
311     public int getUserCount() {
312         return users.size();
313     }
314
315     /*
316      * (non-Javadoc)
317      * 
318      * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
319      */
320     public int getGroupCount() {
321         return groups.size();
322     }
323 }