2 * Copyright 2005 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.
17 package org.wamblee.usermgt;
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;
29 * Administration of users and groups.
31 * @author Erik Brakkee
33 public class UserAdministrationImpl implements UserAdministration {
38 private UserSet _users;
43 private GroupSet _groups;
46 * Validator for user names.
48 private NameValidator _userValidator;
51 * Validator for group names.
53 private NameValidator _groupValidator;
56 * Constructs empty user administration.
59 public UserAdministrationImpl(UserSet aUsers, GroupSet aGroups, NameValidator aUserValidator,
60 NameValidator aGroupValidator) {
63 _userValidator = aUserValidator;
64 _groupValidator = aGroupValidator;
70 * @see org.wamblee.usermgt.UserAdministration#createUser(java.lang.String,
73 public User createUser(String aUser, String aPassword, Group aGroup)
74 throws UserMgtException {
75 _userValidator.validate(aUser);
77 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);
88 Group group = new Group(aName);
89 if (_groups.contains(group)) {
90 throw new UserMgtException(DUPLICATE_GROUP, group);
93 return new Group(group);
99 * @see org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt.User)
101 public void userModified(User aUser) {
102 _users.userModified(aUser);
108 * @see org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt.Group)
110 public void groupModified(Group aGroup) {
111 _groups.groupModified(aGroup);
117 * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
119 public User getUser(String aName) {
120 return _users.find(aName);
126 * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
128 public Group getGroup(String aName) {
129 return _groups.find(aName);
135 * @see org.wamblee.usermgt.UserAdministration#getUsers()
137 public Set<User> getUsers() {
138 return _users.list();
144 * @see org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group)
146 public Set<User> getUsers(Group aGroup) {
147 return _users.list(aGroup);
153 * @see org.wamblee.usermgt.UserAdministration#getGroups()
155 public Set<Group> getGroups() {
156 return _groups.list();
162 * @see org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt.User)
164 public void removeUser(User aUser) throws UserMgtException {
166 _users.remove(aUser);
172 * @see org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt.Group)
174 public void removeGroup(Group aGroup) throws UserMgtException {
176 if (getUsers(aGroup).size() > 0) {
177 throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
179 _groups.remove(aGroup);
185 * @see org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt.User,
188 public void renameUser(User aUser, String aUserName)
189 throws UserMgtException {
191 if (aUser.getName().equals(aUserName)) {
192 throw new UserMgtException(TRIVIAL_RENAME, aUser);
194 if (_users.find(aUserName) != null) {
195 throw new UserMgtException(DUPLICATE_USER, aUser);
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);
208 * @see org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt.Group,
211 public void renameGroup(Group aGroup, String aGroupName)
212 throws UserMgtException {
214 if (aGroup.getName().equals(aGroupName)) {
215 throw new UserMgtException(TRIVIAL_RENAME, aGroup);
217 if (_groups.find(aGroupName) != null) {
218 throw new UserMgtException(DUPLICATE_GROUP, aGroup);
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);
231 * @see org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt.User,
232 * org.wamblee.usermgt.Group)
234 public void addUserToGroup(User aUser, Group aGroup)
235 throws UserMgtException {
238 aUser.addGroup(aGroup);
239 _users.userModified(aUser);
245 * @see org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee.usermgt.User,
246 * org.wamblee.usermgt.Group)
248 public void removeUserFromGroup(User aUser, Group aGroup)
249 throws UserMgtException {
252 aUser.removeGroup(aGroup);
253 _users.userModified(aUser);
258 * @throws UserMgtException
260 private void checkUser(User aUser) throws UserMgtException {
261 if (!_users.contains(aUser)) {
262 throw new UserMgtException(UNKNOWN_USER, aUser);
268 * @throws UserMgtException
270 private void checkGroup(Group aGroup) throws UserMgtException {
271 if (!_groups.contains(aGroup)) {
272 throw new UserMgtException(UNKNOWN_GROUP, aGroup);
277 * @see org.wamblee.usermgt.UserAdministration#getUserCount()
279 public int getUserCount() {
280 return _users.size();
284 * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
286 public int getGroupCount() {
287 return _groups.size();