2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.usermgt;
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;
28 * Administration of users and groups.
30 * @author Erik Brakkee
32 public class UserAdministrationImpl implements UserAdministration {
36 private UserSet users;
41 private GroupSet groups;
44 * Validator for user names.
46 private NameValidator userValidator;
49 * Validator for group names.
51 private NameValidator groupValidator;
54 * Constructs empty user administration.
57 public UserAdministrationImpl(UserSet aUsers, GroupSet aGroups,
58 NameValidator aUserValidator, NameValidator aGroupValidator) {
61 userValidator = aUserValidator;
62 groupValidator = aGroupValidator;
68 * @see org.wamblee.usermgt.UserAdministration#createUser(java.lang.String,
71 public User createUser(String aUser, String aPassword, Group aGroup)
72 throws UserMgtException {
73 userValidator.validate(aUser);
76 User user = users.createUser(aUser, aPassword, aGroup);
78 return new User(user);
84 * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
86 public Group createGroup(String aName) throws UserMgtException {
87 groupValidator.validate(aName);
89 Group group = new Group(aName);
91 if (groups.contains(group)) {
92 throw new UserMgtException(DUPLICATE_GROUP, group);
97 return new Group(group);
104 * org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt
107 public void userModified(User aUser) {
108 users.userModified(aUser);
115 * org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt
118 public void groupModified(Group aGroup) {
119 groups.groupModified(aGroup);
125 * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
127 public User getUser(String aName) {
128 return users.find(aName);
134 * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
136 public Group getGroup(String aName) {
137 return groups.find(aName);
143 * @see org.wamblee.usermgt.UserAdministration#getUsers()
145 public Set<User> getUsers() {
153 * org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group
156 public Set<User> getUsers(Group aGroup) {
157 return users.list(aGroup);
163 * @see org.wamblee.usermgt.UserAdministration#getGroups()
165 public Set<Group> getGroups() {
166 return groups.list();
173 * org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt
176 public void removeUser(User aUser) throws UserMgtException {
185 * org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt
188 public void removeGroup(Group aGroup) throws UserMgtException {
191 if (getUsers(aGroup).size() > 0) {
192 throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
195 groups.remove(aGroup);
202 * org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt
203 * .User, java.lang.String)
205 public void renameUser(User aUser, String aUserName)
206 throws UserMgtException {
209 if (aUser.getName().equals(aUserName)) {
210 throw new UserMgtException(TRIVIAL_RENAME, aUser);
213 if (users.find(aUserName) != null) {
214 throw new UserMgtException(DUPLICATE_USER, aUser);
217 userValidator.validate(aUserName);
218 // we are modifying the user so we should re-insert it into the set
219 // after renaming it.
221 aUser.setName(aUserName);
229 * org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt
230 * .Group, java.lang.String)
232 public void renameGroup(Group aGroup, String aGroupName)
233 throws UserMgtException {
236 if (aGroup.getName().equals(aGroupName)) {
237 throw new UserMgtException(TRIVIAL_RENAME, aGroup);
240 if (groups.find(aGroupName) != null) {
241 throw new UserMgtException(DUPLICATE_GROUP, aGroup);
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);
256 * org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt
257 * .User, org.wamblee.usermgt.Group)
259 public void addUserToGroup(User aUser, Group aGroup)
260 throws UserMgtException {
263 aUser.addGroup(aGroup);
264 users.userModified(aUser);
271 * org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee
272 * .usermgt.User, org.wamblee.usermgt.Group)
274 public void removeUserFromGroup(User aUser, Group aGroup)
275 throws UserMgtException {
278 aUser.removeGroup(aGroup);
279 users.userModified(aUser);
286 * @throws UserMgtException
288 private void checkUser(User aUser) throws UserMgtException {
289 if (!users.contains(aUser)) {
290 throw new UserMgtException(UNKNOWN_USER, aUser);
298 * @throws UserMgtException
300 private void checkGroup(Group aGroup) throws UserMgtException {
301 if (!groups.contains(aGroup)) {
302 throw new UserMgtException(UNKNOWN_GROUP, aGroup);
309 * @see org.wamblee.usermgt.UserAdministration#getUserCount()
311 public int getUserCount() {
318 * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
320 public int getGroupCount() {
321 return groups.size();