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.security.authentication;
18 import static org.wamblee.security.authentication.UserMgtException.Reason.*;
20 import java.util.HashSet;
24 * Administration of users and groups.
26 * @author Erik Brakkee
28 public class UserAdministrationImpl implements UserAdministration {
32 private UserSet users;
37 private GroupSet groups;
40 * Validator for user names.
42 private NameValidator userValidator;
45 * Validator for group names.
47 private NameValidator groupValidator;
50 * Constructs empty user administration.
53 public UserAdministrationImpl(UserSet aUsers, GroupSet aGroups,
54 NameValidator aUserValidator, NameValidator aGroupValidator) {
57 userValidator = aUserValidator;
58 groupValidator = aGroupValidator;
64 * @see org.wamblee.usermgt.UserAdministration#createUser(java.lang.String,
67 public User createUser(String aUser, String aPassword, Group aGroup)
68 throws UserMgtException {
69 userValidator.validate(aUser);
72 User user = users.createUser(aUser, aPassword, aGroup);
74 return new User(user);
80 * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
82 public Group createGroup(String aName) throws UserMgtException {
83 groupValidator.validate(aName);
85 Group group = new Group(aName);
87 if (groups.contains(group)) {
88 throw new UserMgtException(DUPLICATE_GROUP, group);
93 return new Group(group);
100 * org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt
103 public void userModified(User aUser) {
104 users.userModified(aUser);
111 * org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt
114 public void groupModified(Group aGroup) {
115 groups.groupModified(aGroup);
121 * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
123 public User getUser(String aName) {
124 User user = users.find(aName);
128 return new User(user);
134 * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
136 public Group getGroup(String aName) {
137 Group group = groups.find(aName);
138 if ( group == null ) {
141 return new Group(group);
147 * @see org.wamblee.usermgt.UserAdministration#getUsers()
149 public Set<User> getUsers() {
150 Set<User> res = new HashSet<User>();
151 for (User user: users.list()) {
152 res.add(new User(user));
161 * org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group
164 public Set<User> getUsers(Group aGroup) {
165 Set<User> res = new HashSet<User>();
166 for (User user: users.list(aGroup)) {
167 res.add(new User(user));
175 * @see org.wamblee.usermgt.UserAdministration#getGroups()
177 public Set<Group> getGroups() {
178 Set<Group> res = new HashSet<Group>();
179 for (Group group: groups.list()) {
180 res.add(new Group(group));
189 * org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt
192 public void removeUser(User aUser) throws UserMgtException {
201 * org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt
204 public void removeGroup(Group aGroup) throws UserMgtException {
207 if (getUsers(aGroup).size() > 0) {
208 throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
211 groups.remove(aGroup);
218 * org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt
219 * .User, java.lang.String)
221 public void renameUser(User aUser, String aUserName)
222 throws UserMgtException {
225 if (aUser.getName().equals(aUserName)) {
226 throw new UserMgtException(TRIVIAL_RENAME, aUser);
229 if (users.find(aUserName) != null) {
230 throw new UserMgtException(DUPLICATE_USER, aUser);
233 userValidator.validate(aUserName);
235 aUser.setName(aUserName);
236 users.userModified(aUser);
243 * org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt
244 * .Group, java.lang.String)
246 public void renameGroup(Group aGroup, String aGroupName)
247 throws UserMgtException {
250 if (aGroup.getName().equals(aGroupName)) {
251 throw new UserMgtException(TRIVIAL_RENAME, aGroup);
254 if (groups.find(aGroupName) != null) {
255 throw new UserMgtException(DUPLICATE_GROUP, aGroup);
258 groupValidator.validate(aGroupName);
260 aGroup.setName(aGroupName);
261 groups.groupModified(aGroup);
268 * org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt
269 * .User, org.wamblee.usermgt.Group)
271 public void addUserToGroup(User aUser, Group aGroup)
272 throws UserMgtException {
275 aUser.addGroup(aGroup);
276 users.userModified(aUser);
283 * org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee
284 * .usermgt.User, org.wamblee.usermgt.Group)
286 public void removeUserFromGroup(User aUser, Group aGroup)
287 throws UserMgtException {
290 aUser.removeGroup(aGroup);
291 users.userModified(aUser);
298 * @throws UserMgtException
300 private void checkUser(User aUser) throws UserMgtException {
301 if (!users.contains(aUser)) {
302 throw new UserMgtException(UNKNOWN_USER, aUser);
310 * @throws UserMgtException
312 private void checkGroup(Group aGroup) throws UserMgtException {
313 if (!groups.contains(aGroup)) {
314 throw new UserMgtException(UNKNOWN_GROUP, aGroup);
321 * @see org.wamblee.usermgt.UserAdministration#getUserCount()
323 public int getUserCount() {
330 * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
332 public int getGroupCount() {
333 return groups.size();