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