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.
17 package org.wamblee.usermgt;
19 import java.io.Serializable;
21 import java.util.TreeSet;
23 import org.wamblee.persistence.AbstractPersistent;
24 import org.wamblee.security.encryption.MessageDigester;
25 import org.wamblee.usermgt.UserMgtException.Reason;
29 * The methods for managing the groups of the user have package scope.
30 * Managing the groups of the user should be done through the
31 * {@link org.wamblee.usermgt.UserAdministration} interface.
33 public class User extends AbstractPersistent implements Serializable, Comparable {
43 private String _password;
46 * Groups the user belongs to.
48 private Set<Group> _groups;
53 private NameValidator _passwordValidator;
58 private MessageDigester _passwordEncoder;
61 * Constructs the user.
62 * @param aName User name.
63 * @param aPassword Password.
64 * @param aGroup Group the user belongs to.
66 User(String aName, String aPassword, Group aGroup, NameValidator aPasswordValidator,
67 MessageDigester aPasswordEncoder) throws UserMgtException {
70 aPasswordValidator.validate(aPassword);
71 _password = aPasswordEncoder.hash(aPassword);
72 _groups = new TreeSet<Group>();
74 _passwordValidator = aPasswordValidator;
75 _passwordEncoder = aPasswordEncoder;
78 public User(User aUser) {
81 _password = aUser._password;
82 _groups = new TreeSet<Group>();
83 for (Group group: aUser._groups) {
84 _groups.add(new Group(group));
86 _passwordValidator = aUser._passwordValidator;
87 _passwordEncoder = aUser._passwordEncoder;
95 _passwordValidator = null;
96 _passwordEncoder = null;
100 * Sets the password validator.
101 * @param aPasswordValidator Validator.
103 public void setPasswordValidator(NameValidator aPasswordValidator) {
104 _passwordValidator = aPasswordValidator;
108 * Sets the password encoder.
109 * @param aPasswordEncoder Encoder.
111 public void setPasswordEncoder(MessageDigester aPasswordEncoder) {
112 _passwordEncoder = aPasswordEncoder;
116 * @return Returns the _password.
118 String getPassword() {
123 * Checks the password.
124 * @param aPassword Password to check.
125 * @throws UserMgtException In case the password is incorrect.
127 public void checkPassword(String aPassword) throws UserMgtException {
128 String encoded = _passwordEncoder.hash(aPassword);
129 if ( !_password.equals(encoded) ) {
130 throw new UserMgtException(Reason.INVALID_PASSWORD, this);
135 * Changes the password.
136 * @param aOldPassword Old password.
137 * @param aNewPassword New password.
138 * @throws UserMgtException In case the old password is incorrect.
140 public void changePassword(String aOldPassword, String aNewPassword) throws UserMgtException {
141 checkPassword(aOldPassword);
142 _passwordValidator.validate(aNewPassword);
143 setPassword(aNewPassword);
148 * The password to set.
150 public void setPassword(String aPassword) throws UserMgtException {
151 _passwordValidator.validate(aPassword);
152 _password = _passwordEncoder.hash(aPassword);
159 protected String getPasswordString() {
165 * @param aPassword Password.
167 protected void setPasswordString(String aPassword) {
168 _password = aPassword;
172 * @return Returns the _user.
174 public String getName() {
180 * The username to set.
182 void setName(String aName) {
187 * Gets the groups the user belongs to.
190 public Set<Group> getGroups() {
191 Set<Group> result = new TreeSet<Group>();
192 result.addAll(_groups);
197 * Checks whether the user belongs to the given group.
198 * @param aGroup Group.
199 * @return True if the user belongs to the group.
201 public boolean isInGroup(Group aGroup) {
202 return _groups.contains(aGroup);
206 * Checks whether the user belongs to the given group.
207 * @param aGroup Group.
208 * @return True if the user belongs to the group.
210 public boolean isInGroup(String aGroup) {
211 return _groups.contains(new Group(aGroup));
215 * Gets the group set. For OR mapping.
216 * @return set of groups.
218 Set<Group> getGroupSet() {
223 * Sets the groups the user belongs to, for OR mapping.
224 * @param aGroups Groups.
226 void setGroupSet(Set<Group> aGroups) {
231 * Adds the user to a group.
232 * @param aGroup Group to add the user to.
233 * @throws UserMgtException In case the user already belongs to the group.
235 void addGroup(Group aGroup) throws UserMgtException {
236 if (_groups.contains(aGroup)) {
237 throw new UserMgtException(Reason.USER_ALREADY_IN_GROUP, aGroup);
243 * Removes the user from a group.
244 * @param aGroup Group.
245 * @throws UserMgtException In case the user does not belong to the group.
247 void removeGroup(Group aGroup) throws UserMgtException {
248 if (!_groups.contains(aGroup)) {
249 throw new UserMgtException(Reason.USER_NOT_IN_GROUP, this, aGroup);
251 if ( _groups.size() == 1 ) {
252 throw new UserMgtException(Reason.USER_MUST_BE_IN_A_GROUP, this, aGroup);
254 _groups.remove(aGroup);
258 * @see java.lang.Object#equals(java.lang.Object)
261 public boolean equals(Object aUser) {
262 if ( !(aUser instanceof User)) {
265 User user = (User)aUser;
266 return _name.equals(user._name);
270 * @see java.lang.Object#hashCode()
273 public int hashCode() {
274 return _name.hashCode();
278 * @see java.lang.Object#toString()
281 public String toString() {
282 String result = "User(name=" + _name + ", password=" + _password;
283 for (Group group: _groups) {
284 result += ", group=" + group;
290 * @see java.lang.Comparable#compareTo(T)
292 public int compareTo(Object aUser) {
293 return _name.compareTo(((User)aUser)._name);