Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / server / domain / User.java @ 83c3bc8e

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 org.gss_project.gss.server.domain;
20

    
21
import static org.gss_project.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22
import org.gss_project.gss.common.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 list of groups that have been specified by this user.
137
         */
138
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
139
        @OrderBy("name")
140
        private List<Group> groupsSpecified;
141

    
142
        /**
143
         * The set of groups of which this user is member.
144
         */
145
        @ManyToMany(fetch = FetchType.LAZY, mappedBy = "members")
146
        private Set<Group> groupsMember;
147

    
148
        /**
149
         * The list of all tags this user has specified on all files.
150
         */
151
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
152
        @OrderBy("tag")
153
        private List<FileTag> fileTags;
154

    
155
        /**
156
         * The user class to which this user belongs.
157
         */
158
        @ManyToOne
159
        private UserClass userClass;
160

    
161
        /**
162
         * The authentication token issued for this user.
163
         */
164
        private byte[] authToken;
165

    
166
        /**
167
         * The time that the user's issued authentication token
168
         * will expire.
169
         */
170
        @Temporal(TemporalType.TIMESTAMP)
171
        private Date authTokenExpiryDate;
172

    
173
        /**
174
         * The active nonce issued for logging in this user, in
175
         * Base64 encoding.
176
         */
177
        private String nonce;
178

    
179
        /**
180
         * The active nonce expiry date.
181
         */
182
        private Date nonceExpiryDate;
183

    
184
        /**
185
         * Flag that denotes whether the user has accepted the terms and
186
         * conditions of the service.
187
         * XXX: the columnDefinition is postgres specific, if deployment
188
         * database is changed this shall be changed too
189
         */
190
        @Column(columnDefinition=" boolean DEFAULT false")
191
        private boolean acceptedPolicy;
192

    
193
        /**
194
         * A flag that denotes whether the user is active or not. Users may be
195
         * administratively forbidden to use the service by setting this flag to
196
         * false.
197
         */
198
        @Column(columnDefinition=" boolean DEFAULT true")
199
        private boolean active;
200

    
201
        /**
202
         * Password for WebDAV
203
         */
204
        private String webDAVPassword;
205

    
206
    /**
207
     * The HTTP_SHIB_HOMEORGANIZATION schiboleth attribute that is not used to determine the AUTH users
208
     */
209
    private String homeOrganization;
210

    
211
        /**
212
         * Retrieve the firstname.
213
         *
214
         * @return the firstname
215
         */
216
        public String getFirstname() {
217
                return firstname;
218
        }
219

    
220
        /**
221
         * Modify the firstname.
222
         *
223
         * @param newFirstname the firstname to set
224
         */
225
        public void setFirstname(final String newFirstname) {
226
                firstname = newFirstname;
227
        }
228

    
229
        /**
230
         * Retrieve the lastname.
231
         *
232
         * @return the lastname
233
         */
234
        public String getLastname() {
235
                return lastname;
236
        }
237

    
238
        /**
239
         * Modify the lastname.
240
         *
241
         * @param newLastname the lastname to set
242
         */
243
        public void setLastname(final String newLastname) {
244
                lastname = newLastname;
245
        }
246

    
247
        /**
248
         * Retrieve the name.
249
         *
250
         * @return the name
251
         */
252
        public String getName() {
253
                return name;
254
        }
255

    
256
        /**
257
         * Modify the name.
258
         *
259
         * @param newName the name to set
260
         */
261
        public void setName(final String newName) {
262
                name = newName;
263
        }
264

    
265
        /**
266
         * Retrieve the email.
267
         *
268
         * @return the email
269
         */
270
        public String getEmail() {
271
                return email;
272
        }
273

    
274
        /**
275
         * Modify the email.
276
         *
277
         * @param newEmail the email to set
278
         */
279
        public void setEmail(final String newEmail) {
280
                email = newEmail;
281
        }
282

    
283
        /**
284
         * Retrieve the id.
285
         *
286
         * @return the id
287
         */
288
        public Long getId() {
289
                return id;
290
        }
291

    
292
        /**
293
         * Retrieve the groups specified by this user.
294
         *
295
         * @return the groups
296
         */
297
        public List<Group> getGroupsSpecified() {
298
                return groupsSpecified;
299
        }
300

    
301
        /**
302
         * Modify the groups specified by this user.
303
         *
304
         * @param newGroupsSpecified the groups to set
305
         */
306
        public void setGroupsSpecified(final List<Group> newGroupsSpecified) {
307
                groupsSpecified = newGroupsSpecified;
308
        }
309

    
310
        /**
311
         * Retrieve the groups of which this user is member.
312
         *
313
         * @return the groups
314
         */
315
        public Set<Group> getGroupsMember() {
316
                return groupsMember;
317
        }
318

    
319
        /**
320
         * Modify the groups of which this user is member.
321
         *
322
         * @param newGroupsMember the groups to set
323
         */
