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.List;
23 import org.wamblee.security.authentication.UserMgtException.Reason;
26 * Administration of users and groups.
28 * @author Erik Brakkee
30 public class UserAdministrationImpl implements UserAdministration {
34 private UserSet users;
39 private GroupSet groups;
42 * Validator for user names.
44 private NameValidator userValidator;
47 * Validator for group names.
49 private NameValidator groupValidator;
52 * Constructs empty user administration.
55 public UserAdministrationImpl(UserSet aUsers, GroupSet aGroups,
56 NameValidator aUserValidator, NameValidator aGroupValidator) {
59 userValidator = aUserValidator;
60 groupValidator = aGroupValidator;
66 * @see org.wamblee.usermgt.UserAdministration#createUser(java.lang.String,
70 public void createUser(String aUser, String aPassword) {
71 if (!userValidator.validate(aUser)) {
72 throw new UserMgtException(Reason.INVALID_USERNAME, aUser);
75 users.createUser(aUser, aPassword);
81 * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
84 public void createGroup(String aName) {
85 if (!groupValidator.validate(aName)) {
86 throw new UserMgtException(Reason.INVALID_GROUPNAME, aName);
89 Group group = new Group(aName);
91 if (groups.contains(group)) {
92 throw new UserMgtException(DUPLICATE_GROUP, group);
99 public boolean checkUser(String aUser) {
100 return users.find(aUser) != null;
104 public boolean checkGroup(String aGroup) {
105 return groups.find(aGroup) != null;
109 public boolean checkPassword(String aUser, String aPassword) {
110 User user = requireUser(aUser, "While checking password");
111 return user.checkPassword(aPassword);
114 private User requireUser(String aUser, String aMsg) {
115 User user = users.find(aUser);
117 throw new UserMgtException(Reason.UNKNOWN_USER, aMsg);
122 private Group requireGroup(String aGroup, String aMsg) {
123 Group group = groups.find(aGroup);
125 throw new UserMgtException(Reason.UNKNOWN_GROUP, aMsg);
131 public boolean changePassword(String aUser, String aOldPassword,
132 String aNewPassword) {
133 User user = requireUser(aUser, "While checking password");
134 boolean res = user.changePassword(aOldPassword, aNewPassword);
135 users.userModified(user);
140 public void setPassword(String aUser, String aPassword) {
141 User user = requireUser(aUser, "While setting password");
142 user.setPassword(aPassword);
143 users.userModified(user);
147 public boolean isInGroup(String aUser, String aGroup) {
148 User user = requireUser(aUser,
149 "While checking whether user belongs to group");
150 return user.isInGroup(aGroup);
154 public int getUserCount() {
159 public int getGroupCount() {
160 return groups.size();
164 public List<String> getUsers() {
165 List<String> res = new ArrayList<String>();
166 for (User user : users.list()) {
167 res.add(user.getName());
173 public List<String> getUsers(String aGroup) {
174 Group group = requireGroup(aGroup, "While finding users for group");
175 List<String> res = new ArrayList<String>();
176 for (User user : users.list(group)) {
177 res.add(user.getName());
183 public List<String> getGroups() {
184 List<String> res = new ArrayList<String>();
185 for (Group group : groups.list()) {
186 res.add(group.getName());
192 public List<String> getGroups(String aUser) {
193 User user = requireUser(aUser, "While getting list of groups");
194 List<String> res = new ArrayList<String>();
195 for (Group g : user.getGroups()) {
196 res.add(g.getName());
202 public void renameUser(String aOldUsername, String aNewUsername) {
203 User user = requireUser(aOldUsername, "While renaming user");
205 if (aOldUsername.equals(aNewUsername)) {
206 return; // nothing to do.
209 if (users.find(aNewUsername) != null) {
210 throw new UserMgtException(DUPLICATE_USER, aNewUsername);
213 if (!userValidator.validate(aNewUsername)) {
214 throw new UserMgtException(Reason.INVALID_USERNAME, aNewUsername);
217 user.setName(aNewUsername);
218 users.userModified(user);
222 public void renameGroup(String aOldGroup, String aNewGroup) {
223 Group group = requireGroup(aOldGroup, "While renaming group");
225 if (aOldGroup.equals(aNewGroup)) {
229 if (groups.find(aNewGroup) != null) {
230 throw new UserMgtException(DUPLICATE_GROUP, aNewGroup);
233 if (!groupValidator.validate(aNewGroup)) {
234 throw new UserMgtException(Reason.INVALID_GROUPNAME, aNewGroup);
237 group.setName(aNewGroup);
238 groups.groupModified(group);
239 // Because a group has changed, a cached user could contain reference to an old copy of the
240 // group. Therefore, we clear the cache in this case.
245 public void removeUser(String aUser) {
246 User user = requireUser(aUser, "While removing user");
247 boolean removed = users.remove(user);
249 throw new UserMgtException(Reason.UNKNOWN_USER, aUser);
254 public void removeGroup(String aGroup) {
255 Group group = requireGroup(aGroup, "While removing group");
257 if (getUsers(aGroup).size() > 0) {
258 throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
261 boolean removed = groups.remove(group);
263 throw new UserMgtException(Reason.UNKNOWN_GROUP, aGroup);
268 public void addUserToGroup(String aUser, String aGroup) {
269 User user = requireUser(aUser, "While adding user to group");
270 Group group = requireGroup(aGroup, "While adding user to group");
271 user.addGroup(group);
272 users.userModified(user);
276 public void removeUserFromGroup(String aUser, String aGroup) {
277 User user = requireUser(aUser, "While removing user from group");
278 Group group = requireGroup(aGroup, "While removing user from group");
279 user.removeGroup(group);
280 users.userModified(user);