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.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
25 import org.wamblee.security.authentication.UserMgtException.Reason;
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,
72 public void createUser(String aUser, String aPassword) {
73 if (!userValidator.validate(aUser)) {
74 throw new UserMgtException(Reason.INVALID_USERNAME, aUser);
77 users.createUser(aUser, aPassword);
83 * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
86 public void createGroup(String aName) {
87 if (!groupValidator.validate(aName)) {
88 throw new UserMgtException(Reason.INVALID_GROUPNAME, aName);
91 Group group = new Group(aName);
93 if (groups.contains(group)) {
94 throw new UserMgtException(DUPLICATE_GROUP, group);
101 public boolean checkUser(String aUser) {
102 return users.find(aUser) != null;
106 public boolean checkGroup(String aGroup) {
107 return groups.find(aGroup) != null;
111 public boolean checkPassword(String aUser, String aPassword) {
112 User user = requireUser(aUser, "While checking password");
113 return user.checkPassword(aPassword);
116 private User requireUser(String aUser, String aMsg) {
117 User user = users.find(aUser);
119 throw new UserMgtException(Reason.UNKNOWN_USER, aMsg);
124 private Group requireGroup(String aGroup, String aMsg) {
125 Group group = groups.find(aGroup);
127 throw new UserMgtException(Reason.UNKNOWN_GROUP, aMsg);
133 public boolean changePassword(String aUser, String aOldPassword,
134 String aNewPassword) {
135 User user = requireUser(aUser, "While checking password");
136 boolean res = user.changePassword(aOldPassword, aNewPassword);
137 users.userModified(user);
142 public void setPassword(String aUser, String aPassword) {
143 User user = requireUser(aUser, "While setting password");
144 user.setPassword(aPassword);
145 users.userModified(user);
149 public boolean isInGroup(String aUser, String aGroup) {
150 User user = requireUser(aUser,
151 "While checking whether user belongs to group");
152 return user.isInGroup(aGroup);
156 public int getUserCount() {
161 public int getGroupCount() {
162 return groups.size();
166 public List<String> getUsers() {
167 List<String> res = new ArrayList<String>();
168 for (User user : users.list()) {
169 res.add(user.getName());
175 public List<String> getUsers(String aGroup) {
176 Group group = requireGroup(aGroup, "While finding users for group");
177 List<String> res = new ArrayList<String>();
178 for (User user : users.list(group)) {
179 res.add(user.getName());
185 public List<String> getGroups() {
186 List<String> res = new ArrayList<String>();
187 for (Group group : groups.list()) {
188 res.add(group.getName());
194 public List<String> getGroups(String aUser) {
195 User user = requireUser(aUser, "While getting list of groups");
196 List<String> res = new ArrayList<String>();
197 for (Group g : user.getGroups()) {
198 res.add(g.getName());
204 public void renameUser(String aOldUsername, String aNewUsername) {
205 User user = requireUser(aOldUsername, "While renaming user");
207 if (aOldUsername.equals(aNewUsername)) {
208 return; // nothing to do.
211 if (users.find(aNewUsername) != null) {
212 throw new UserMgtException(DUPLICATE_USER, aNewUsername);
215 if (!userValidator.validate(aNewUsername)) {
216 throw new UserMgtException(Reason.INVALID_USERNAME, aNewUsername);
219 user.setName(aNewUsername);
220 users.userModified(user);
224 public void renameGroup(String aOldGroup, String aNewGroup) {
225 Group group = requireGroup(aOldGroup, "While renaming group");
227 if (aOldGroup.equals(aNewGroup)) {
231 if (groups.find(aNewGroup) != null) {
232 throw new UserMgtException(DUPLICATE_GROUP, aNewGroup);
235 if (!groupValidator.validate(aNewGroup)) {
236 throw new UserMgtException(Reason.INVALID_GROUPNAME, aNewGroup);
239 group.setName(aNewGroup);
240 groups.groupModified(group);
241 // Because a group has changed, a cached user could contain reference to an old copy of the
242 // group. Therefore, we clear the cache in this case.
247 public void removeUser(String aUser) {
248 User user = requireUser(aUser, "While removing user");
249 boolean removed = users.remove(user);
251 throw new UserMgtException(Reason.UNKNOWN_USER, aUser);
256 public void removeGroup(String aGroup) {
257 Group group = requireGroup(aGroup, "While removing group");
259 if (getUsers(aGroup).size() > 0) {
260 throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
263 boolean removed = groups.remove(group);
265 throw new UserMgtException(Reason.UNKNOWN_GROUP, aGroup);
270 public void addUserToGroup(String aUser, String aGroup) {
271 User user = requireUser(aUser, "While adding user to group");
272 Group group = requireGroup(aGroup, "While adding user to group");
273 user.addGroup(group);
274 users.userModified(user);
278 public void removeUserFromGroup(String aUser, String aGroup) {
279 User user = requireUser(aUser, "While removing user from group");
280 Group group = requireGroup(aGroup, "While removing user from group");
281 user.removeGroup(group);
282 users.userModified(user);