View Javadoc

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