Removed DOCUMENT ME comments that were generated and applied source code
[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 package org.wamblee.usermgt;
17
18 import org.wamblee.persistence.AbstractPersistent;
19
20 import org.wamblee.security.encryption.MessageDigester;
21
22 import org.wamblee.usermgt.UserMgtException.Reason;
23
24 import java.io.Serializable;
25
26 import java.util.Set;
27 import java.util.TreeSet;
28
29 /**
30  * Represents a user. The methods for managing the groups of the user have
31  * package scope. Managing the groups of the user should be done through the
32  * {@link org.wamblee.usermgt.UserAdministration} interface.
33  */
34 public class User extends AbstractPersistent implements Serializable,
35     Comparable {
36     /**
37      * User name.
38      */
39     private String name;
40
41     /**
42      * Password.
43      */
44     private String password;
45
46     /**
47      * Groups the user belongs to.
48      */
49     private Set<Group> groups;
50
51     /**
52      * Password validator.
53      */
54     private NameValidator passwordValidator;
55
56     /**
57      * Password encoder.
58      */
59     private MessageDigester passwordEncoder;
60
61     /**
62      * Constructs the user.
63      * 
64      * @param aName
65      *            User name.
66      * @param aPassword
67      *            Password.
68      * @param aGroup
69      *            Group the user belongs to.
70      */
71     User(String aName, String aPassword, Group aGroup,
72         NameValidator aPasswordValidator, MessageDigester aPasswordEncoder)
73         throws UserMgtException {
74         super();
75         name = aName;
76         aPasswordValidator.validate(aPassword);
77         password = aPasswordEncoder.hash(aPassword);
78         groups = new TreeSet<Group>();
79         groups.add(aGroup);
80         passwordValidator = aPasswordValidator;
81         passwordEncoder = aPasswordEncoder;
82     }
83
84     /**
85      * Creates a new User object.
86      * 
87      */
88     public User(User aUser) {
89         super(aUser);
90         name = aUser.name;
91         password = aUser.password;
92         groups = new TreeSet<Group>();
93
94         for (Group group : aUser.groups) {
95             groups.add(new Group(group));
96         }
97
98         passwordValidator = aUser.passwordValidator;
99         passwordEncoder = aUser.passwordEncoder;
100     }
101
102     /**
103      * Creates a new User object.
104      */
105     User() {
106         super();
107         name = null;
108         password = null;
109         groups = null;
110         passwordValidator = null;
111         passwordEncoder = null;
112     }
113
114     /**
115      * Sets the password validator.
116      * 
117      * @param aPasswordValidator
118      *            Validator.
119      */
120     public void setPasswordValidator(NameValidator aPasswordValidator) {
121         passwordValidator = aPasswordValidator;
122     }
123
124     /**
125      * Sets the password encoder.
126      * 
127      * @param aPasswordEncoder
128      *            Encoder.
129      */
130     public void setPasswordEncoder(MessageDigester aPasswordEncoder) {
131         passwordEncoder = aPasswordEncoder;
132     }
133
134     /**
135      * 
136      * @return Returns the password.
137      */
138     String getPassword() {
139         return password;
140     }
141
142     /**
143      * Checks the password.
144      * 
145      * @param aPassword
146      *            Password to check.
147      * 
148      * @throws UserMgtException
149      *             In case the password is incorrect.
150      */
151     public void checkPassword(String aPassword) throws UserMgtException {
152         String encoded = passwordEncoder.hash(aPassword);
153
154         if (!password.equals(encoded)) {
155             throw new UserMgtException(Reason.INVALID_PASSWORD, this);
156         }
157     }
158
159     /**
160      * Changes the password.
161      * 
162      * @param aOldPassword
163      *            Old password.
164      * @param aNewPassword
165      *            New password.
166      * 
167      * @throws UserMgtException
168      *             In case the old password is incorrect.
169      */
170     public void changePassword(String aOldPassword, String aNewPassword)
171         throws UserMgtException {
172         checkPassword(aOldPassword);
173         passwordValidator.validate(aNewPassword);
174         setPassword(aNewPassword);
175     }
176
177     /**
178      * 
179      * @param aPassword
180      *            The password to set.
181      * 
182      */
183     public void setPassword(String aPassword) throws UserMgtException {
184         passwordValidator.validate(aPassword);
185         password = passwordEncoder.hash(aPassword);
186     }
187
188     /**
189      * For OR mapping.
190      * 
191      * @return Password.
192      */
193     protected String getPasswordString() {
194         return password;
195     }
196
197     /**
198      * For OR mapping.
199      * 
200      * @param aPassword
201      *            Password.
202      */
203     protected void setPasswordString(String aPassword) {
204         password = aPassword;
205     }
206
207     /**
208      * 
209      * @return Returns the _user.
210      */
211     public String getName() {
212         return name;
213     }
214
215     /**
216      * 
217      * @param aName
218      *            The username to set.
219      */
220     void setName(String aName) {
221         name = aName;
222     }
223
224     /**
225      * Gets the groups the user belongs to.
226      * 
227      * @return Groups.
228      */
229     public Set<Group> getGroups() {
230         Set<Group> result = new TreeSet<Group>();
231         result.addAll(groups);
232
233         return result;
234     }
235
236     /**
237      * Checks whether the user belongs to the given group.
238      * 
239      * @param aGroup
240      *            Group.
241      * 
242      * @return True if the user belongs to the group.
243      */
244     public boolean isInGroup(Group aGroup) {
245         return groups.contains(aGroup);
246     }
247
248     /**
249      * Checks whether the user belongs to the given group.
250      * 
251      * @param aGroup
252      *            Group.
253      * 
254      * @return True if the user belongs to the group.
255      */
256     public boolean isInGroup(String aGroup) {
257         return groups.contains(new Group(aGroup));
258     }
259
260     /**
261      * Gets the group set. For OR mapping.
262      * 
263      * @return set of groups.
264      */
265     Set<Group> getGroupSet() {
266         return groups;
267     }
268
269     /**
270      * Sets the groups the user belongs to, for OR mapping.
271      * 
272      * @param aGroups
273      *            Groups.
274      */
275     void setGroupSet(Set<Group> aGroups) {
276         groups = aGroups;
277     }
278
279     /**
280      * Adds the user to a group.
281      * 
282      * @param aGroup
283      *            Group to add the user to.
284      * 
285      * @throws UserMgtException
286      *             In case the user already belongs to the group.
287      */
288     void addGroup(Group aGroup) throws UserMgtException {
289         if (groups.contains(aGroup)) {
290             throw new UserMgtException(Reason.USER_ALREADY_IN_GROUP, aGroup);
291         }
292
293         groups.add(aGroup);
294     }
295
296     /**
297      * Removes the user from a group.
298      * 
299      * @param aGroup
300      *            Group.
301      * 
302      * @throws UserMgtException
303      *             In case the user does not belong to the group.
304      */
305     void removeGroup(Group aGroup) throws UserMgtException {
306         if (!groups.contains(aGroup)) {
307             throw new UserMgtException(Reason.USER_NOT_IN_GROUP, this, aGroup);
308         }
309
310         if (groups.size() == 1) {
311             throw new UserMgtException(Reason.USER_MUST_BE_IN_A_GROUP, this,
312                 aGroup);
313         }
314
315         groups.remove(aGroup);
316     }
317
318     /*
319      * (non-Javadoc)
320      * 
321      * @see java.lang.Object#equals(java.lang.Object)
322      */
323     @Override
324     public boolean equals(Object aUser) {
325         if (!(aUser instanceof User)) {
326             return false;
327         }
328
329         User user = (User) aUser;
330
331         return name.equals(user.name);
332     }
333
334     /*
335      * (non-Javadoc)
336      * 
337      * @see java.lang.Object#hashCode()
338      */
339     @Override
340     public int hashCode() {
341         return name.hashCode();
342     }
343
344     /*
345      * (non-Javadoc)
346      * 
347      * @see java.lang.Object#toString()
348      */
349     @Override
350     public String toString() {
351         String result = "User(name=" + name + ", password=" + password;
352
353         for (Group group : groups) {
354             result += (", group=" + group);
355         }
356
357         return result + ")";
358     }
359
360     /*
361      * (non-Javadoc)
362      * 
363      * @see java.lang.Comparable#compareTo(T)
364      */
365     public int compareTo(Object aUser) {
366         return name.compareTo(((User) aUser).name);
367     }
368 }