Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.model.common;
11

    
12
import java.util.Collection;
13
import java.util.HashSet;
14
import java.util.Set;
15

    
16
import javax.persistence.Column;
17
import javax.persistence.Entity;
18
import javax.persistence.FetchType;
19
import javax.persistence.ManyToMany;
20
import javax.persistence.OneToOne;
21
import javax.persistence.Table;
22
import javax.persistence.Transient;
23
import javax.validation.constraints.NotNull;
24
import javax.xml.bind.annotation.XmlAccessType;
25
import javax.xml.bind.annotation.XmlAccessorType;
26
import javax.xml.bind.annotation.XmlElement;
27
import javax.xml.bind.annotation.XmlElementWrapper;
28
import javax.xml.bind.annotation.XmlIDREF;
29
import javax.xml.bind.annotation.XmlRootElement;
30
import javax.xml.bind.annotation.XmlSchemaType;
31
import javax.xml.bind.annotation.XmlTransient;
32
import javax.xml.bind.annotation.XmlType;
33

    
34
import org.apache.commons.lang3.StringUtils;
35
import org.apache.log4j.Logger;
36
import org.hibernate.annotations.Cascade;
37
import org.hibernate.annotations.CascadeType;
38
import org.hibernate.envers.Audited;
39
import org.hibernate.envers.NotAudited;
40
import org.hibernate.search.annotations.Analyze;
41
import org.hibernate.search.annotations.Field;
42
import org.hibernate.search.annotations.IndexedEmbedded;
43
import org.springframework.security.core.Authentication;
44
import org.springframework.security.core.GrantedAuthority;
45
import org.springframework.security.core.context.SecurityContextHolder;
46
import org.springframework.security.core.userdetails.UserDetails;
47

    
48
import eu.etaxonomy.cdm.model.agent.Person;
49

    
50
@XmlAccessorType(XmlAccessType.FIELD)
51
@XmlType(name = "User", propOrder = {
52
    "username",
53
    "password",
54
    "salt",
55
    "emailAddress",
56
    "grantedAuthorities",
57
    "groups",
58
    "enabled",
59
    "accountNonExpired",
60
    "credentialsNonExpired",
61
    "accountNonLocked",
62
    "person"
63
})
64
@XmlRootElement(name = "User")
65
@Entity
66
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
67
//@Indexed(index = "eu.etaxonomy.cdm.model.common.User")
68
@Audited
69
@Table(name = "UserAccount")
70
public class User extends CdmBase implements UserDetails {
71
    private static final long serialVersionUID = 6582191171369439163L;
72
    private static final Logger logger = Logger.getLogger(User.class);
73

    
74
 // **************************** FACTORY *****************************************/
75

    
76
    public static User NewInstance(String username, String pwd){
77
        User user = new User();
78
        user.setUsername(username);
79
        user.setPassword(pwd);
80

    
81
        user.setAccountNonExpired(true);
82
        user.setAccountNonLocked(true);
83
        user.setCredentialsNonExpired(true);
84
        user.setEnabled(true);
85

    
86
        return user;
87
    }
88

    
89
    public static User NewInstance(String personTitle, String username, String pwd){
90
        User user = new User();
91
        user.setUsername(username);
92
        user.setPassword(pwd);
93

    
94
        user.setAccountNonExpired(true);
95
        user.setAccountNonLocked(true);
96
        user.setCredentialsNonExpired(true);
97
        user.setEnabled(true);
98
        Person userPerson = Person.NewTitledInstance(personTitle);
99
        user.setPerson(userPerson);
100

    
101
        return user;
102
    }
103

    
104
//***************************** Fields *********************** /
105

    
106
    @XmlElement(name = "Username")
107
    @Column(unique = true)
108
    @Field(analyze = Analyze.NO)
109
    @NotNull
110
    protected String username;
111

    
112
    /**
113
     * a salted, MD5 encoded hash of the plain text password
114
     */
115
    @XmlElement(name = "Password")
116
    @NotAudited
117
    protected String password;
118

    
119

    
120
    /**
121
     * The salt for password hashing.
122
     * @see https://dev.e-taxonomy.eu/redmine/issues/7210
123
     * @see https://code-bude.net/2015/03/30/grundlagen-sicheres-passwort-hashing-mit-salts/
124
     */
125
    @XmlElement(name = "Salt")
126
    @NotAudited
127
    protected String salt;
128

    
129
    @XmlElement(name = "EmailAddress")
130
    protected String emailAddress;
131

    
132
    @XmlElementWrapper(name = "GrantedAuthorities")
133
    @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
134
    @XmlIDREF
135
    @XmlSchemaType(name = "IDREF")
136
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
137
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.REFRESH}) // see #2414 (Group updating doesn't work)
138
    @NotAudited
139
    protected Set<GrantedAuthority> grantedAuthorities = new HashSet<>();  //authorities of this user only
140

    
141
    @XmlElementWrapper(name = "Groups")
142
    @XmlElement(name = "Group")
143
    @XmlIDREF
144
    @XmlSchemaType(name = "IDREF")
145
    @ManyToMany(fetch = FetchType.LAZY)
146
        @Cascade({CascadeType.REFRESH, CascadeType.SAVE_UPDATE,CascadeType.MERGE}) // see #2414 (Group updating doesn't work)
147
    @IndexedEmbedded(depth = 1)
148
    @NotAudited
149
    protected Set<Group> groups = new HashSet<>();
150

    
151
    @XmlElement(name = "Enabled")
