Moved over some of the security stuff from Photos.
[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 public class UserAdministrationImpl implements UserAdministration {
32    
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, NameValidator aUserValidator, 
58               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         User user = _users.createUser(aUser, aPassword, aGroup);
76         return new User(user);
77     }
78     
79     /*
80      * (non-Javadoc)
81      * 
82      * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
83      */
84     public Group createGroup(String aName) throws UserMgtException {
85         _groupValidator.validate(aName);
86         Group group = new Group(aName);
87         if (_groups.contains(group)) {
88             throw new UserMgtException(DUPLICATE_GROUP, group);
89         }
90         _groups.add(group);
91         return new Group(group);
92     }
93
94     /*
95      * (non-Javadoc)
96      * 
97      * @see org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt.User)
98      */
99     public void userModified(User aUser) {
100         _users.userModified(aUser);
101     }
102
103     /*
104      * (non-Javadoc)
105      * 
106      * @see org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt.Group)
107      */
108     public void groupModified(Group aGroup) {
109         _groups.groupModified(aGroup);
110     }
111
112     /*
113      * (non-Javadoc)
114      * 
115      * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
116      */
117     public User getUser(String aName) {
118         return _users.find(aName);
119     }
120
121     /*
122      * (non-Javadoc)
123      * 
124      * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
125      */
126     public Group getGroup(String aName) {
127         return _groups.find(aName);
128     }
129
130     /*
131      * (non-Javadoc)
132      * 
133      * @see org.wamblee.usermgt.UserAdministration#getUsers()
134      */
135     public Set<User> getUsers() {
136         return _users.list();
137     }
138
139     /*
140      * (non-Javadoc)
141      * 
142      * @see org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group)
143      */
144     public Set<User> getUsers(Group aGroup) {
145         return _users.list(aGroup);
146     }
147
148     /*
149      * (non-Javadoc)
150      * 
151      * @see org.wamblee.usermgt.UserAdministration#getGroups()
152      */
153     public Set<Group> getGroups() {
154         return _groups.list();
155     }
156
157     /*
158      * (non-Javadoc)
159      * 
160      * @see org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt.User)
161      */
162     public void removeUser(User aUser) throws UserMgtException {
163         checkUser(aUser);
164         _users.remove(aUser);
165     }
166
167     /*
168      * (non-Javadoc)
169      * 
170      * @see org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt.Group)
171      */
172     public void removeGroup(Group aGroup) throws UserMgtException {
173         checkGroup(aGroup);
174         if (getUsers(aGroup).size() > 0) {
175             throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
176         }
177         _groups.remove(aGroup);
178     }
179
180     /*
181      * (non-Javadoc)
182      * 
183      * @see org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt.User,
184      *      java.lang.String)
185      */
186     public void renameUser(User aUser, String aUserName)
187             throws UserMgtException {
188         checkUser(aUser);
189         if (aUser.getName().equals(aUserName)) {
190             throw new UserMgtException(TRIVIAL_RENAME, aUser);
191         }
192         if (_users.find(aUserName) != null) {
193             throw new UserMgtException(DUPLICATE_USER, aUser);
194         }
195         _userValidator.validate(aUserName);
196         // we are modifying the user so we should re-insert it into the set
197         // after renaming it.
198         _users.remove(aUser);
199         aUser.setName(aUserName);
200         _users.add(aUser);
201     }
202
203     /*
204      * (non-Javadoc)
205      * 
206      * @see org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt.Group,
207      *      java.lang.String)
208      */
209     public void renameGroup(Group aGroup, String aGroupName)
210             throws UserMgtException {
211         checkGroup(aGroup);
212         if (aGroup.getName().equals(aGroupName)) {
213             throw new UserMgtException(TRIVIAL_RENAME, aGroup);
214         }
215         if (_groups.find(aGroupName) != null) {
216             throw new UserMgtException(DUPLICATE_GROUP, aGroup);
217         }
218         _groupValidator.validate(aGroupName);
219         // we are renaming the group so we should re-insert it into the set
220         // after renaming it.
221         _groups.remove(aGroup);
222         aGroup.setName(aGroupName);
223         _groups.add(aGroup);
224     }
225
226     /*
227      * (non-Javadoc)
228      * 
229      * @see org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt.User,
230      *      org.wamblee.usermgt.Group)
231      */
232     public void addUserToGroup(User aUser, Group aGroup)
233             throws UserMgtException {
234         checkUser(aUser);
235         checkGroup(aGroup);
236         aUser.addGroup(aGroup);
237         _users.userModified(aUser);
238     }
239
240     /*
241      * (non-Javadoc)
242      * 
243      * @see org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee.usermgt.User,
244      *      org.wamblee.usermgt.Group)
245      */
246     public void removeUserFromGroup(User aUser, Group aGroup)
247             throws UserMgtException {
248         checkUser(aUser);
249         checkGroup(aGroup);
250         aUser.removeGroup(aGroup);
251         _users.userModified(aUser);
252     }
253
254     /**
255      * @param aUser
256      * @throws UserMgtException
257      */
258     private void checkUser(User aUser) throws UserMgtException {
259         if (!_users.contains(aUser)) {
260             throw new UserMgtException(UNKNOWN_USER, aUser);
261         }
262     }
263
264     /**
265      * @param aGroup
266      * @throws UserMgtException
267      */
268     private void checkGroup(Group aGroup) throws UserMgtException {
269         if (!_groups.contains(aGroup)) {
270             throw new UserMgtException(UNKNOWN_GROUP, aGroup);
271         }
272     }
273     
274     /* (non-Javadoc)
275      * @see org.wamblee.usermgt.UserAdministration#getUserCount()
276      */
277     public int getUserCount() {
278         return _users.size(); 
279     }
280     
281     /* (non-Javadoc)
282      * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
283      */
284     public int getGroupCount() {
285         return _groups.size(); 
286     }
287 }