View Javadoc

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