View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.model;
24  
25  import java.io.Serializable;
26  import java.util.ArrayList;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  
31  import javax.persistence.Column;
32  import javax.persistence.Embedded;
33  import javax.persistence.Entity;
34  import javax.persistence.FetchType;
35  import javax.persistence.GeneratedValue;
36  import javax.persistence.GenerationType;
37  import javax.persistence.Id;
38  import javax.persistence.JoinColumn;
39  import javax.persistence.JoinTable;
40  import javax.persistence.ManyToMany;
41  import javax.persistence.Table;
42  import javax.persistence.Transient;
43  import javax.persistence.Version;
44  
45  import org.springframework.security.GrantedAuthority;
46  import org.springframework.security.userdetails.UserDetails;
47  import org.apache.commons.lang.builder.ToStringBuilder;
48  import org.apache.commons.lang.builder.ToStringStyle;
49  
50  /**
51   * This class represents the basic "user" object in AppFuse that allows for
52   * authentication and user management. It implements Acegi Security's
53   * UserDetails interface.
54   * 
55   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> Updated by
56   *         Dan Kibler (dan@getrolling.com) Extended to implement Acegi
57   *         UserDetails interface by David Carter david@carter.net
58   */
59  @Entity
60  @Table(name = "app_user")
61  public class User extends BaseObject implements Serializable, UserDetails {
62      private static final long serialVersionUID = 3832626162173359411L;
63  
64      private Long id;
65  
66      private String username; // required
67  
68      private String password; // required
69  
70      private String confirmPassword;
71  
72      private String passwordHint;
73  
74      private String firstName; // required
75  
76      private String lastName; // required
77  
78      private String email; // required; unique
79  
80      private String phoneNumber;
81  
82      private String website;
83  
84      private Address address = new Address();
85  
86      private Integer version;
87  
88      private Set<Role> roles = new HashSet<Role>();
89  
90      private boolean enabled;
91  
92      private boolean accountExpired;
93  
94      private boolean accountLocked;
95  
96      private boolean credentialsExpired;
97  
98      /**
99       * Default constructor - creates a new instance with no values set.
100      */
101     public User() {
102     }
103 
104     /**
105      * Create a new instance and set the username.
106      * 
107      * @param username
108      *                login name for user.
109      */
110     public User(final String username) {
111 	this.username = username;
112     }
113 
114     @Id
115     @GeneratedValue(strategy = GenerationType.AUTO)
116     public Long getId() {
117 	return id;
118     }
119 
120     @Column(nullable = false, length = 50, unique = true)
121     public String getUsername() {
122 	return username;
123     }
124 
125     @Column(nullable = false)
126     public String getPassword() {
127 	return password;
128     }
129 
130     @Transient
131     public String getConfirmPassword() {
132 	return confirmPassword;
133     }
134 
135     @Column(name = "password_hint")
136     public String getPasswordHint() {
137 	return passwordHint;
138     }
139 
140     @Column(name = "first_name", nullable = false, length = 50)
141     public String getFirstName() {
142 	return firstName;
143     }
144 
145     @Column(name = "last_name", nullable = false, length = 50)
146     public String getLastName() {
147 	return lastName;
148     }
149 
150     @Column(nullable = false, unique = true)
151     public String getEmail() {
152 	return email;
153     }
154 
155     @Column(name = "phone_number")
156     public String getPhoneNumber() {
157 	return phoneNumber;
158     }
159 
160     public String getWebsite() {
161 	return website;
162     }
163 
164     /**
165      * Returns the full name.
166      * 
167      * @return firstName + ' ' + lastName
168      */
169     @Transient
170     public String getFullName() {
171 	return firstName + ' ' + lastName;
172     }
173 
174     @Embedded
175     public Address getAddress() {
176 	return address;
177     }
178 
179     @ManyToMany(fetch = FetchType.EAGER)
180     @JoinTable(name = "user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = @JoinColumn(name = "role_id"))
181     public Set<Role> getRoles() {
182 	return roles;
183     }
184 
185     /**
186      * Convert user roles to LabelValue objects for convenience.
187      * 
188      * @return a list of LabelValue objects with role information
189      */
190     @Transient
191     public List<LabelValue> getRoleList() {
192 	List<LabelValue> userRoles = new ArrayList<LabelValue>();
193 
194 	if (this.roles != null) {
195 	    for (Role role : roles) {
196 		// convert the user's roles to LabelValue Objects
197 		userRoles.add(new LabelValue(role.getName(), role.getName()));
198 	    }
199 	}
200 
201 	return userRoles;
202     }
203 
204     /**
205      * Adds a role for the user
206      * 
207      * @param role
208      *                the fully instantiated role
209      */
210     public void addRole(Role role) {
211 	getRoles().add(role);
212     }
213 
214     /**
215      * @see org.acegisecurity.userdetails.UserDetails#getAuthorities()
216      * @return GrantedAuthority[] an array of roles.
217      */
218     @Transient
219     public GrantedAuthority[] getAuthorities() {
220 	return roles.toArray(new GrantedAuthority[0]);
221     }
222 
223     @Version
224     public Integer getVersion() {
225 	return version;
226     }
227 
228     @Column(name = "account_enabled")
229     public boolean isEnabled() {
230 	return enabled;
231     }
232 
233     @Column(name = "account_expired", nullable = false)
234     public boolean isAccountExpired() {
235 	return accountExpired;
236     }
237 
238     /**
239      * @see org.acegisecurity.userdetails.UserDetails#isAccountNonExpired()
240      */
241     @Transient
242     public boolean isAccountNonExpired() {
243 	return !isAccountExpired();
244     }
245 
246     @Column(name = "account_locked", nullable = false)
247     public boolean isAccountLocked() {
248 	return accountLocked;
249     }
250 
251     /**
252      * @see org.acegisecurity.userdetails.UserDetails#isAccountNonLocked()
253      */
254     @Transient
255     public boolean isAccountNonLocked() {
256 	return !isAccountLocked();
257     }
258 
259     @Column(name = "credentials_expired", nullable = false)
260     public boolean isCredentialsExpired() {
261 	return credentialsExpired;
262     }
263 
264     /**
265      * @see org.acegisecurity.userdetails.UserDetails#isCredentialsNonExpired()
266      */
267     @Transient
268     public boolean isCredentialsNonExpired() {
269 	return !credentialsExpired;
270     }
271 
272     public void setId(Long id) {
273 	this.id = id;
274     }
275 
276     public void setUsername(String username) {
277 	this.username = username;
278     }
279 
280     public void setPassword(String password) {
281 	this.password = password;
282     }
283 
284     public void setConfirmPassword(String confirmPassword) {
285 	this.confirmPassword = confirmPassword;
286     }
287 
288     public void setPasswordHint(String passwordHint) {
289 	this.passwordHint = passwordHint;
290     }
291 
292     public void setFirstName(String firstName) {
293 	this.firstName = firstName;
294     }
295 
296     public void setLastName(String lastName) {
297 	this.lastName = lastName;
298     }
299 
300     public void setEmail(String email) {
301 	this.email = email;
302     }
303 
304     public void setPhoneNumber(String phoneNumber) {
305 	this.phoneNumber = phoneNumber;
306     }
307 
308     public void setWebsite(String website) {
309 	this.website = website;
310     }
311 
312     public void setAddress(Address address) {
313 	this.address = address;
314     }
315 
316     public void setRoles(Set<Role> roles) {
317 	this.roles = roles;
318     }
319 
320     public void setVersion(Integer version) {
321 	this.version = version;
322     }
323 
324     public void setEnabled(boolean enabled) {
325 	this.enabled = enabled;
326     }
327 
328     public void setAccountExpired(boolean accountExpired) {
329 	this.accountExpired = accountExpired;
330     }
331 
332     public void setAccountLocked(boolean accountLocked) {
333 	this.accountLocked = accountLocked;
334     }
335 
336     public void setCredentialsExpired(boolean credentialsExpired) {
337 	this.credentialsExpired = credentialsExpired;
338     }
339 
340     /**
341      * {@inheritDoc}
342      */
343     @Override
344     public boolean equals(Object o) {
345 	if (this == o) {
346 	    return true;
347 	}
348 	if (!(o instanceof User)) {
349 	    return false;
350 	}
351 
352 	final User user = (User) o;
353 
354 	return !(username != null ? !username.equals(user.getUsername()) : user
355 		.getUsername() != null);
356 
357     }
358 
359     /**
360      * {@inheritDoc}
361      */
362     @Override
363     public int hashCode() {
364 	return (username != null ? username.hashCode() : 0);
365     }
366 
367     /**
368      * {@inheritDoc}
369      */
370     @Override
371     public String toString() {
372 	ToStringBuilder sb = new ToStringBuilder(this,
373 		ToStringStyle.DEFAULT_STYLE).append("username", this.username)
374 		.append("enabled", this.enabled).append("accountExpired",
375 			this.accountExpired).append("credentialsExpired",
376 			this.credentialsExpired).append("accountLocked",
377 			this.accountLocked);
378 
379 	GrantedAuthority[] auths = this.getAuthorities();
380 	if (auths != null) {
381 	    sb.append("Granted Authorities: ");
382 
383 	    for (int i = 0; i < auths.length; i++) {
384 		if (i > 0) {
385 		    sb.append(", ");
386 		}
387 		sb.append(auths[i].toString());
388 	    }
389 	} else {
390 	    sb.append("No Granted Authorities");
391 	}
392 	return sb.toString();
393     }
394 }