152
    protected boolean enabled;
153

    
154
    @XmlElement(name = "AccountNonExpired")
155
    protected boolean accountNonExpired;
156

    
157
    @XmlElement(name = "CredentialsNonExpired")
158
    protected boolean credentialsNonExpired;
159

    
160
    @XmlElement(name = "AccountNonLocked")
161
    protected boolean accountNonLocked;
162

    
163
    @XmlElement(name = "Person")
164
    @XmlIDREF
165
    @XmlSchemaType(name = "IDREF")
166
    @OneToOne(fetch = FetchType.LAZY)
167
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
168
    @IndexedEmbedded(depth = 1)
169
    protected Person person;
170

    
171
    @XmlTransient
172
    @Transient
173
    private Set<GrantedAuthority> authorities;  //authorities of this user and of all groups the user belongs to
174

    
175
//***************************** Constructor *********************** /
176

    
177
    protected User(){
178
        super();
179
    }
180

    
181
// ***************************** METHODS ******************************/
182

    
183
    /**
184
     * Initializes or refreshes the collection of authorities, See
185
     * {@link #getAuthorities()}
186
     */
187
    //FIXME made public as preliminary solution to #4053 (Transient field User.authorities not refreshed on reloading entity)
188
    public void initAuthorities() {
189
        authorities = new HashSet<>();
190
        authorities.addAll(grantedAuthorities);
191
        for(Group group : groups) {
192
            authorities.addAll(group.getGrantedAuthorities());
193
        }
194
    }
195

    
196
    /**
197
     * Implementation of {@link UserDetails#getAuthorities()}
198
     *
199
     * {@inheritDoc}
200
     *
201
     * @return returns all {@code Set<GrantedAuthority>} instances contained in
202
     *         the sets {@link #getGrantedAuthorities()} and
203
     *         {@link #getGroups()}
204
     */
205
    @Override
206
    @Transient
207
    public Collection<GrantedAuthority> getAuthorities() {
208
        if(authorities == null || authorities.size() == 0) {
209
            initAuthorities();
210
        }
211
        return authorities;
212
    }
213

    
214
    @Override
215
    public String getPassword() {
216
        return password;
217
    }
218

    
219
    @Override
220
    public String getUsername() {
221
        return username;
222
    }
223

    
224
    @Override
225
    public boolean isAccountNonExpired() {
226
        return accountNonExpired;
227
    }
228

    
229
    @Override
230
    public boolean isAccountNonLocked() {
231
        return accountNonLocked;
232
    }
233

    
234
    @Override
235
    public boolean isCredentialsNonExpired() {
236
        return credentialsNonExpired;
237
    }
238

    
239
    @Override
240
    public boolean isEnabled() {
241
        return enabled;
242
    }
243

    
244
    public String getEmailAddress() {
245
        return emailAddress;
246
    }
247

    
248
    public void setEmailAddress(String emailAddress) {
249
        this.emailAddress = emailAddress;
250
    }
251

    
252
    public Set<GrantedAuthority> getGrantedAuthorities() {
253
        return grantedAuthorities;
254
    }
255

    
256
    public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
257
        this.grantedAuthorities = grantedAuthorities;
258
        initAuthorities();
259
    }
260

    
261
    public void setUsername(String username) {
262
        this.username = username;
263
    }
264

    
265
    public void setPassword(String password) {
266
        this.password = password;
267
    }
268

    
269
    public void setEnabled(boolean enabled) {
270
        this.enabled = enabled;
271
    }
272

    
273
    public void setAccountNonExpired(boolean accountNonExpired) {
274
        this.accountNonExpired = accountNonExpired;
275
    }
276

    
277
    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
278
        this.credentialsNonExpired = credentialsNonExpired;
279
    }
280

    
281
    public void setAccountNonLocked(boolean accountNonLocked) {
282
        this.accountNonLocked = accountNonLocked;
283
    }
284

    
285
    protected void setGroups(Set<Group> groups) {
286
        this.groups = groups;
287
        initAuthorities();
288
    }
289

    
290
    public Set<Group> getGroups() {
291
        return groups;
292
    }
293

    
294

    
295
    public Person getPerson() {
296
        return person;
297
    }
298

    
299
    public void setPerson(Person person) {
300
        this.person = person;
301
    }
302

    
303
    public static User getCurrentAuthenticatedUser() {
304
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
305
        if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
306
            return (User)authentication.getPrincipal();
307
        }
308
        return null;
309
    }
310

    
311
//*********************** CLONE ********************************************************/
312

    
313
    /**
314
     * Clones <i>this</i> User. This is a shortcut that enables to create
315
     * a new instance that differs only slightly from <i>this</i> User.
316
     * The corresponding person is cloned.
317
     *
318
     * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
319
     * @see java.lang.Object#clone()
320
     */
321
    @Override
322
    public Object clone() {
323
        try{
324
            User result = (User)super.clone();
325
            result.setPerson((Person)this.person.clone());
326
            return result;
327
        } catch (CloneNotSupportedException e){
328
            logger.warn("Object does not implement cloneable");
329
            e.printStackTrace();
330
            return null;
331
        }
332

    
333

    
334
    }
335

    
336
//************************************** toString ***************************************
337

    
338
    @Override
339
    public String toString() {
340
        if (StringUtils.isNotBlank(username)){
341
            return username;
342
        }else{
343
            return super.toString();
344
        }
345
    }
346
}
(77-77/83)