root / src / gr / ebs / gss / server / domain / User.java @ 623:66f69a7348ed
History | View | Annotate | Download (13.8 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
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.
|
10 |
*
|
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.
|
15 |
*
|
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/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.server.domain; |
20 |
|
21 |
import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration; |
22 |
import gr.ebs.gss.server.domain.dto.UserDTO; |
23 |
|
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; |
29 |
import java.util.Random; |
30 |
import java.util.Set; |
31 |
|
32 |
import javax.persistence.CascadeType; |
33 |
import javax.persistence.Column; |
34 |
import javax.persistence.Embedded; |
35 |
import javax.persistence.Entity; |
36 |
import javax.persistence.FetchType; |
37 |
import javax.persistence.GeneratedValue; |
38 |
import javax.persistence.Id; |
39 |
import javax.persistence.ManyToMany; |
40 |
import javax.persistence.ManyToOne; |
41 |
import javax.persistence.OneToMany; |
42 |
import javax.persistence.OrderBy; |
43 |
import javax.persistence.Table; |
44 |
import javax.persistence.Temporal; |
45 |
import javax.persistence.TemporalType; |
46 |
import javax.persistence.Version; |
47 |
|
48 |
import org.hibernate.annotations.Cache; |
49 |
import org.hibernate.annotations.CacheConcurrencyStrategy; |
50 |
|
51 |
/**
|
52 |
* The class that holds information about a particular user of the system.
|
53 |
*
|
54 |
* @author past
|
55 |
*/
|
56 |
@Entity
|
57 |
@Table(name = "GSS_User") |
58 |
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
|
59 |
public class User implements Serializable { |
60 |
|
61 |
/**
|
62 |
* Length of generated random password.
|
63 |
*/
|
64 |
private static final int PASSWORD_LENGTH = 15; |
65 |
|
66 |
/**
|
67 |
* These characters will be used to generate random password.
|
68 |
*/
|
69 |
private static final String allowedPasswordCharacters = |
70 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
71 |
|
72 |
/**
|
73 |
* The authentication token size in bytes.
|
74 |
*/
|
75 |
private static final int TOKEN_SIZE = 40; |
76 |
|
77 |
/**
|
78 |
* The persistence ID of the object.
|
79 |
*/
|
80 |
@Id
|
81 |
@GeneratedValue
|
82 |
private Long id; |
83 |
|
84 |
/**
|
85 |
* Version field for optimistic locking.
|
86 |
*/
|
87 |
@SuppressWarnings("unused") |
88 |
@Version
|
89 |
private int version; |
90 |
|
91 |
/**
|
92 |
* The audit information.
|
93 |
*/
|
94 |
@Embedded
|
95 |
private AuditInfo auditInfo;
|
96 |
|
97 |
/**
|
98 |
* The first name of the user.
|
99 |
*/
|
100 |
private String firstname; |
101 |
|
102 |
/**
|
103 |
* The last name of the user.
|
104 |
*/
|
105 |
private String lastname; |
106 |
|
107 |
/**
|
108 |
* The full name of the user.
|
109 |
*/
|
110 |
private String name; |
111 |
|
112 |
/**
|
113 |
* The username of the user.
|
114 |
*/
|
115 |
@Column(unique = true) |
116 |
private String username; |
117 |
|
118 |
/**
|
119 |
* The e-mail address of the user.
|
120 |
*/
|
121 |
private String email; |
122 |
|
123 |
/**
|
124 |
* A unique ID provided by the Shibboleth IdP.
|
125 |
*/
|
126 |
@SuppressWarnings("unused") |
127 |
private String identityProviderId; |
128 |
|
129 |
/**
|
130 |
* The IdP URL.
|
131 |
*/
|
132 |
@SuppressWarnings("unused") |
133 |
private String identityProvider; |
134 |
|
135 |
/**
|
136 |
* The date and time the user last logged into the service.
|
137 |
*/
|
138 |
@Temporal(TemporalType.TIMESTAMP)
|
139 |
private Date lastLogin; |
140 |
|
141 |
/**
|
142 |
* The list of groups that have been specified by this user.
|
143 |
*/
|
144 |
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner") |
145 |
@OrderBy("name") |
146 |
private List<Group> groupsSpecified; |
147 |
|
148 |
/**
|
149 |
* The set of groups of which this user is member.
|
150 |
*/
|
151 |
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "members") |
152 |
private Set<Group> groupsMember; |
153 |
|
154 |
/**
|
155 |
* The list of all tags this user has specified on all files.
|
156 |
*/
|
157 |
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user") |
158 |
@OrderBy("tag") |
159 |
private List<FileTag> fileTags; |
160 |
|
161 |
/**
|
162 |
* The user class to which this user belongs.
|
163 |
*/
|
164 |
@ManyToOne
|
165 |
private UserClass userClass;
|
166 |
|
167 |
/**
|
168 |
* The authentication token issued for this user.
|
169 |
*/
|
170 |
private byte[] authToken; |
171 |
|
172 |
/**
|
173 |
* The time that the user's issued authentication token
|
174 |
* will expire.
|
175 |
*/
|
176 |
@Temporal(TemporalType.TIMESTAMP)
|
177 |
private Date authTokenExpiryDate; |
178 |
|
179 |
/**
|
180 |
* The active nonce issued for logging in this user, in
|
181 |
* Base64 encoding.
|
182 |
*/
|
183 |
private String nonce; |
184 |
|
185 |
/**
|
186 |
* The active nonce expiry date.
|
187 |
*/
|
188 |
private Date nonceExpiryDate; |
189 |
|
190 |
/**
|
191 |
* Flag that denotes whether the user has accepted the terms and
|
192 |
* conditions of the service.
|
193 |
* XXX: the columnDefinition is postgres specific, if deployment
|
194 |
* database is changed this shall be changed too
|
195 |
*/
|
196 |
@Column(columnDefinition=" boolean DEFAULT false") |
197 |
private boolean acceptedPolicy; |
198 |
|
199 |
/**
|
200 |
* A flag that denotes whether the user is active or not. Users may be
|
201 |
* administratively forbidden to use the service by setting this flag to
|
202 |
* false.
|
203 |
*/
|
204 |
@Column(columnDefinition=" boolean DEFAULT true") |
205 |
private boolean active; |
206 |
|
207 |
/**
|
208 |
* Password for WebDAV
|
209 |
*/
|
210 |
private String webDAVPassword; |
211 |
|
212 |
/**
|
213 |
* Retrieve the firstname.
|
214 |
*
|
215 |
* @return the firstname
|
216 |
*/
|
217 |
public String getFirstname() { |
218 |
return firstname;
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* Modify the firstname.
|
223 |
*
|
224 |
* @param newFirstname the firstname to set
|
225 |
*/
|
226 |
public void setFirstname(final String newFirstname) { |
227 |
firstname = newFirstname; |
228 |
} |
229 |
|
230 |
/**
|
231 |
* Retrieve the lastname.
|
232 |
*
|
233 |
* @return the lastname
|
234 |
*/
|
235 |
public String getLastname() { |
236 |
return lastname;
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* Modify the lastname.
|
241 |
*
|
242 |
* @param newLastname the lastname to set
|
243 |
*/
|
244 |
public void setLastname(final String newLastname) { |
245 |
lastname = newLastname; |
246 |
} |
247 |
|
248 |
/**
|
249 |
* Retrieve the name.
|
250 |
*
|
251 |
* @return the name
|
252 |
*/
|
253 |
public String getName() { |
254 |
return name;
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* Modify the name.
|
259 |
*
|
260 |
* @param newName the name to set
|
261 |
*/
|
262 |
public void setName(final String newName) { |
263 |
name = newName; |
264 |
} |
265 |
|
266 |
/**
|
267 |
* Retrieve the email.
|
268 |
*
|
269 |
* @return the email
|
270 |
*/
|
271 |
public String getEmail() { |
272 |
return email;
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Modify the email.
|
277 |
*
|
278 |
* @param newEmail the email to set
|
279 |
*/
|
280 |
public void setEmail(final String newEmail) { |
281 |
email = newEmail; |
282 |
} |
283 |
|
284 |
/**
|
285 |
* Retrieve the id.
|
286 |
*
|
287 |
* @return the id
|
288 |
*/
|
289 |
public Long getId() { |
290 |
return id;
|
291 |
} |
292 |
|
293 |
/**
|
294 |
* Retrieve the groups specified by this user.
|
295 |
*
|
296 |
* @return the groups
|
297 |
*/
|
298 |
public List<Group> getGroupsSpecified() { |
299 |
return groupsSpecified;
|
300 |
} |
301 |
|
302 |
/**
|
303 |
* Modify the groups specified by this user.
|
304 |
*
|
305 |
* @param newGroupsSpecified the groups to set
|
306 |
*/
|
307 |
public void setGroupsSpecified(final List<Group> newGroupsSpecified) { |
308 |
groupsSpecified = newGroupsSpecified; |
309 |
} |
310 |
|
311 |
/**
|
312 |
* Retrieve the groups of which this user is member.
|
313 |
*
|
314 |
* @return the groups
|
315 |
*/
|
316 |
public Set<Group> getGroupsMember() { |
317 |
return groupsMember;
|
318 |
} |
319 |
|
320 |
/**
|
321 |
* Modify the groups of which this user is member.
|
322 |
*
|
323 |
* @param newGroupsMember the groups to set
|
324 |
*/
|
325 |
public void setGroupsMember(final Set<Group> newGroupsMember) { |
326 |
groupsMember = newGroupsMember; |
327 |
} |
328 |
|
329 |
/**
|
330 |
* Retrieve the audit info.
|
331 |
*
|
332 |
* @return the audit info
|
333 |
*/
|
334 |
public AuditInfo getAuditInfo() {
|
335 |
return auditInfo;
|
336 |
} |
337 |
|
338 |
/**
|
339 |
* Modify the audit info.
|
340 |
*
|
341 |
* @param newAuditInfo the new audit info
|
342 |
*/
|
343 |
public void setAuditInfo(final AuditInfo newAuditInfo) { |
344 |
auditInfo = newAuditInfo; |
345 |
} |
346 |
|
347 |
/**
|
348 |
* Retrieve the file tags.
|
349 |
*
|
350 |
* @return a list of file tags
|
351 |
*/
|
352 |
public List<FileTag> getFileTags() { |
353 |
return fileTags;
|
354 |
} |
355 |
|
356 |
/**
|
357 |
* Replace the list of file tags.
|
358 |
*
|
359 |
* @param newFileTags the new file tags
|
360 |
*/
|
361 |
public void setFileTags(final List<FileTag> newFileTags) { |
362 |
fileTags = newFileTags; |
363 |
} |
364 |
|
365 |
/**
|
366 |
* Retrieve the user class.
|
367 |
*
|
368 |
* @return the user class
|
369 |
*/
|
370 |
public UserClass getUserClass() {
|
371 |
return userClass;
|
372 |
} |
373 |
|
374 |
/**
|
375 |
* Modify the user class.
|
376 |
*
|
377 |
* @param newUserClass the new user class
|
378 |
*/
|
379 |
public void setUserClass(final UserClass newUserClass) { |
380 |
userClass = newUserClass; |
381 |
} |
382 |
|
383 |
/**
|
384 |
* Retrieve the username.
|
385 |
*
|
386 |
* @return the username
|
387 |
*/
|
388 |
public String getUsername() { |
389 |
return username;
|
390 |
} |
391 |
|
392 |
/**
|
393 |
* Modify the username.
|
394 |
*
|
395 |
* @param aUsername the username to set
|
396 |
*/
|
397 |
public void setUsername(String aUsername) { |
398 |
username = aUsername; |
399 |
} |
400 |
|
401 |
/**
|
402 |
* Modify the lastLogin.
|
403 |
*
|
404 |
* @param aLastLogin the lastLogin to set
|
405 |
*/
|
406 |
public void setLastLogin(Date aLastLogin) { |
407 |
lastLogin = aLastLogin; |
408 |
} |
409 |
|
410 |
/**
|
411 |
* Retrieve the lastLogin.
|
412 |
*
|
413 |
* @return the lastLogin
|
414 |
*/
|
415 |
public Date getLastLogin() { |
416 |
return lastLogin;
|
417 |
} |
418 |
|
419 |
/**
|
420 |
* Retrieve the acceptedPolicy flag.
|
421 |
*
|
422 |
* @return the acceptedPolicy
|
423 |
*/
|
424 |
public boolean hasAcceptedPolicy() { |
425 |
return acceptedPolicy;
|
426 |
} |
427 |
/**
|
428 |
* Modify the acceptedPolicy flag.
|
429 |
*
|
430 |
* @param newAcceptedPolicy the acceptedPolicy to set
|
431 |
*/
|
432 |
public void setAcceptedPolicy(boolean newAcceptedPolicy) { |
433 |
acceptedPolicy = newAcceptedPolicy; |
434 |
} |
435 |
|
436 |
|
437 |
public String getWebDAVPassword() { |
438 |
return webDAVPassword;
|
439 |
} |
440 |
public void setWebDAVPassword(String aWebDAVPassword1) { |
441 |
webDAVPassword = aWebDAVPassword1; |
442 |
} |
443 |
|
444 |
// ********************** Business Methods ********************** //
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
/**
|
450 |
* Modify the identityProviderId.
|
451 |
*
|
452 |
* @param anIdentityProviderId the identityProviderId to set
|
453 |
*/
|
454 |
public void setIdentityProviderId(String anIdentityProviderId) { |
455 |
identityProviderId = anIdentityProviderId; |
456 |
} |
457 |
|
458 |
|
459 |
/**
|
460 |
* Modify the identityProvider.
|
461 |
*
|
462 |
* @param anIdentityProvider the identityProvider to set
|
463 |
*/
|
464 |
public void setIdentityProvider(String anIdentityProvider) { |
465 |
identityProvider = anIdentityProvider; |
466 |
} |
467 |
|
468 |
/**
|
469 |
* Retrieve the authentication token. If it is not valid
|
470 |
* or non-existent, this method returns null. Therefore, call
|
471 |
* sites must request a regeneration of the authentication
|
472 |
* token in both cases.
|
473 |
*
|
474 |
* @return the authToken
|
475 |
*/
|
476 |
public byte[] getAuthToken() { |
477 |
if (isAuthTokenValid())
|
478 |
return authToken;
|
479 |
return null; |
480 |
} |
481 |
|
482 |
/**
|
483 |
* Add a tag from this user to specified file.
|
484 |
*
|
485 |
* @param file the file
|
486 |
* @param tag the tag string
|
487 |
*/
|
488 |
public void addTag(final FileHeader file, final String tag) { |
489 |
@SuppressWarnings("unused") |
490 |
final FileTag fileTag = new FileTag(this, file, tag); |
491 |
// Cascade should take care of saving here.
|
492 |
} |
493 |
|
494 |
/**
|
495 |
* Return a Data Transfer Object for this User object.
|
496 |
*
|
497 |
* @return a user DTO
|
498 |
*/
|
499 |
public UserDTO getDTO() {
|
500 |
final UserDTO u = new UserDTO(); |
501 |
u.setId(id); |
502 |
u.setName(name); |
503 |
u.setLastname(lastname); |
504 |
u.setFirstname(firstname); |
505 |
u.setEmail(email); |
506 |
u.setUsername(username); |
507 |
u.setActive(active); |
508 |
if(userClass!= null) |
509 |
u.setUserClass(userClass.getDTOWithoutUsers()); |
510 |
u.setLastLoginDate(lastLogin); |
511 |
return u;
|
512 |
} |
513 |
|
514 |
/**
|
515 |
* Removes a group from this user's specified groups list.
|
516 |
*
|
517 |
* @param group the group to remove
|
518 |
* @throws IllegalArgumentException if group is null
|
519 |
*/
|
520 |
public void removeSpecifiedGroup(final Group group) { |
521 |
if (group == null) |
522 |
throw new IllegalArgumentException("Can't remove a null group."); |
523 |
getGroupsSpecified().remove(group); |
524 |
group.setOwner(null);
|
525 |
} |
526 |
|
527 |
/**
|
528 |
* @param name2
|
529 |
*/
|
530 |
public void createGroup(final String name2) { |
531 |
final Group group = new Group(name2); |
532 |
group.setOwner(this);
|
533 |
final Date now = new Date(); |
534 |
final AuditInfo ai = new AuditInfo(); |
535 |
ai.setCreatedBy(this);
|
536 |
ai.setCreationDate(now); |
537 |
ai.setModifiedBy(this);
|
538 |
ai.setModificationDate(now); |
539 |
group.setAuditInfo(ai); |
540 |
groupsSpecified.add(group); |
541 |
} |
542 |
|
543 |
/**
|
544 |
* Removes the specified tag from this user
|
545 |
*
|
546 |
* @param tag
|
547 |
*/
|
548 |
public void removeTag(FileTag tag) { |
549 |
fileTags.remove(tag); |
550 |
tag.setUser(null);
|
551 |
} |
552 |
|
553 |
/**
|
554 |
* Creates a new authentication token and resets
|
555 |
* its expiry date.
|
556 |
*/
|
557 |
public void generateAuthToken() { |
558 |
SecureRandom random = new SecureRandom(); |
559 |
authToken = new byte[TOKEN_SIZE]; |
560 |
random.nextBytes(authToken); |
561 |
Calendar cal = Calendar.getInstance(); |
562 |
// Set token time-to-live to the number of days specified in
|
563 |
// gss.properties.
|
564 |
cal.add(Calendar.DAY_OF_MONTH, getConfiguration().getInt("tokenTTL", 1)); |
565 |
authTokenExpiryDate = cal.getTime(); |
566 |
} |
567 |
|
568 |
/**
|
569 |
* Return true if the authentication token is usable, or false
|
570 |
* if a new one must be regenerated.
|
571 |
*
|
572 |
* @return true if the authentication token is valid
|
573 |
*/
|
574 |
private boolean isAuthTokenValid() { |
575 |
if (authToken == null) |
576 |
return false; |
577 |
if (authTokenExpiryDate == null) |
578 |
return false; |
579 |
if (authTokenExpiryDate.before(new Date())) |
580 |
return false; |
581 |
return true; |
582 |
} |
583 |
|
584 |
/**
|
585 |
* Request the invalidation of the authentication token.
|
586 |
* After this method is called, a new token must be generated.
|
587 |
*/
|
588 |
public void invalidateAuthToken() { |
589 |
authToken = null;
|
590 |
authTokenExpiryDate = null;
|
591 |
} |
592 |
|
593 |
/**
|
594 |
* Retrieve the nonce. If it is not valid or non-existent,
|
595 |
* this method returns null.
|
596 |
*
|
597 |
* @return the nonce
|
598 |
*/
|
599 |
public String getNonce() { |
600 |
if (isNonceValid())
|
601 |
return nonce;
|
602 |
return null; |
603 |
} |
604 |
|
605 |
/**
|
606 |
* Return true if the nonce is usable, or false
|
607 |
* if not.
|
608 |
*
|
609 |
* @return true if the nonce is valid
|
610 |
*/
|
611 |
private boolean isNonceValid() { |
612 |
if (nonce == null) |
613 |
return false; |
614 |
if (nonceExpiryDate == null) |
615 |
return false; |
616 |
if (nonceExpiryDate.before(new Date())) |
617 |
return false; |
618 |
return true; |
619 |
} |
620 |
|
621 |
/**
|
622 |
* Modify the nonce.
|
623 |
*
|
624 |
* @param aNonce the nonce to set
|
625 |
*/
|
626 |
public void setNonce(String aNonce) { |
627 |
nonce = aNonce; |
628 |
} |
629 |
|
630 |
/**
|
631 |
* Modify the nonce expiry date.
|
632 |
*
|
633 |
* @param aNonceExpiryDate the nonce expiry date to set
|
634 |
*/
|
635 |
public void setNonceExpiryDate(Date aNonceExpiryDate) { |
636 |
nonceExpiryDate = aNonceExpiryDate; |
637 |
} |
638 |
|
639 |
public boolean isActive() { |
640 |
return active;
|
641 |
} |
642 |
|
643 |
public void setActive(boolean isActive) { |
644 |
active = isActive; |
645 |
} |
646 |
|
647 |
@Override
|
648 |
public boolean equals(Object o) { |
649 |
if (this == o) return true; |
650 |
if (!(o instanceof User)) return false; |
651 |
User user = (User) o; |
652 |
return user.getUsername().equals(username) && user.getName().equals(name);
|
653 |
} |
654 |
|
655 |
@Override
|
656 |
public int hashCode() { |
657 |
return 37 * username.hashCode() + name.hashCode(); |
658 |
} |
659 |
|
660 |
public void generateWebDAVPassword() { |
661 |
Random random = new Random(); |
662 |
StringBuilder sb = new StringBuilder(); |
663 |
int length = allowedPasswordCharacters.length();
|
664 |
for (int i=0; i<PASSWORD_LENGTH; i++) { |
665 |
int j = random.nextInt(length);
|
666 |
sb.append(allowedPasswordCharacters.charAt(j)); |
667 |
} |
668 |
webDAVPassword = sb.toString(); |
669 |
} |
670 |
} |