0a69cef9b27cd21cf3b72aa3d17f2ed056b09168
[utils] / security / src / main / java / org / wamblee / usermgt / User.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16
17 package org.wamblee.usermgt;
18
19 import java.io.Serializable;
20 import java.util.Set;
21 import java.util.TreeSet;
22
23 import org.wamblee.persistence.AbstractPersistent;
24 import org.wamblee.security.encryption.MessageDigester;
25 import org.wamblee.usermgt.UserMgtException.Reason;
26
27 /**
28  * Represents a user.
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. 
32  */
33 public class User extends AbstractPersistent implements Serializable, Comparable {
34
35     /**
36      * User name. 
37      */
38     private String name;
39
40     /**
41      * Password. 
42      */
43     private String password;
44
45     /**
46      * Groups the user belongs to. 
47      */
48     private Set<Group> groups;
49     
50     /**
51      * Password validator. 
52      */
53     private NameValidator passwordValidator; 
54
55     /**
56      * Password encoder. 
57      */
58     private MessageDigester passwordEncoder;
59     
60     /**
61      * Constructs the user. 
62      * @param aName User name. 
63      * @param aPassword Password. 
64      * @param aGroup Group the user belongs to.  
65      */
66     User(String aName, String aPassword, Group aGroup, NameValidator aPasswordValidator, 
67             MessageDigester aPasswordEncoder) throws UserMgtException {
68         super(); 
69         name = aName;
70         aPasswordValidator.validate(aPassword);
71         password = aPasswordEncoder.hash(aPassword);
72         groups = new TreeSet<Group>(); 
73         groups.add(aGroup);
74         passwordValidator = aPasswordValidator;
75         passwordEncoder = aPasswordEncoder;
76     }
77     
78     public User(User aUser) { 
79         super(aUser); 
80         name = aUser.name;
81         password = aUser.password;
82         groups = new TreeSet<Group>(); 
83         for (Group group: aUser.groups) {
84             groups.add(new Group(group)); 
85         }
86         passwordValidator = aUser.passwordValidator; 
87         passwordEncoder = aUser.passwordEncoder;
88     }
89     
90     User() { 
91         super(); 
92         name = null; 
93         password = null; 
94         groups = null; 
95         passwordValidator = null; 
96         passwordEncoder = null;
97     }
98     
99     /**
100      * Sets the password validator. 
101      * @param aPasswordValidator Validator. 
102      */
103     public void setPasswordValidator(NameValidator aPasswordValidator) { 
104         passwordValidator = aPasswordValidator;
105     }
106     
107     /**
108      * Sets the password encoder. 
109      * @param aPasswordEncoder Encoder. 
110      */
111     public void setPasswordEncoder(MessageDigester aPasswordEncoder) { 
112         passwordEncoder = aPasswordEncoder; 
113     }
114
115     /**
116      * @return Returns the password.
117      */
118     String getPassword() {
119         return password;
120     }
121     
122     /**
123      * Checks the password. 
124      * @param aPassword Password to check. 
125      * @throws UserMgtException In case the password is incorrect. 
126      */
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);
131         }
132     }
133     
134     /**
135      * Changes the password. 
136      * @param aOldPassword Old password. 
137      * @param aNewPassword New password. 
138      * @throws UserMgtException In case the old password is incorrect. 
139      */
140     public void changePassword(String aOldPassword, String aNewPassword) throws UserMgtException {
141         checkPassword(aOldPassword);
142         passwordValidator.validate(aNewPassword);
143         setPassword(aNewPassword);
144     }
145
146     /**
147      * @param aPassword
148      *            The password to set.
149      */
150     public void setPassword(String aPassword) throws UserMgtException {
151         passwordValidator.validate(aPassword);
152         password = passwordEncoder.hash(aPassword);
153     }
154     
155     /**
156      * For OR mapping. 
157      * @return Password. 
158      */
159     protected String getPasswordString() { 
160         return password;
161     }
162     
163     /**
164      * For OR mapping. 
165      * @param aPassword Password. 
166      */
167     protected void setPasswordString(String aPassword) { 
168         password = aPassword; 
169     }
170
171     /**
172      * @return Returns the _user.
173      */
174     public String getName() {
175         return name;
176     }
177
178     /**
179      * @param aName
180      *            The username to set.
181      */
182     void setName(String aName) {
183         name = aName;
184     }
185
186     /**
187      * Gets the groups the user belongs to. 
188      * @return Groups.
189      */
190     public Set<Group> getGroups() {
191         Set<Group> result = new TreeSet<Group>(); 
192         result.addAll(groups);
193         return result; 
194     }
195     
196     /**
197      * Checks whether the user belongs to the given group. 
198      * @param aGroup Group. 
199      * @return True if the user belongs to the group.
200      */
201     public boolean isInGroup(Group aGroup) {
202         return groups.contains(aGroup);
203     }
204     
205     /**
206      * Checks whether the user belongs to the given group. 
207      * @param aGroup Group. 
208      * @return True if the user belongs to the group.
209      */
210     public boolean isInGroup(String aGroup) {
211         return groups.contains(new Group(aGroup));
212     }
213     
214     /**
215      * Gets the group set. For OR mapping. 
216      * @return set of groups. 
217      */
218     Set<Group> getGroupSet() { 
219         return groups;
220     }
221
222     /**
223      * Sets the groups the user belongs to, for OR mapping. 
224      * @param aGroups Groups.
225      */
226     void setGroupSet(Set<Group> aGroups) {
227         groups = aGroups;
228     }
229
230     /**
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.  
234      */
235     void addGroup(Group aGroup) throws UserMgtException {
236         if (groups.contains(aGroup)) {
237             throw new UserMgtException(Reason.USER_ALREADY_IN_GROUP, aGroup);
238         }
239         groups.add(aGroup);
240     }
241
242     /**
243      * Removes the user from a group. 
244      * @param aGroup Group. 
245      * @throws UserMgtException In case the user does not belong to the group.
246      */
247     void removeGroup(Group aGroup) throws UserMgtException {
248         if (!groups.contains(aGroup)) {
249             throw new UserMgtException(Reason.USER_NOT_IN_GROUP, this, aGroup);
250         }
251         if ( groups.size() == 1 ) { 
252             throw new UserMgtException(Reason.USER_MUST_BE_IN_A_GROUP, this, aGroup); 
253         }
254         groups.remove(aGroup);
255     }
256     
257     /* (non-Javadoc)
258      * @see java.lang.Object#equals(java.lang.Object)
259      */
260     @Override
261     public boolean equals(Object aUser) {
262         if ( !(aUser instanceof User)) {
263             return false; 
264         }
265         User user = (User)aUser; 
266         return name.equals(user.name);
267     }
268     
269     /* (non-Javadoc)
270      * @see java.lang.Object#hashCode()
271      */
272     @Override
273     public int hashCode() {
274         return name.hashCode(); 
275     }
276     
277     /* (non-Javadoc)
278      * @see java.lang.Object#toString()
279      */
280     @Override
281     public String toString() {
282         String result = "User(name=" + name + ", password=" + password;
283         for (Group group: groups) { 
284             result += ", group=" + group; 
285         }
286         return result + ")"; 
287     }
288     
289     /* (non-Javadoc)
290      * @see java.lang.Comparable#compareTo(T)
291      */
292     public int compareTo(Object aUser) {
293         return name.compareTo(((User)aUser).name);
294     }
295 }