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);
219 aUser.setName(aUserName);
220 users.userModified(aUser);
227 * org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt
228 * .Group, java.lang.String)
230 public void renameGroup(Group aGroup, String aGroupName)
231 throws UserMgtException {
234 if (aGroup.getName().equals(aGroupName)) {
235 throw new UserMgtException(TRIVIAL_RENAME, aGroup);
238 if (groups.find(aGroupName) != null) {
239 throw new UserMgtException(DUPLICATE_GROUP, aGroup);
242 groupValidator.validate(aGroupName);
244 aGroup.setName(aGroupName);
245 groups.groupModified(aGroup);
252 * org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt
253 * .User, org.wamblee.usermgt.Group)
255 public void addUserToGroup(User aUser, Group aGroup)
256 throws UserMgtException {
259 aUser.addGroup(aGroup);
260 users.userModified(aUser);
267 * org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee
268 * .usermgt.User, org.wamblee.usermgt.Group)
270 public void removeUserFromGroup(User aUser, Group aGroup)
271 throws UserMgtException {
274 aUser.removeGroup(aGroup);
275 users.userModified(aUser);
282 * @throws UserMgtException
284 private void checkUser(User aUser) throws UserMgtException {
285 if (!users.contains(aUser)) {
286 throw new UserMgtException(UNKNOWN_USER, aUser);
294 * @throws UserMgtException
296 private void checkGroup(Group aGroup) throws UserMgtException {
297 if (!groups.contains(aGroup)) {
298 throw new UserMgtException(UNKNOWN_GROUP, aGroup);
305 * @see org.wamblee.usermgt.UserAdministration#getUserCount()
307 public int getUserCount() {
314 * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
316 public int getGroupCount() {
317 return groups.size();