6866d45416a6e93f9eb4b9abfff262d62ff14d66
[utils] / security / impl / src / main / java / org / wamblee / usermgt / 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.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   
219         aUser.setName(aUserName);
220         users.userModified(aUser);
221     }
222
223     /*
224      * (non-Javadoc)
225      * 
226      * @see
227      * org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt
228      * .Group, java.lang.String)
229      */
230     public void renameGroup(Group aGroup, String aGroupName)
231         throws UserMgtException {
232         checkGroup(aGroup);
233
234         if (aGroup.getName().equals(aGroupName)) {
235             throw new UserMgtException(TRIVIAL_RENAME, aGroup);
236         }
237
238         if (groups.find(aGroupName) != null) {
239             throw new UserMgtException(DUPLICATE_GROUP, aGroup);
240         }
241
242         groupValidator.validate(aGroupName);
243       
244         aGroup.setName(aGroupName);
245         groups.groupModified(aGroup);
246     }
247
248     /*
249      * (non-Javadoc)
250      * 
251      * @see
252      * org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt
253      * .User, org.wamblee.usermgt.Group)
254      */
255     public void addUserToGroup(User aUser, Group aGroup)
256         throws UserMgtException {
257         checkUser(aUser);
258         checkGroup(aGroup);
259         aUser.addGroup(aGroup);
260         users.userModified(aUser);
261     }
262
263     /*
264      * (non-Javadoc)
265      * 
266      * @see
267      * org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee
268      * .usermgt.User, org.wamblee.usermgt.Group)
269      */
270     public void removeUserFromGroup(User aUser, Group aGroup)
271         throws UserMgtException {
272         checkUser(aUser);
273         checkGroup(aGroup);
274         aUser.removeGroup(aGroup);
275         users.userModified(aUser);
276     }
277
278     /**
279      * 
280      * @param aUser
281      * 
282      * @throws UserMgtException
283      */
284     private void checkUser(User aUser) throws UserMgtException {
285         if (!users.contains(aUser)) {
286             throw new UserMgtException(UNKNOWN_USER, aUser);
287         }
288     }
289
290     /**
291      * 
292      * @param aGroup
293      * 
294      * @throws UserMgtException
295      */
296     private void checkGroup(Group aGroup) throws UserMgtException {
297         if (!groups.contains(aGroup)) {
298             throw new UserMgtException(UNKNOWN_GROUP, aGroup);
299         }
300     }
301
302     /*
303      * (non-Javadoc)
304      * 
305      * @see org.wamblee.usermgt.UserAdministration#getUserCount()
306      */
307     public int getUserCount() {
308         return users.size();
309     }
310
311     /*
312      * (non-Javadoc)
313      * 
314      * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
315      */
316     public int getGroupCount() {
317         return groups.size();
318     }
319 }