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 org.wamblee.persistence.AbstractPersistent;
20 import org.wamblee.security.encryption.MessageDigester;
22 import org.wamblee.usermgt.UserMgtException.Reason;
24 import java.io.Serializable;
27 import java.util.TreeSet;
30 * Represents a user. The methods for managing the groups of the user have
31 * package scope. Managing the groups of the user should be done through the
32 * {@link org.wamblee.usermgt.UserAdministration} interface.
34 public class User extends AbstractPersistent implements Serializable,
44 private String password;
47 * Groups the user belongs to.
49 private Set<Group> groups;
54 private NameValidator passwordValidator;
59 private MessageDigester passwordEncoder;
62 * Constructs the user.
69 * Group the user belongs to.
71 User(String aName, String aPassword, Group aGroup,
72 NameValidator aPasswordValidator, MessageDigester aPasswordEncoder)
73 throws UserMgtException {
76 aPasswordValidator.validate(aPassword);
77 password = aPasswordEncoder.hash(aPassword);
78 groups = new TreeSet<Group>();
80 passwordValidator = aPasswordValidator;
81 passwordEncoder = aPasswordEncoder;
85 * Creates a new User object.
88 public User(User aUser) {
91 password = aUser.password;
92 groups = new TreeSet<Group>();
94 for (Group group : aUser.groups) {
95 groups.add(new Group(group));
98 passwordValidator = aUser.passwordValidator;
99 passwordEncoder = aUser.passwordEncoder;
103 * Creates a new User object.
110 passwordValidator = null;
111 passwordEncoder = null;
115 * Sets the password validator.
117 * @param aPasswordValidator
120 public void setPasswordValidator(NameValidator aPasswordValidator) {
121 passwordValidator = aPasswordValidator;
125 * Sets the password encoder.
127 * @param aPasswordEncoder
130 public void setPasswordEncoder(MessageDigester aPasswordEncoder) {
131 passwordEncoder = aPasswordEncoder;
136 * @return Returns the password.
138 String getPassword() {
143 * Checks the password.
148 * @throws UserMgtException
149 * In case the password is incorrect.
151 public void checkPassword(String aPassword) throws UserMgtException {
152 String encoded = passwordEncoder.hash(aPassword);
154 if (!password.equals(encoded)) {
155 throw new UserMgtException(Reason.INVALID_PASSWORD, this);
160 * Changes the password.
162 * @param aOldPassword
164 * @param aNewPassword
167 * @throws UserMgtException
168 * In case the old password is incorrect.
170 public void changePassword(String aOldPassword, String aNewPassword)
171 throws UserMgtException {
172 checkPassword(aOldPassword);
173 passwordValidator.validate(aNewPassword);
174 setPassword(aNewPassword);
180 * The password to set.
183 public void setPassword(String aPassword) throws UserMgtException {
184 passwordValidator.validate(aPassword);
185 password = passwordEncoder.hash(aPassword);
193 protected String getPasswordString() {
203 protected void setPasswordString(String aPassword) {
204 password = aPassword;
209 * @return Returns the _user.
211 public String getName() {
218 * The username to set.
220 void setName(String aName) {
225 * Gets the groups the user belongs to.
229 public Set<Group> getGroups() {
230 Set<Group> result = new TreeSet<Group>();
231 result.addAll(groups);
237 * Checks whether the user belongs to the given group.
242 * @return True if the user belongs to the group.
244 public boolean isInGroup(Group aGroup) {
245 return groups.contains(aGroup);
249 * Checks whether the user belongs to the given group.
254 * @return True if the user belongs to the group.
256 public boolean isInGroup(String aGroup) {
257 return groups.contains(new Group(aGroup));
261 * Gets the group set. For OR mapping.
263 * @return set of groups.
265 Set<Group> getGroupSet() {
270 * Sets the groups the user belongs to, for OR mapping.
275 void setGroupSet(Set<Group> aGroups) {
280 * Adds the user to a group.
283 * Group to add the user to.
285 * @throws UserMgtException
286 * In case the user already belongs to the group.
288 void addGroup(Group aGroup) throws UserMgtException {
289 if (groups.contains(aGroup)) {
290 throw new UserMgtException(Reason.USER_ALREADY_IN_GROUP, aGroup);
297 * Removes the user from a group.
302 * @throws UserMgtException
303 * In case the user does not belong to the group.
305 void removeGroup(Group aGroup) throws UserMgtException {
306 if (!groups.contains(aGroup)) {
307 throw new UserMgtException(Reason.USER_NOT_IN_GROUP, this, aGroup);
310 if (groups.size() == 1) {
311 throw new UserMgtException(Reason.USER_MUST_BE_IN_A_GROUP, this,
315 groups.remove(aGroup);
321 * @see java.lang.Object#equals(java.lang.Object)
324 public boolean equals(Object aUser) {
325 if (!(aUser instanceof User)) {
329 User user = (User) aUser;
331 return name.equals(user.name);
337 * @see java.lang.Object#hashCode()
340 public int hashCode() {
341 return name.hashCode();
347 * @see java.lang.Object#toString()
350 public String toString() {
351 String result = "User(name=" + name + ", password=" + password;
353 for (Group group : groups) {
354 result += (", group=" + group);
363 * @see java.lang.Comparable#compareTo(T)
365 public int compareTo(Object aUser) {
366 return name.compareTo(((User) aUser).name);