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 java.io.Serializable;
20 import java.util.TreeSet;
22 import javax.persistence.CascadeType;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.FetchType;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.JoinTable;
31 import javax.persistence.ManyToMany;
32 import javax.persistence.NamedQueries;
33 import javax.persistence.NamedQuery;
34 import javax.persistence.Table;
35 import javax.persistence.Transient;
36 import javax.persistence.Version;
38 import org.wamblee.security.authentication.UserMgtException.Reason;
39 import org.wamblee.security.encryption.MessageDigester;
42 * Represents a user. The methods for managing the groups of the user have
43 * package scope. Managing the groups of the user should be done through the
44 * {@link org.wamblee.security.authentication.UserAdministration} interface.
47 @Table(name = "SEC_USER")
49 @NamedQuery(name = User.QUERY_FIND_BY_NAME, query = "select u from User u where u.name = :" +
51 @NamedQuery(name = User.QUERY_FIND_BY_GROUP_NAME, query = "select user from User user join user.groups grp where grp.name = :name"),
52 @NamedQuery(name = User.QUERY_COUNT_USERS, query = "select count(u) from User u"),
53 @NamedQuery(name = User.QUERY_ALL_USERS, query = "select u from User u") })
54 public class User implements Serializable, Comparable {
56 public static final String QUERY_FIND_BY_NAME = "User.findByName";
57 public static final String QUERY_FIND_BY_GROUP_NAME = "User.findByGroupName";
58 public static final String QUERY_COUNT_USERS = "User.count";
59 public static final String QUERY_ALL_USERS = "User.all";
60 public static final String NAME_PARAM = "name";
63 @GeneratedValue(strategy = GenerationType.AUTO)
77 private String password;
80 * Groups the user belongs to.
82 @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
83 @JoinTable(name = "SEC_USER_GROUP", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "GROUP_ID") })
84 private Set<Group> groups;
90 private NameValidator passwordValidator;
96 private MessageDigester passwordEncoder;
99 * Constructs the user.
106 User(String aName, String aPassword, NameValidator aPasswordValidator,
107 MessageDigester aPasswordEncoder) {
110 aPasswordValidator.validate(aPassword);
111 password = aPasswordEncoder.hash(aPassword);
112 groups = new TreeSet<Group>();
113 passwordValidator = aPasswordValidator;
114 passwordEncoder = aPasswordEncoder;
118 * Creates a new User object.
121 public User(User aUser) {
123 version = aUser.version;
125 password = aUser.password;
126 groups = new TreeSet<Group>();
128 for (Group group : aUser.groups) {
129 groups.add(new Group(group));
132 passwordValidator = aUser.passwordValidator;
133 passwordEncoder = aUser.passwordEncoder;
137 * Creates a new User object.
144 passwordValidator = null;
145 passwordEncoder = null;
149 * Sets the password validator.
151 * @param aPasswordValidator
154 public void setPasswordValidator(NameValidator aPasswordValidator) {
155 passwordValidator = aPasswordValidator;
159 * Sets the password encoder.
161 * @param aPasswordEncoder
164 public void setPasswordEncoder(MessageDigester aPasswordEncoder) {
165 passwordEncoder = aPasswordEncoder;
170 * @return Returns the password.
172 String getPassword() {
177 * Checks the password.
182 * @return True iff the password is correct.
184 public boolean checkPassword(String aPassword) {
185 String encoded = passwordEncoder.hash(aPassword);
187 return password.equals(encoded);
191 * Changes the password.
193 * @param aOldPassword
195 * @param aNewPassword
198 * @return True iff the password was changed successfully.
200 public boolean changePassword(String aOldPassword, String aNewPassword) {
201 if (!checkPassword(aOldPassword)) {
204 passwordValidator.validate(aNewPassword);
205 setPassword(aNewPassword);
212 * The password to set.
215 public void setPassword(String aPassword) {
216 passwordValidator.validate(aPassword);
217 password = passwordEncoder.hash(aPassword);
225 protected String getPasswordString() {
235 protected void setPasswordString(String aPassword) {
236 password = aPassword;
241 * @return Returns the _user.
243 public String getName() {
250 * The username to set.
252 void setName(String aName) {
257 * Gets the groups the user belongs to.
261 public Set<Group> getGroups() {
262 Set<Group> result = new TreeSet<Group>();
263 result.addAll(groups);
269 * Checks whether the user belongs to the given group.
274 * @return True if the user belongs to the group.
276 public boolean isInGroup(Group aGroup) {
277 return groups.contains(aGroup);
281 * Checks whether the user belongs to the given group.
286 * @return True if the user belongs to the group.
288 public boolean isInGroup(String aGroup) {
289 return groups.contains(new Group(aGroup));
293 * Gets the group set. For OR mapping.
295 * @return set of groups.
297 Set<Group> getGroupSet() {
302 * Sets the groups the user belongs to, for OR mapping.
307 void setGroupSet(Set<Group> aGroups) {
312 * Adds the user to a group.
315 * Group to add the user to.
317 * @throws UserMgtException
318 * In case the user already belongs to the group.
320 void addGroup(Group aGroup) throws UserMgtException {
321 if (groups.contains(aGroup)) {
322 throw new UserMgtException(Reason.USER_ALREADY_IN_GROUP, aGroup);
329 * Removes the user from a group.
334 * @throws UserMgtException
335 * In case the user does not belong to the group.
337 void removeGroup(Group aGroup) {
338 if (!groups.contains(aGroup)) {
339 throw new UserMgtException(Reason.USER_NOT_IN_GROUP, this, aGroup);
342 groups.remove(aGroup);
348 * @see java.lang.Object#equals(java.lang.Object)
351 public boolean equals(Object aUser) {
355 if (!(aUser instanceof User)) {
359 User user = (User) aUser;
361 return name.equals(user.name);
367 * @see java.lang.Object#hashCode()
370 public int hashCode() {
371 return name.hashCode();
377 * @see java.lang.Object#toString()
380 public String toString() {
381 String result = "User(name=" + name + ", password=" + password;
383 for (Group group : groups) {
384 result += (", group=" + group);
393 * @see java.lang.Comparable#compareTo(T)
395 public int compareTo(Object aUser) {
396 return name.compareTo(((User) aUser).name);
399 public Long getPrimaryKey() {