View Javadoc

1   /*
2    *  DISCLAIMER
3    */
4   package org.flowfuse.base.model.user;
5   
6   import net.sf.acegisecurity.GrantedAuthority;
7   import net.sf.acegisecurity.GrantedAuthorityImpl;
8   import net.sf.acegisecurity.UserDetails;
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.flowfuse.base.services.right.RightsManagementService;
12  import org.flowfuse.base.model.BaseObject;
13  
14  import java.util.HashMap;
15  import java.util.HashSet;
16  import java.util.Iterator;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Set;
20  
21  /***
22   * @author <a href="stefan@flowfuse.org">Stefan Kleineikenscheidt</a>,
23   *         Flowfuse.org
24   * @version $Id: User.java,v 1.3 2006/04/17 12:18:45 skleinei Exp $
25   * @hibernate.class table="FF_USER"
26   */
27  public class User extends BaseObject implements UserDetails {
28  
29    /***
30     * RCS ID
31     */
32    public final static String rcsid = "$Id: User.java,v 1.3 2006/04/17 12:18:45 skleinei Exp $";
33  
34    /***
35     * Logger
36     */
37    protected final Log logger = LogFactory.getLog(this.getClass());
38  
39    Long id = null;
40    String username = new String();
41    String password = new String();
42    String firstName = new String();
43    String lastName = new String();
44    String email = new String();
45    Set groups = new HashSet();
46    Map details = new HashMap();
47    boolean locked = true;
48    boolean disabled = true;
49    private RightsManagementService rightsManagementService = null;
50  
51    /***
52     * @return Returns the id.
53     * @hibernate.id column="USER_ID" generator-class="increment"
54     * unsaved-value="null"
55     */
56    public Long getId() {
57      return id;
58    }
59  
60    private void setId(Long id) {
61      this.id = id;
62    }
63  
64    /***
65     * @hibernate.property column="USER_USERNAME" length="40"
66     * not-null="true"
67     */
68    public String getUsername() {
69      return username;
70    }
71  
72    public void setUsername(String username) {
73      this.username = username;
74    }
75  
76    /***
77     * @hibernate.property column="USER_PASSWORD" length="40"
78     * not-null="true"
79     */
80    public String getPassword() {
81      return password;
82    }
83  
84    public void setPassword(String password) {
85      this.password = password;
86    }
87  
88    /***
89     * @hibernate.property column="USER_FIRSTNAME" length="60"
90     */
91    public String getFirstName() {
92      return firstName;
93    }
94  
95    public void setFirstName(String firstName) {
96      this.firstName = firstName;
97    }
98  
99    /***
100    * @hibernate.property column="USER_LASTNAME" length="60"
101    */
102   public String getLastName() {
103     return lastName;
104   }
105 
106   public void setLastName(String lastName) {
107     this.lastName = lastName;
108   }
109 
110   public String getFullName() {
111     if (!this.firstName.equals("") && !this.lastName.equals("")) {
112       return this.firstName + " " + this.lastName;
113     } else if (!this.firstName.equals("")) {
114       return this.firstName;
115     } else if (!this.lastName.equals("")) {
116       return this.lastName;
117     } else {
118       return this.username;
119     }
120   }
121 
122   /***
123    * @hibernate.property column="USER_EMAIL" length="60"
124    */
125   public String getEmail() {
126     return email;
127   }
128 
129   public void setEmail(String email) {
130     this.email = email;
131   }
132 
133   /***
134    * @hibernate.set lazy="false" cascade="all" table="FF_USER_GROUP"
135    * @hibernate.key column="USER_ID"
136    * @hibernate.many-to-many class="org.flowfuse.base.model.user.Group"
137    * column="GROUP_ID"
138    */
139   public Set getGroups() {
140     return groups;
141   }
142 
143   public void setGroups(Set groups) {
144     this.groups = groups;
145   }
146 
147   public void addGroup(Group group) {
148     if (this.groups == null) {
149       this.groups = new HashSet();
150     }
151 
152     this.groups.add(group);
153   }
154 
155   /***
156    * Checks whether this user is member of the given group.
157    *
158    * @param group
159    * @return true, if user is in group.
160    */
161   public boolean isInGroup(Group group) {
162     if (this.groups == null) {
163       this.groups = new HashSet();
164     }
165 
166     return this.groups.contains(group);
167   }
168 
169   /***
170    * @hibernate.map lazy="false" table="FF_USER_DETAIL"
171    * @hibernate.key column="USER_ID"
172    * @hibernate.index column="DETAIL_KEY" type="string"
173    * length="60"
174    * @hibernate.element column="DETAIL_VALUE" type="string"
175    * length="200" not-null="true"
176    */
177   public Map getDetails() {
178     return details;
179   }
180 
181   public void setDetails(Map details) {
182     Set keys = details.keySet();
183     for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
184       Object key = (Object) iterator.next();
185       Object value = details.get(key);
186       if (!(key instanceof String) ||
187               (!(value instanceof String))) {
188         throw new IllegalArgumentException("All keys and values for user " +
189                 "details have to be Strings");
190       }
191     }
192     this.details = details;
193   }
194 
195   /***
196    * Add subscriber detail.  Both key and value have to be
197    * <code>String</code>s for this implementation.
198    *
199    * @param key   detail key (has to be a <code>String</code>)
200    * @param value detail value (has to be a <code>String</code>)
201    * @throws IllegalArgumentException if key or value are not <code>String</code>s.
202    */
203   public void addDetail(String key, String value) {
204     if ((!(key instanceof String)) ||
205             (!(value instanceof String))) {
206       throw new IllegalArgumentException("Key and value for user details" +
207               "have to be Strings");
208     }
209     this.details.put(key, value);
210   }
211 
212   public String getDetail(String key) {
213     return (String) this.details.get(key);
214   }
215 
216   /***
217    * @hibernate.property column="USER_LOCKED" type="boolean"
218    * not-null="true"
219    */
220   public boolean isLocked() {
221     return locked;
222   }
223 
224   public void setLocked(boolean locked) {
225     this.locked = locked;
226   }
227 
228   /***
229    * @hibernate.property column="USER_DISABLED" type="boolean"
230    * not-null="true"
231    */
232   public boolean isDisabled() {
233     return disabled;
234   }
235 
236   public void setDisabled(boolean disabled) {
237     this.disabled = disabled;
238   }
239 
240   public List getSystemRights() {
241     return this.rightsManagementService.getSystemRights(this);
242   }
243 
244   public boolean isAccountNonExpired() {
245     // TODO implement expired accounts
246     logger.warn(
247             "Not checked whether account is expired, b/c its not " +
248             "implemented yet.");
249     return true;
250   }
251 
252   public boolean isAccountNonLocked() {
253     return !this.locked;
254   }
255 
256   public GrantedAuthority[] getAuthorities() {
257     Set groups = this.getGroups();
258     GrantedAuthority[] authorities = new GrantedAuthority[groups.size()];
259     int cnt = 0;
260 
261     for (Iterator iterator = groups.iterator(); iterator.hasNext();) {
262       Group group = (Group) iterator.next();
263       authorities[cnt] = new GrantedAuthorityImpl(group.getName());
264     }
265     return authorities;
266   }
267 
268   public boolean isCredentialsNonExpired() {
269     // TODO implement expired credentials
270     logger.warn("Not checked whether credentials are expired, " +
271             "b/c its not implemented yet.");
272     return true;
273   }
274 
275   public boolean isEnabled() {
276     return !this.disabled;
277   }
278 
279   public boolean equals(Object o) {
280     if (this == o) {
281       return true;
282     }
283     if (!(o instanceof User)) {
284       return false;
285     }
286 
287     final User user = (User) o;
288 
289     if (!username.equals(user.username)) {
290       return false;
291     }
292 
293     return true;
294   }
295 
296   public int hashCode() {
297     return username.hashCode();
298   }
299 
300   public String toString() {
301     return "User{" +
302             "username='" + username + "'" +
303             ", firstName='" + firstName + "'" +
304             ", lastName='" + lastName + "'" +
305             ", email='" + email + "'" +
306             ", groups=" + groups +
307             ", details=" + details +
308             "}";
309   }
310 
311   /***
312    * Retrieves dependency (see <a href="http://www.springframework.org">
313    * http://www.springframework.org</a>).
314    *
315    * @return
316    */
317   public RightsManagementService getRightsManagementService() {
318     return rightsManagementService;
319   }
320 
321   /***
322    * Injects dependency (see <a href="http://www.springframework.org">
323    * http://www.springframework.org</a>).
324    *
325    * @param rightsManagementService
326    */
327   public void setRightsManagementService(
328           RightsManagementService rightsManagementService) {
329     this.rightsManagementService = rightsManagementService;
330   }
331 
332 }
333 
334 /* EOF */