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 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, NameValidator aUserValidator,
58 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);
75 User user = _users.createUser(aUser, aPassword, aGroup);
76 return new User(user);
82 * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
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);
91 return new Group(group);
97 * @see org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt.User)
99 public void userModified(User aUser) {
100 _users.userModified(aUser);
106 * @see org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt.Group)
108 public void groupModified(Group aGroup) {
109 _groups.groupModified(aGroup);
115 * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
117 public User getUser(String aName) {
118 return _users.find(aName);
124 * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
126 public Group getGroup(String aName) {
127 return _groups.find(aName);
133 * @see org.wamblee.usermgt.UserAdministration#getUsers()
135 public Set<User> getUsers() {
136 return _users.list();
142 * @see org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group)
144 public Set<User> getUsers(Group aGroup) {
145 return _users.list(aGroup);
151 * @see org.wamblee.usermgt.UserAdministration#getGroups()
153 public Set<Group> getGroups() {
154 return _groups.list();
160 * @see org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt.User)
162 public void removeUser(User aUser) throws UserMgtException {
164 _users.remove(aUser);
170 * @see org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt.Group)
172 public void removeGroup(Group aGroup) throws UserMgtException {
174 if (getUsers(aGroup).size() > 0) {
175 throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
177 _groups.remove(aGroup);
183 * @see org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt.User,
186 public void renameUser(User aUser, String aUserName)
187 throws UserMgtException {
189 if (aUser.getName().equals(aUserName)) {
190 throw new UserMgtException(TRIVIAL_RENAME, aUser);
192 if (_users.find(aUserName) != null) {
193 throw new UserMgtException(DUPLICATE_USER, aUser);
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);
206 * @see org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt.Group,
209 public void renameGroup(Group aGroup, String aGroupName)
210 throws UserMgtException {
212 if (aGroup.getName().equals(aGroupName)) {
213 throw new UserMgtException(TRIVIAL_RENAME, aGroup);
215 if (_groups.find(aGroupName) != null) {
216 throw new UserMgtException(DUPLICATE_GROUP, aGroup);
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);
229 * @see org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt.User,
230 * org.wamblee.usermgt.Group)
232 public void addUserToGroup(User aUser, Group aGroup)
233 throws UserMgtException {
236 aUser.addGroup(aGroup);
237 _users.userModified(aUser);
243 * @see org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee.usermgt.User,
244 * org.wamblee.usermgt.Group)
246 public void removeUserFromGroup(User aUser, Group aGroup)
247 throws UserMgtException {
250 aUser.removeGroup(aGroup);
251 _users.userModified(aUser);
256 * @throws UserMgtException
258 private void checkUser(User aUser) throws UserMgtException {
259 if (!_users.contains(aUser)) {
260 throw new UserMgtException(UNKNOWN_USER, aUser);
266 * @throws UserMgtException
268 private void checkGroup(Group aGroup) throws UserMgtException {
269 if (!_groups.contains(aGroup)) {
270 throw new UserMgtException(UNKNOWN_GROUP, aGroup);
275 * @see org.wamblee.usermgt.UserAdministration#getUserCount()
277 public int getUserCount() {
278 return _users.size();
282 * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
284 public int getGroupCount() {
285 return _groups.size();