55793da6256c04c0f2788035947d77c0b08dbd91
[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
17 package org.wamblee.usermgt;
18
19 import static org.wamblee.usermgt.UserMgtException.Reason.DUPLICATE_GROUP;
20 import static org.wamblee.usermgt.UserMgtException.Reason.DUPLICATE_USER;
21 import static org.wamblee.usermgt.UserMgtException.Reason.GROUP_STILL_OCCUPIED;
22 import static org.wamblee.usermgt.UserMgtException.Reason.TRIVIAL_RENAME;
23 import static org.wamblee.usermgt.UserMgtException.Reason.UNKNOWN_GROUP;
24 import static org.wamblee.usermgt.UserMgtException.Reason.UNKNOWN_USER;
25
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     /**
36      * All known users.
37      */
38     private UserSet users;
39
40     /**
41      * All known groups.
42      */
43     private GroupSet groups;
44     
45     /**
46      * Validator for user names. 
47      */
48     private NameValidator userValidator;
49     
50     /**
51      * Validator for group names. 
52      */
53     private NameValidator groupValidator;
54     
55     /**
56      * Constructs empty user administration.
57      * 
58      */
59     public UserAdministrationImpl(UserSet aUsers, GroupSet aGroups, NameValidator aUserValidator, 
60               NameValidator aGroupValidator) { 
61         users = aUsers;
62         groups = aGroups;
63         userValidator = aUserValidator;
64         groupValidator = aGroupValidator;
65     }
66
67     /*
68      * (non-Javadoc)
69      * 
70      * @see org.wamblee.usermgt.UserAdministration#createUser(java.lang.String,
71      *      java.lang.String)
72      */
73     public User createUser(String aUser, String aPassword, Group aGroup)
74             throws UserMgtException {
75         userValidator.validate(aUser);
76         checkGroup(aGroup);
77         User user = users.createUser(aUser, aPassword, aGroup);
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         Group group = new Group(aName);
89         if (groups.contains(group)) {
90             throw new UserMgtException(DUPLICATE_GROUP, group);
91         }
92         groups.add(group);
93         return new Group(group);
94     }
95
96     /*
97      * (non-Javadoc)
98      * 
99      * @see org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt.User)
100      */
101     public void userModified(User aUser) {
102         users.userModified(aUser);
103     }
104
105     /*
106      * (non-Javadoc)
107      * 
108      * @see org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt.Group)
109      */
110     public void groupModified(Group aGroup) {
111         groups.groupModified(aGroup);
112     }
113
114     /*
115      * (non-Javadoc)
116      * 
117      * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
118      */
119     public User getUser(String aName) {
120         return users.find(aName);
121     }
122
123     /*
124      * (non-Javadoc)
125      * 
126      * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
127      */
128     public Group getGroup(String aName) {
129         return groups.find(aName);
130     }
131
132     /*
133      * (non-Javadoc)
134      * 
135      * @see org.wamblee.usermgt.UserAdministration#getUsers()
136      */
137     public Set<User> getUsers() {
138         return users.list();
139     }
140
141     /*
142      * (non-Javadoc)
143      * 
144      * @see org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group)
145      */
146     public Set<User> getUsers(Group aGroup) {
147         return users.list(aGroup);
148     }
149
150     /*
151      * (non-Javadoc)
152      * 
153      * @see org.wamblee.usermgt.UserAdministration#getGroups()
154      */
155     public Set<Group> getGroups() {
156         return groups.list();
157     }
158
159     /*
160      * (non-Javadoc)
161      * 
162      * @see org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt.User)
163      */
164     public void removeUser(User aUser) throws UserMgtException {
165         checkUser(aUser);
166         users.remove(aUser);
167     }
168
169     /*
170      * (non-Javadoc)
171      * 
172      * @see org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt.Group)
173      */
174     public void removeGroup(Group aGroup) throws UserMgtException {
175         checkGroup(aGroup);
176         if (getUsers(aGroup).size() > 0) {
177             throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
178         }
179         groups.remove(aGroup);
180     }
181
182     /*
183      * (non-Javadoc)
184      * 
185      * @see org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt.User,
186      *      java.lang.String)
187      */
188     public void renameUser(User aUser, String aUserName)
189             throws UserMgtException {
190         checkUser(aUser);
191         if (aUser.getName().equals(aUserName)) {
192             throw new UserMgtException(TRIVIAL_RENAME, aUser);
193         }
194         if (users.find(aUserName) != null) {
195             throw new UserMgtException(DUPLICATE_USER, aUser);
196         }
197         userValidator.validate(aUserName);
198         // we are modifying the user so we should re-insert it into the set
199         // after renaming it.
200         users.remove(aUser);
201         aUser.setName(aUserName);
202         users.add(aUser);
203     }
204
205     /*
206      * (non-Javadoc)
207      * 
208      * @see org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt.Group,
209      *      java.lang.String)
210      */
211     public void renameGroup(Group aGroup, String aGroupName)
212             throws UserMgtException {
213         checkGroup(aGroup);
214         if (aGroup.getName().equals(aGroupName)) {
215             throw new UserMgtException(TRIVIAL_RENAME, aGroup);
216         }
217         if (groups.find(aGroupName) != null) {
218             throw new UserMgtException(DUPLICATE_GROUP, aGroup);
219         }
220         groupValidator.validate(aGroupName);
221         // we are renaming the group so we should re-insert it into the set
222         // after renaming it.
223         groups.remove(aGroup);
224         aGroup.setName(aGroupName);
225         groups.add(aGroup);
226     }
227
228     /*
229      * (non-Javadoc)
230      * 
231      * @see org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt.User,
232      *      org.wamblee.usermgt.Group)
233      */
234     public void addUserToGroup(User aUser, Group aGroup)
235             throws UserMgtException {
236         checkUser(aUser);
237         checkGroup(aGroup);
238         aUser.addGroup(aGroup);
239         users.userModified(aUser);
240     }
241
242     /*
243      * (non-Javadoc)
244      * 
245      * @see org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee.usermgt.User,
246      *      org.wamblee.usermgt.Group)
247      */
248     public void removeUserFromGroup(User aUser, Group aGroup)
249             throws UserMgtException {
250         checkUser(aUser);
251         checkGroup(aGroup);
252         aUser.removeGroup(aGroup);
253         users.userModified(aUser);
254     }
255
256     /**
257      * @param aUser
258      * @throws UserMgtException
259      */
260     private void checkUser(User aUser) throws UserMgtException {
261         if (!users.contains(aUser)) {
262             throw new UserMgtException(UNKNOWN_USER, aUser);
263         }
264     }
265
266     /**
267      * @param aGroup
268      * @throws UserMgtException
269      */
270     private void checkGroup(Group aGroup) throws UserMgtException {
271         if (!groups.contains(aGroup)) {
272             throw new UserMgtException(UNKNOWN_GROUP, aGroup);
273         }
274     }
275     
276     /* (non-Javadoc)
277      * @see org.wamblee.usermgt.UserAdministration#getUserCount()
278      */
279     public int getUserCount() {
280         return users.size(); 
281     }
282     
283     /* (non-Javadoc)
284      * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
285      */
286     public int getGroupCount() {
287         return groups.size(); 
288     }
289 }