--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.security.authentication;
+
+import static org.wamblee.security.authentication.UserMgtException.Reason.*;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Administration of users and groups.
+ *
+ * @author Erik Brakkee
+ */
+public class UserAdministrationImpl implements UserAdministration {
+ /**
+ * All known users.
+ */
+ private UserSet users;
+
+ /**
+ * All known groups.
+ */
+ private GroupSet groups;
+
+ /**
+ * Validator for user names.
+ */
+ private NameValidator userValidator;
+
+ /**
+ * Validator for group names.
+ */
+ private NameValidator groupValidator;
+
+ /**
+ * Constructs empty user administration.
+ *
+ */
+ public UserAdministrationImpl(UserSet aUsers, GroupSet aGroups,
+ NameValidator aUserValidator, NameValidator aGroupValidator) {
+ users = aUsers;
+ groups = aGroups;
+ userValidator = aUserValidator;
+ groupValidator = aGroupValidator;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserAdministration#createUser(java.lang.String,
+ * java.lang.String)
+ */
+ public User createUser(String aUser, String aPassword, Group aGroup)
+ throws UserMgtException {
+ userValidator.validate(aUser);
+ checkGroup(aGroup);
+
+ User user = users.createUser(aUser, aPassword, aGroup);
+
+ return new User(user);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserAdministration#createGroup(java.lang.String)
+ */
+ public Group createGroup(String aName) throws UserMgtException {
+ groupValidator.validate(aName);
+
+ Group group = new Group(aName);
+
+ if (groups.contains(group)) {
+ throw new UserMgtException(DUPLICATE_GROUP, group);
+ }
+
+ groups.add(group);
+
+ return new Group(group);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.UserAdministration#userModified(org.wamblee.usermgt
+ * .User)
+ */
+ public void userModified(User aUser) {
+ users.userModified(aUser);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.UserAdministration#groupModified(org.wamblee.usermgt
+ * .Group)
+ */
+ public void groupModified(Group aGroup) {
+ groups.groupModified(aGroup);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
+ */
+ public User getUser(String aName) {
+ User user = users.find(aName);
+ if (user == null) {
+ return user;
+ }
+ return new User(user);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
+ */
+ public Group getGroup(String aName) {
+ Group group = groups.find(aName);
+ if ( group == null ) {
+ return group;
+ }
+ return new Group(group);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserAdministration#getUsers()
+ */
+ public Set<User> getUsers() {
+ Set<User> res = new HashSet<User>();
+ for (User user: users.list()) {
+ res.add(new User(user));
+ }
+ return res;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.UserAdministration#getUsers(org.wamblee.usermgt.Group
+ * )
+ */
+ public Set<User> getUsers(Group aGroup) {
+ Set<User> res = new HashSet<User>();
+ for (User user: users.list(aGroup)) {
+ res.add(new User(user));
+ }
+ return res;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserAdministration#getGroups()
+ */
+ public Set<Group> getGroups() {
+ Set<Group> res = new HashSet<Group>();
+ for (Group group: groups.list()) {
+ res.add(new Group(group));
+ }
+ return res;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.UserAdministration#removeUser(org.wamblee.usermgt
+ * .User)
+ */
+ public void removeUser(User aUser) throws UserMgtException {
+ checkUser(aUser);
+ users.remove(aUser);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.UserAdministration#removeGroup(org.wamblee.usermgt
+ * .Group)
+ */
+ public void removeGroup(Group aGroup) throws UserMgtException {
+ checkGroup(aGroup);
+
+ if (getUsers(aGroup).size() > 0) {
+ throw new UserMgtException(GROUP_STILL_OCCUPIED, aGroup);
+ }
+
+ groups.remove(aGroup);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.UserAdministration#renameUser(org.wamblee.usermgt
+ * .User, java.lang.String)
+ */
+ public void renameUser(User aUser, String aUserName)
+ throws UserMgtException {
+ checkUser(aUser);
+
+ if (aUser.getName().equals(aUserName)) {
+ throw new UserMgtException(TRIVIAL_RENAME, aUser);
+ }
+
+ if (users.find(aUserName) != null) {
+ throw new UserMgtException(DUPLICATE_USER, aUser);
+ }
+
+ userValidator.validate(aUserName);
+
+ aUser.setName(aUserName);
+ users.userModified(aUser);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.UserAdministration#renameGroup(org.wamblee.usermgt
+ * .Group, java.lang.String)
+ */
+ public void renameGroup(Group aGroup, String aGroupName)
+ throws UserMgtException {
+ checkGroup(aGroup);
+
+ if (aGroup.getName().equals(aGroupName)) {
+ throw new UserMgtException(TRIVIAL_RENAME, aGroup);
+ }
+
+ if (groups.find(aGroupName) != null) {
+ throw new UserMgtException(DUPLICATE_GROUP, aGroup);
+ }
+
+ groupValidator.validate(aGroupName);
+
+ aGroup.setName(aGroupName);
+ groups.groupModified(aGroup);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.UserAdministration#addUserToGroup(org.wamblee.usermgt
+ * .User, org.wamblee.usermgt.Group)
+ */
+ public void addUserToGroup(User aUser, Group aGroup)
+ throws UserMgtException {
+ checkUser(aUser);
+ checkGroup(aGroup);
+ aUser.addGroup(aGroup);
+ users.userModified(aUser);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.UserAdministration#removeUserFromGroup(org.wamblee
+ * .usermgt.User, org.wamblee.usermgt.Group)
+ */
+ public void removeUserFromGroup(User aUser, Group aGroup)
+ throws UserMgtException {
+ checkUser(aUser);
+ checkGroup(aGroup);
+ aUser.removeGroup(aGroup);
+ users.userModified(aUser);
+ }
+
+ /**
+ *
+ * @param aUser
+ *
+ * @throws UserMgtException
+ */
+ private void checkUser(User aUser) throws UserMgtException {
+ if (!users.contains(aUser)) {
+ throw new UserMgtException(UNKNOWN_USER, aUser);
+ }
+ }
+
+ /**
+ *
+ * @param aGroup
+ *
+ * @throws UserMgtException
+ */
+ private void checkGroup(Group aGroup) throws UserMgtException {
+ if (!groups.contains(aGroup)) {
+ throw new UserMgtException(UNKNOWN_GROUP, aGroup);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserAdministration#getUserCount()
+ */
+ public int getUserCount() {
+ return users.size();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserAdministration#getGroupCount()
+ */
+ public int getGroupCount() {
+ return groups.size();
+ }
+}