2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.server.domain;
21 import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 import gr.ebs.gss.server.domain.dto.UserDTO;
24 import java.io.Serializable;
25 import java.security.SecureRandom;
26 import java.util.Calendar;
27 import java.util.Date;
28 import java.util.List;
31 import javax.persistence.CascadeType;
32 import javax.persistence.Column;
33 import javax.persistence.Embedded;
34 import javax.persistence.Entity;
35 import javax.persistence.FetchType;
36 import javax.persistence.GeneratedValue;
37 import javax.persistence.Id;
38 import javax.persistence.ManyToMany;
39 import javax.persistence.ManyToOne;
40 import javax.persistence.OneToMany;
41 import javax.persistence.OrderBy;
42 import javax.persistence.Table;
43 import javax.persistence.Temporal;
44 import javax.persistence.TemporalType;
45 import javax.persistence.Version;
48 * The class that holds information about a particular user of the system.
53 @Table(name = "GSS_User")
54 public class User implements Serializable {
57 * The authentication token size in bytes.
59 private static final int TOKEN_SIZE = 40;
62 * The persistence ID of the object.
69 * Version field for optimistic locking.
71 @SuppressWarnings("unused")
76 * The audit information.
79 private AuditInfo auditInfo;
82 * The first name of the user.
84 private String firstname;
87 * The last name of the user.
89 private String lastname;
92 * The full name of the user.
97 * The username of the user.
99 @Column(unique = true)
100 private String username;
103 * The e-mail address of the user.
105 private String email;
108 * The list of groups that have been specified by this user.
110 @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
112 private List<Group> groupsSpecified;
115 * The set of groups of which this user is member.
117 @ManyToMany(fetch = FetchType.LAZY, mappedBy = "members")
118 private Set<Group> groupsMember;
121 * The list of all tags this user has specified on all files.
123 @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
125 private List<FileTag> fileTags;
128 * The user class to which this user belongs.
131 private UserClass userClass;
134 * The authentication token issued for this user.
136 private byte[] authToken;
139 * The time that the user's issued authentication token
142 @Temporal(TemporalType.TIMESTAMP)
143 private Date authTokenExpiryDate;
146 * The active nonce issued for logging in this user, in
149 private String nonce;
152 * The active nonce expiry date.
154 private Date nonceExpiryDate;
157 * Flag that denotes whether the user has accepted the
158 * terms and conditions of the service.
159 * XXX: the columnDefinition is postgres specific, if deployment database is changed this shall be changed too
161 @Column(columnDefinition=" boolean DEFAULT false")
162 private boolean acceptedPolicy;
165 * Retrieve the firstname.
167 * @return the firstname
169 public String getFirstname() {
174 * Modify the firstname.
176 * @param newFirstname the firstname to set
178 public void setFirstname(final String newFirstname) {
179 firstname = newFirstname;
183 * Retrieve the lastname.
185 * @return the lastname
187 public String getLastname() {
192 * Modify the lastname.
194 * @param newLastname the lastname to set
196 public void setLastname(final String newLastname) {
197 lastname = newLastname;
205 public String getName() {
212 * @param newName the name to set
214 public void setName(final String newName) {
219 * Retrieve the email.
223 public String getEmail() {
230 * @param newEmail the email to set
232 public void setEmail(final String newEmail) {
241 public Long getId() {
246 * Retrieve the groups specified by this user.
250 public List<Group> getGroupsSpecified() {
251 return groupsSpecified;
255 * Modify the groups specified by this user.
257 * @param newGroupsSpecified the groups to set
259 public void setGroupsSpecified(final List<Group> newGroupsSpecified) {
260 groupsSpecified = newGroupsSpecified;
264 * Retrieve the groups of which this user is member.
268 public Set<Group> getGroupsMember() {
273 * Modify the groups of which this user is member.
275 * @param newGroupsMember the groups to set
277 public void setGroupsMember(final Set<Group> newGroupsMember) {
278 groupsMember = newGroupsMember;
282 * Retrieve the audit info.
284 * @return the audit info
286 public AuditInfo getAuditInfo() {
291 * Modify the audit info.
293 * @param newAuditInfo the new audit info
295 public void setAuditInfo(final AuditInfo newAuditInfo) {
296 auditInfo = newAuditInfo;
300 * Retrieve the file tags.
302 * @return a list of file tags
304 public List<FileTag> getFileTags() {
309 * Replace the list of file tags.
311 * @param newFileTags the new file tags
313 public void setFileTags(final List<FileTag> newFileTags) {
314 fileTags = newFileTags;
318 * Retrieve the user class.
320 * @return the user class
322 public UserClass getUserClass() {
327 * Modify the user class.
329 * @param newUserClass the new user class
331 public void setUserClass(final UserClass newUserClass) {
332 userClass = newUserClass;
335 // ********************** Business Methods ********************** //
340 * Retrieve the username.
342 * @return the username
344 public String getUsername() {
350 * Modify the username.
352 * @param aUsername the username to set
354 public void setUsername(String aUsername) {
355 username = aUsername;
359 * Retrieve the authentication token. If it is not valid
360 * or non-existent, this method returns null. Therefore, call
361 * sites must request a regeneration of the authentication
362 * token in both cases.
364 * @return the authToken
366 public byte[] getAuthToken() {
367 if (isAuthTokenValid())
373 * Add a tag from this user to specified file.
375 * @param file the file
376 * @param tag the tag string
378 public void addTag(final FileHeader file, final String tag) {
379 @SuppressWarnings("unused")
380 final FileTag fileTag = new FileTag(this, file, tag);
381 // Cascade should take care of saving here.
385 * Return a Data Transfer Object for this User object.
389 public UserDTO getDTO() {
390 final UserDTO u = new UserDTO();
393 u.setLastname(lastname);
394 u.setFirstname(firstname);
396 u.setUsername(username);
401 * Removes a group from this user's specified groups list.
403 * @param group the group to remove
404 * @throws IllegalArgumentException if group is null
406 public void removeSpecifiedGroup(final Group group) {
408 throw new IllegalArgumentException("Can't remove a null group.");
409 getGroupsSpecified().remove(group);
410 group.setOwner(null);
416 public void createGroup(final String name2) {
417 final Group group = new Group(name2);
418 group.setOwner(this);
419 final Date now = new Date();
420 final AuditInfo ai = new AuditInfo();
421 ai.setCreatedBy(this);
422 ai.setCreationDate(now);
423 ai.setModifiedBy(this);
424 ai.setModificationDate(now);
425 group.setAuditInfo(ai);
426 groupsSpecified.add(group);
430 * Removes the specified tag from this user
434 public void removeTag(FileTag tag) {
435 fileTags.remove(tag);
440 * Creates a new authentication token and resets
443 public void generateAuthToken() {
444 SecureRandom random = new SecureRandom();
445 authToken = new byte[TOKEN_SIZE];
446 random.nextBytes(authToken);
447 Calendar cal = Calendar.getInstance();
448 // Set token time-to-live to the number of days specified in
450 cal.add(Calendar.DAY_OF_MONTH, getConfiguration().getInt("tokenTTL", 1));
451 authTokenExpiryDate = cal.getTime();
455 * Return true if the authentication token is usable, or false
456 * if a new one must be regenerated.
458 * @return true if the authentication token is valid
460 private boolean isAuthTokenValid() {
461 if (authToken == null)
463 if (authTokenExpiryDate == null)
465 if (authTokenExpiryDate.before(new Date()))
471 * Request the invalidation of the authentication token.
472 * After this method is called, a new token must be generated.
474 public void invalidateAuthToken() {
476 authTokenExpiryDate = null;
480 * Retrieve the nonce. If it is not valid or non-existent,
481 * this method returns null.
485 public String getNonce() {
492 * Return true if the nonce is usable, or false
495 * @return true if the nonce is valid
497 private boolean isNonceValid() {
500 if (nonceExpiryDate == null)
502 if (nonceExpiryDate.before(new Date()))
510 * @param aNonce the nonce to set
512 public void setNonce(String aNonce) {
517 * Modify the nonce expiry date.
519 * @param aNonceExpiryDate the nonce expiry date to set
521 public void setNonceExpiryDate(Date aNonceExpiryDate) {
522 nonceExpiryDate = aNonceExpiryDate;
526 public boolean equals(Object o) {
527 if (this == o) return true;
528 if (!(o instanceof User)) return false;
529 User user = (User) o;
530 return user.getUsername().equals(username) && user.getName().equals(name);
534 public int hashCode() {
535 return 37 * username.hashCode() + name.hashCode();
539 * Retrieve the acceptedPolicy flag.
541 * @return the acceptedPolicy
543 public boolean hasAcceptedPolicy() {
544 return acceptedPolicy;
548 * Modify the acceptedPolicy flag.
550 * @param newAcceptedPolicy the acceptedPolicy to set
552 public void setAcceptedPolicy(boolean newAcceptedPolicy) {
553 acceptedPolicy = newAcceptedPolicy;