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.Entity;
24 import javax.persistence.FetchType;
25 import javax.persistence.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.JoinColumn;
29 import javax.persistence.JoinTable;
30 import javax.persistence.ManyToMany;
31 import javax.persistence.NamedQueries;
32 import javax.persistence.NamedQuery;
33 import javax.persistence.Table;
34 import javax.persistence.Transient;
35 import javax.persistence.Version;
37 import org.wamblee.security.authentication.UserMgtException.Reason;
40 * Represents a user. The methods for managing the groups of the user have
41 * package scope. Managing the groups of the user should be done through the
42 * {@link org.wamblee.security.authentication.UserAdministration} interface.
45 @Table(name = "SEC_USER")
47 @NamedQuery(name = User.QUERY_FIND_BY_NAME, query = "select u from User u where u.name = :" +
49 @NamedQuery(name = User.QUERY_FIND_BY_GROUP_NAME, query = "select user from User user join user.groups grp where grp.name = :name"),
50 @NamedQuery(name = User.QUERY_COUNT_USERS, query = "select count(u) from User u"),
51 @NamedQuery(name = User.QUERY_ALL_USERS, query = "select u from User u") })
52 public class User implements Serializable, Comparable {
54 public static final String QUERY_FIND_BY_NAME = "User.findByName";
55 public static final String QUERY_FIND_BY_GROUP_NAME = "User.findByGroupName";
56 public static final String QUERY_COUNT_USERS = "User.count";
57 public static final String QUERY_ALL_USERS = "User.all";
58 public static final String NAME_PARAM = "name";
61 @GeneratedValue(strategy = GenerationType.AUTO)
75 private String password;
78 * Groups the user belongs to.
80 @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
81 @JoinTable(name = "SEC_USER_GROUP", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "GROUP_ID") })
82 private Set<Group> groups;
88 private NameValidator passwordValidator;
94 private MessageDigester passwordEncoder;
97 * Constructs the user.
104 User(String aName, String aPassword, NameValidator aPasswordValidator,
105 MessageDigester aPasswordEncoder) {
108 aPasswordValidator.validate(aPassword);
109 password = aPasswordEncoder.hash(aPassword);
110 groups = new TreeSet<Group>();
111 passwordValidator = aPasswordValidator;
112 passwordEncoder = aPasswordEncoder;
116 * Creates a new User object.
119 public User(User aUser) {
121 version = aUser.version;
123 password = aUser.password;
124 groups = new TreeSet<Group>();
126 for (Group group : aUser.groups) {
127 groups.add(new Group(group));
130 passwordValidator = aUser.passwordValidator;
131 passwordEncoder = aUser.passwordEncoder;
135 * Creates a new User object.
142 passwordValidator = null;
143 passwordEncoder = null;
147 * Sets the password validator.
149 * @param aPasswordValidator
152 public void setPasswordValidator(NameValidator aPasswordValidator) {
153 passwordValidator = aPasswordValidator;
157 * Sets the password encoder.
159 * @param aPasswordEncoder
162 public void setPasswordEncoder(MessageDigester aPasswordEncoder) {
163 passwordEncoder = aPasswordEncoder;
168 * @return Returns the password.
170 String getPassword() {
175 * Checks the password.
180 * @return True iff the password is correct.
182 public boolean checkPassword(String aPassword) {
183 String encoded = passwordEncoder.hash(aPassword);
185 return password.equals(encoded);
189 * Changes the password.
191 * @param aOldPassword
193 * @param aNewPassword
196 * @return True iff the password was changed successfully.
198 public boolean changePassword(String aOldPassword, String aNewPassword) {
199 if (!checkPassword(aOldPassword)) {
202 passwordValidator.validate(aNewPassword);
203 setPassword(aNewPassword);
210 * The password to set.
213 public void setPassword(String aPassword) {
214 passwordValidator.validate(aPassword);
215 password = passwordEncoder.hash(aPassword);
223 protected String getPasswordString() {
233 protected void setPasswordString(String aPassword) {
234 password = aPassword;
239 * @return Returns the _user.
241 public String getName() {
248 * The username to set.
250 void setName(String aName) {
255 * Gets the groups the user belongs to.
259 public Set<Group> getGroups() {
260 Set<Group> result = new TreeSet<Group>();
261 result.addAll(groups);
267 * Checks whether the user belongs to the given group.
272 * @return True if the user belongs to the group.
274 public boolean isInGroup(Group aGroup) {
275 return groups.contains(aGroup);
279 * Checks whether the user belongs to the given group.
284 * @return True if the user belongs to the group.
286 public boolean isInGroup(String aGroup) {
287 return groups.contains(new Group(aGroup));
291 * Gets the group set. For OR mapping.
293 * @return set of groups.
295 Set<Group> getGroupSet() {
300 * Sets the groups the user belongs to, for OR mapping.
305 void setGroupSet(Set<Group> aGroups) {
310 * Adds the user to a group.
313 * Group to add the user to.
315 * @throws UserMgtException
316 * In case the user already belongs to the group.
318 void addGroup(Group aGroup) throws UserMgtException {
319 if (groups.contains(aGroup)) {
320 throw new UserMgtException(Reason.USER_ALREADY_IN_GROUP, aGroup);
327 * Removes the user from a group.
332 * @throws UserMgtException
333 * In case the user does not belong to the group.
335 void removeGroup(Group aGroup) {
336 if (!groups.contains(aGroup)) {
337 throw new UserMgtException(Reason.USER_NOT_IN_GROUP, this, aGroup);
340 groups.remove(aGroup);
346 * @see java.lang.Object#equals(java.lang.Object)
349 public boolean equals(Object aUser) {
353 if (!(aUser instanceof User)) {
357 User user = (User) aUser;
359 return name.equals(user.name);
365 * @see java.lang.Object#hashCode()
368 public int hashCode() {
369 return name.hashCode();
375 * @see java.lang.Object#toString()
378 public String toString() {
379 String result = "User(name=" + name + ", password=" + password;
381 for (Group group : groups) {
382 result += (", group=" + group);
391 * @see java.lang.Comparable#compareTo(T)
393 public int compareTo(Object aUser) {
394 return name.compareTo(((User) aUser).name);
397 public Long getPrimaryKey() {