2 * Copyright 2005 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;
31 * Represents a user. The methods for managing the groups of the user have
32 * package scope. Managing the groups of the user should be done through the
33 * {@link org.wamblee.usermgt.UserAdministration} interface.
35 public class User extends AbstractPersistent implements Serializable,
45 private String password;
48 * Groups the user belongs to.
50 private Set<Group> groups;
55 private NameValidator passwordValidator;
60 private MessageDigester passwordEncoder;
63 * Constructs the user.
64 * @param aName User name.
65 * @param aPassword Password.
66 * @param aGroup Group the user belongs to.
68 User(String aName, String aPassword, Group aGroup,
69 NameValidator aPasswordValidator, MessageDigester aPasswordEncoder)
70 throws UserMgtException {
73 aPasswordValidator.validate(aPassword);
74 password = aPasswordEncoder.hash(aPassword);
75 groups = new TreeSet<Group>();
77 passwordValidator = aPasswordValidator;
78 passwordEncoder = aPasswordEncoder;
82 * Creates a new User object.
84 * @param aUser DOCUMENT ME!
86 public User(User aUser) {
89 password = aUser.password;
90 groups = new TreeSet<Group>();
92 for (Group group : aUser.groups) {
93 groups.add(new Group(group));
96 passwordValidator = aUser.passwordValidator;
97 passwordEncoder = aUser.passwordEncoder;
101 * Creates a new User object.
108 passwordValidator = null;
109 passwordEncoder = null;
113 * Sets the password validator.
115 * @param aPasswordValidator Validator.
117 public void setPasswordValidator(NameValidator aPasswordValidator) {
118 passwordValidator = aPasswordValidator;
122 * Sets the password encoder.
124 * @param aPasswordEncoder Encoder.
126 public void setPasswordEncoder(MessageDigester aPasswordEncoder) {
127 passwordEncoder = aPasswordEncoder;
133 * @return Returns the password.
135 String getPassword() {
140 * Checks the password.
142 * @param aPassword Password to check.
144 * @throws UserMgtException In case the password is incorrect.
146 public void checkPassword(String aPassword) throws UserMgtException {
147 String encoded = passwordEncoder.hash(aPassword);
149 if (!password.equals(encoded)) {
150 throw new UserMgtException(Reason.INVALID_PASSWORD, this);
155 * Changes the password.
157 * @param aOldPassword Old password.
158 * @param aNewPassword New password.
160 * @throws UserMgtException In case the old password is incorrect.
162 public void changePassword(String aOldPassword, String aNewPassword)
163 throws UserMgtException {
164 checkPassword(aOldPassword);
165 passwordValidator.validate(aNewPassword);
166 setPassword(aNewPassword);
172 * @param aPassword The password to set.
174 * @throws UserMgtException DOCUMENT ME!
176 public void setPassword(String aPassword) throws UserMgtException {
177 passwordValidator.validate(aPassword);
178 password = passwordEncoder.hash(aPassword);
186 protected String getPasswordString() {
193 * @param aPassword Password.
195 protected void setPasswordString(String aPassword) {
196 password = aPassword;
202 * @return Returns the _user.
204 public String getName() {
211 * @param aName The username to set.
213 void setName(String aName) {
218 * Gets the groups the user belongs to.
222 public Set<Group> getGroups() {
223 Set<Group> result = new TreeSet<Group>();
224 result.addAll(groups);
230 * Checks whether the user belongs to the given group.
232 * @param aGroup Group.
234 * @return True if the user belongs to the group.
236 public boolean isInGroup(Group aGroup) {
237 return groups.contains(aGroup);
241 * Checks whether the user belongs to the given group.
243 * @param aGroup Group.
245 * @return True if the user belongs to the group.
247 public boolean isInGroup(String aGroup) {
248 return groups.contains(new Group(aGroup));
254 * @return DOCUMENT ME!
259 * @return DOCUMENT ME!
262 * 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.
272 * @param aGroups Groups.
274 void setGroupSet(Set<Group> aGroups) {
279 * Adds the user to a group.
281 * @param aGroup Group to add the user to.
283 * @throws UserMgtException In case the user already belongs to the group.
285 void addGroup(Group aGroup) throws UserMgtException {
286 if (groups.contains(aGroup)) {
287 throw new UserMgtException(Reason.USER_ALREADY_IN_GROUP, aGroup);
294 * Removes the user from a group.
296 * @param aGroup Group.
298 * @throws UserMgtException In case the user does not belong to the group.
300 void removeGroup(Group aGroup) throws UserMgtException {
301 if (!groups.contains(aGroup)) {
302 throw new UserMgtException(Reason.USER_NOT_IN_GROUP, this, aGroup);
305 if (groups.size() == 1) {
306 throw new UserMgtException(Reason.USER_MUST_BE_IN_A_GROUP, this,
310 groups.remove(aGroup);
314 * @see java.lang.Object#equals(java.lang.Object)
319 * @param aUser DOCUMENT ME!
321 * @return DOCUMENT ME!
324 public boolean equals(Object aUser) {
325 if (!(aUser instanceof User)) {
329 User user = (User) aUser;
331 return name.equals(user.name);
335 * @see java.lang.Object#hashCode()
340 * @return DOCUMENT ME!
343 public int hashCode() {
344 return name.hashCode();
348 * @see java.lang.Object#toString()
353 * @return DOCUMENT ME!
356 public String toString() {
357 String result = "User(name=" + name + ", password=" + password;
359 for (Group group : groups) {
360 result += (", group=" + group);
367 * @see java.lang.Comparable#compareTo(T)
372 * @param aUser DOCUMENT ME!
374 * @return DOCUMENT ME!
376 public int compareTo(Object aUser) {
377 return name.compareTo(((User) aUser).name);