324
        public void setGroupsMember(final Set<Group> newGroupsMember) {
325
                groupsMember = newGroupsMember;
326
        }
327

    
328
        /**
329
         * Retrieve the audit info.
330
         *
331
         * @return the audit info
332
         */
333
        public AuditInfo getAuditInfo() {
334
                return auditInfo;
335
        }
336

    
337
        /**
338
         * Modify the audit info.
339
         *
340
         * @param newAuditInfo the new audit info
341
         */
342
        public void setAuditInfo(final AuditInfo newAuditInfo) {
343
                auditInfo = newAuditInfo;
344
        }
345

    
346
        /**
347
         * Retrieve the file tags.
348
         *
349
         * @return a list of file tags
350
         */
351
        public List<FileTag> getFileTags() {
352
                return fileTags;
353
        }
354

    
355
        /**
356
         * Replace the list of file tags.
357
         *
358
         * @param newFileTags the new file tags
359
         */
360
        public void setFileTags(final List<FileTag> newFileTags) {
361
                fileTags = newFileTags;
362
        }
363

    
364
        /**
365
         * Retrieve the user class.
366
         *
367
         * @return the user class
368
         */
369
        public UserClass getUserClass() {
370
                return userClass;
371
        }
372

    
373
        /**
374
         * Modify the user class.
375
         *
376
         * @param newUserClass the new user class
377
         */
378
        public void setUserClass(final UserClass newUserClass) {
379
                userClass = newUserClass;
380
        }
381

    
382
        /**
383
         * Retrieve the username.
384
         *
385
         * @return the username
386
         */
387
        public String getUsername() {
388
                return username;
389
        }
390

    
391
        /**
392
         * Modify the username.
393
         *
394
         * @param aUsername the username to set
395
         */
396
        public void setUsername(String aUsername) {
397
                username = aUsername;
398
        }
399
        
400
        /**
401
         * Retrieve the acceptedPolicy flag.
402
         *
403
         * @return the acceptedPolicy
404
         */
405
        public boolean hasAcceptedPolicy() {
406
                return acceptedPolicy;
407
        }
408
        /**
409
         * Modify the acceptedPolicy flag.
410
         *
411
         * @param newAcceptedPolicy the acceptedPolicy to set
412
         */
413
        public void setAcceptedPolicy(boolean newAcceptedPolicy) {
414
                acceptedPolicy = newAcceptedPolicy;
415
        }
416

    
417

    
418
        public String getWebDAVPassword() {
419
                return webDAVPassword;
420
        }
421
        public void setWebDAVPassword(String aWebDAVPassword1) {
422
                webDAVPassword = aWebDAVPassword1;
423
        }
424

    
425
        // ********************** Business Methods ********************** //
426

    
427

    
428

    
429

    
430
        /**
431
         * Modify the identityProviderId.
432
         *
433
         * @param anIdentityProviderId the identityProviderId to set
434
         */
435
        public void setIdentityProviderId(String anIdentityProviderId) {
436
                identityProviderId = anIdentityProviderId;
437
        }
438

    
439

    
440
        /**
441
         * Modify the identityProvider.
442
         *
443
         * @param anIdentityProvider the identityProvider to set
444
         */
445
        public void setIdentityProvider(String anIdentityProvider) {
446
                identityProvider = anIdentityProvider;
447
        }
448

    
449
        /**
450
         * Retrieve the authentication token. If it is not valid
451
         * or non-existent, this method returns null. Therefore, call
452
         * sites must request a regeneration of the authentication
453
         * token in both cases.
454
         *
455
         * @return the authToken
456
         */
457
        public byte[] getAuthToken() {
458
                if (isAuthTokenValid())
459
                        return authToken;
460
                return null;
461
        }
462

    
463
        /**
464
         * Add a tag from this user to specified file.
465
         *
466
         * @param file the file
467
         * @param tag the tag string
468
         */
469
        public void addTag(final FileHeader file, final String tag) {
470
                @SuppressWarnings("unused")
471
                final FileTag fileTag = new FileTag(this, file, tag);
472
                // Cascade should take care of saving here.
473
        }
474

    
475
        /**
476
         * Return a Data Transfer Object for this User object.
477
         *
478
         * @return a user DTO
479
         */
480
        public UserDTO getDTO() {
481
                final UserDTO u = new UserDTO();
482
                u.setId(id);
483
                u.setName(name);
484
                u.setLastname(lastname);
485
                u.setFirstname(firstname);
486
                u.setEmail(email);
487
                u.setUsername(username);
488
                u.setActive(active);
489
                if(userClass!= null)
490
                        u.setUserClass(userClass.getDTOWithoutUsers());
491
                return u;
492
        }
493

    
494
        /**
495
         * Removes a group from this user's specified groups list.
496
         *
497
         * @param group the group to remove
498
         * @throws IllegalArgumentException if group is null
499
         */
