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;
25 import java.util.HashSet;
29 * Administration of users and groups.
31 * @author Erik Brakkee
33 public class UserAdministrationImpl implements UserAdministration {
37 private UserSet users;
42 private GroupSet groups;
45 * Validator for user names.
47 private NameValidator userValidator;
50 * Validator for group names.
52 private NameValidator groupValidator;
55 * Constructs empty user administration.
58 public UserAdministrationImpl(UserSet aUsers, GroupSet aGroups,
59 NameValidator aUserValidator, NameValidator aGroupValidator) {
62 userValidator = aUserValidator;
63 groupValidator = aGroupValidator;
69 * @see org.wamblee.usermgt.UserAdministration#createUser(java.lang.String,
72 public User createUser(String aUser, String aPassword, Group aGroup)
73 throws UserMgtException {
74 userValidator.validate(aUser);
77 User user = users.createUser(aUser, aPassword, aGroup);
79 return new User(user);
85 * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
87 public Group createGroup(String aName) throws UserMgtException {
88 groupValidator.validate(aName);
90 Group group = new Group(aName);
92 if (groups.contains(group)) {
93 throw new UserMgtException(DUPLICATE_GROUP, group);
98 return new Group(group);
105 * org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt
108 public void userModified(User aUser) {
109 users.userModified(aUser);
116 * org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt
119 public void groupModified(Group aGroup) {
120 groups.groupModified(aGroup);
126 * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
128 public User getUser(String aName) {
129 User user = users.find(aName);
133 return new User(user);
139 * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
141 public Group getGroup(String aName) {
142 Group group = groups.find(aName);
143 if ( group == null ) {
146 return new Group(group);
152 * @see org.wamblee.usermgt.UserAdministration#getUsers()
154 public Set<User> getUsers() {
155 Set<User> res = new HashSet<User>();
156 for (User user: users.list()) {
157 res.add(new User(user));
166 * org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group
169 public Set<User> getUsers(Group aGroup) {
170 Set<User> res = new HashSet<User>();
171 for (User user: users.list(aGroup)) {
172 res.add(new User(user));
180 * @see org.wamblee.usermgt.UserAdministration#getGroups()
182 public Set<Group> getGroups() {
183 Set<Group> res = new HashSet<Group>();
184 for (Group group: groups.list()) {
185 res.add(new Group(group));
194 * org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt
197 public void removeUser(User aUser) throws UserMgtException {
206 * org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt
209 public void removeGroup(Group aGroup) throws UserMgtException {
212 if (getUsers(aGroup).size() > 0) {
213 throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
216 groups.remove(aGroup);
223 * org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt
224 * .User, java.lang.String)
226 public void renameUser(User aUser, String aUserName)
227 throws UserMgtException {
230 if (aUser.getName().equals(aUserName)) {
231 throw new UserMgtException(TRIVIAL_RENAME, aUser);
234 if (users.find(aUserName) != null) {
235 throw new UserMgtException(DUPLICATE_USER, aUser);
238 userValidator.validate(aUserName);
240 aUser.setName(aUserName);
241 users.userModified(aUser);
248 * org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt
249 * .Group, java.lang.String)
251 public void renameGroup(Group aGroup, String aGroupName)
252 throws UserMgtException {
255 if (aGroup.getName().equals(aGroupName)) {
256 throw new UserMgtException(TRIVIAL_RENAME, aGroup);
259 if (groups.find(aGroupName) != null) {
260 throw new UserMgtException(DUPLICATE_GROUP, aGroup);
263 groupValidator.validate(aGroupName);
265 aGroup.setName(aGroupName);
266 groups.groupModified(aGroup);
273 * org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt
274 * .User, org.wamblee.usermgt.Group)
276 public void addUserToGroup(User aUser, Group aGroup)
277 throws UserMgtException {
280 aUser.addGroup(aGroup);
281 users.userModified(aUser);
288 * org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee
289 * .usermgt.User, org.wamblee.usermgt.Group)
291 public void removeUserFromGroup(User aUser, Group aGroup)
292 throws UserMgtException {
295 aUser.removeGroup(aGroup);
296 users.userModified(aUser);
303 * @throws UserMgtException
305 private void checkUser(User aUser) throws UserMgtException {
306 if (!users.contains(aUser)) {
307 throw new UserMgtException(UNKNOWN_USER, aUser);
315 * @throws UserMgtException
317 private void checkGroup(Group aGroup) throws UserMgtException {
318 if (!groups.contains(aGroup)) {
319 throw new UserMgtException(UNKNOWN_GROUP, aGroup);
326 * @see org.wamblee.usermgt.UserAdministration#getUserCount()
328 public int getUserCount() {
335 * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
337 public int getGroupCount() {
338 return groups.size();