500
        public void removeSpecifiedGroup(final Group group) {
501
                if (group == null)
502
                        throw new IllegalArgumentException("Can't remove a null group.");
503
                getGroupsSpecified().remove(group);
504
                group.setOwner(null);
505
        }
506

    
507
        /**
508
         * @param name2
509
         */
510
        public void createGroup(final String name2) {
511
                final Group group = new Group(name2);
512
                group.setOwner(this);
513
                final Date now = new Date();
514
                final AuditInfo ai = new AuditInfo();
515
                ai.setCreatedBy(this);
516
                ai.setCreationDate(now);
517
                ai.setModifiedBy(this);
518
                ai.setModificationDate(now);
519
                group.setAuditInfo(ai);
520
                groupsSpecified.add(group);
521
        }
522

    
523
        /**
524
         * Removes the specified tag from this user
525
         *
526
         * @param tag
527
         */
528
        public void removeTag(FileTag tag) {
529
                fileTags.remove(tag);
530
                tag.setUser(null);
531
        }
532

    
533
        /**
534
         * Creates a new authentication token and resets
535
         * its expiry date.
536
         */
537
        public void generateAuthToken() {
538
                SecureRandom random = new SecureRandom();
539
                authToken = new byte[TOKEN_SIZE];
540
                random.nextBytes(authToken);
541
                Calendar cal = Calendar.getInstance();
542
                // Set token time-to-live to the number of days specified in
543
                // gss.properties.
544
                cal.add(Calendar.DAY_OF_MONTH, getConfiguration().getInt("tokenTTL", 1));
545
                authTokenExpiryDate = cal.getTime();
546
        }
547

    
548
        /**
549
         * Return true if the authentication token is usable, or false
550
         * if a new one must be regenerated.
551
         *
552
         * @return true if the authentication token is valid
553
         */
554
        private boolean isAuthTokenValid() {
555
                if (authToken == null)
556
                        return false;
557
                if (authTokenExpiryDate == null)
558
                        return false;
559
                if (authTokenExpiryDate.before(new Date()))
560
                        return false;
561
                return true;
562
        }
563

    
564
        /**
565
         * Request the invalidation of the authentication token.
566
         * After this method is called, a new token must be generated.
567
         */
568
        public void invalidateAuthToken() {
569
                authToken = null;
570
                authTokenExpiryDate = null;
571
        }
572

    
573
        /**
574
         * Retrieve the nonce. If it is not valid or non-existent,
575
         * this method returns null.
576
         *
577
         * @return the nonce
578
         */
579
        public String getNonce() {
580
                if (isNonceValid())
581
                        return nonce;
582
                return null;
583
        }
584

    
585
        /**
586
         * Return true if the nonce is usable, or false
587
         * if not.
588
         *
589
         * @return true if the nonce is valid
590
         */
591
        private boolean isNonceValid() {
592
                if (nonce == null)
593
                        return false;
594
                if (nonceExpiryDate == null)
595
                        return false;
596
                if (nonceExpiryDate.before(new Date()))
597
                        return false;
598
                return true;
599
        }
600

    
601
        /**
602
         * Modify the nonce.
603
         *
604
         * @param aNonce the nonce to set
605
         */
606
        public void setNonce(String aNonce) {
607
                nonce = aNonce;
608
        }
609

    
610
        /**
611
         * Modify the nonce expiry date.
612
         *
613
         * @param aNonceExpiryDate the nonce expiry date to set
614
         */
615
        public void setNonceExpiryDate(Date aNonceExpiryDate) {
616
                nonceExpiryDate = aNonceExpiryDate;
617
        }
618

    
619
        public boolean isActive() {
620
                return active;
621
        }
622

    
623
        public void setActive(boolean isActive) {
624
                active = isActive;
625
        }
626

    
627
        @Override
628
        public boolean equals(Object o) {
629
                if (this == o) return true;
630
                if (!(o instanceof User)) return false;
631
                User user = (User) o;
632
                return user.getUsername().equals(username) && user.getName().equals(name);
633
        }
634

    
635
        @Override
636
        public int hashCode() {
637
                return 37 * username.hashCode() + name.hashCode();
638
        }
639

    
640
        public void generateWebDAVPassword() {
641
                Random random = new Random();
642
                StringBuilder sb = new StringBuilder();
643
                int length = allowedPasswordCharacters.length();
644
                for (int i=0; i<PASSWORD_LENGTH; i++) {
645
                        int j = random.nextInt(length);
646
                        sb.append(allowedPasswordCharacters.charAt(j));
647
                }
648
                webDAVPassword = sb.toString();
649
        }
650

    
651
    public String getHomeOrganization() {
652
        return homeOrganization;
653
    }
654

    
655
    public void setHomeOrganization(String homeOrganization) {
656
        this.homeOrganization = homeOrganization;
657
    }
658
}