Project

General

Profile

Download (9.51 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.log4j.Logger;
35
import org.hibernate.annotations.Cascade;
36
import org.hibernate.annotations.CascadeType;
37
import org.hibernate.envers.Audited;
38
import org.hibernate.envers.NotAudited;
39
import org.hibernate.search.annotations.Analyze;
40
import org.hibernate.search.annotations.Field;
41
import org.hibernate.search.annotations.IndexedEmbedded;
42
import org.springframework.security.core.Authentication;
43
import org.springframework.security.core.GrantedAuthority;
44
import org.springframework.security.core.context.SecurityContextHolder;
45
import org.springframework.security.core.userdetails.UserDetails;
46

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

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

    
72
 // **************************** FACTORY *****************************************/
73

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

    
79
        user.setAccountNonExpired(true);
80
        user.setAccountNonLocked(true);
81
        user.setCredentialsNonExpired(true);
82
        user.setEnabled(true);
83

    
84
        return user;
85
    }
86

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

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

    
99
        return user;
100
    }
101

    
102
//***************************** Fields *********************** /
103

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

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

    
117
    @XmlElement(name = "EmailAddress")
118
    protected String emailAddress;
119

    
120
    @XmlElementWrapper(name = "GrantedAuthorities")
121
    @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
122
    @XmlIDREF
123
    @XmlSchemaType(name = "IDREF")
124
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
125
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.REFRESH}) // see #2414 (Group updating doesn't work)
126
    @NotAudited
127
    protected Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();  //authorities of this user only
128

    
129
    @XmlElementWrapper(name = "Groups")
130
    @XmlElement(name = "Group")
131
    @XmlIDREF
132
    @XmlSchemaType(name = "IDREF")
133
    @ManyToMany(fetch = FetchType.LAZY)
134
        @Cascade({CascadeType.REFRESH, CascadeType.SAVE_UPDATE,CascadeType.MERGE}) // see #2414 (Group updating doesn't work)
135
    @IndexedEmbedded(depth = 1)
136
    @NotAudited
137
    protected Set<Group> groups = new HashSet<Group>();
138

    
139
    @XmlElement(name = "Enabled")
140
    protected boolean enabled;
141

    
142
    @XmlElement(name = "AccountNonExpired")
143
    protected boolean accountNonExpired;
144

    
145
    @XmlElement(name = "CredentialsNonExpired")
146
    protected boolean credentialsNonExpired;
147

    
148
    @XmlElement(name = "AccountNonLocked")
149
    protected boolean accountNonLocked;
150

    
151
    @XmlElement(name = "Person")
152
    @XmlIDREF
153
    @XmlSchemaType(name = "IDREF")
154
    @OneToOne(fetch = FetchType.LAZY)
155
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
156
    @IndexedEmbedded(depth = 1)
157
    protected Person person;
158

    
159
    @XmlTransient
160
    @Transient
161
    private Set<GrantedAuthority> authorities;  //authorities of this user and of all groups the user belongs to
162

    
163
//***************************** Constructor *********************** /
164

    
165
    protected User(){
166
        super();
167
    }
168

    
169
// ***************************** METHODS ******************************/
170

    
171
    /**
172
     * Initializes or refreshes the collection of authorities, See
173
     * {@link #getAuthorities()}
174
     */
175
    //FIXME made public as preliminary solution to #4053 (Transient field User.authorities not refreshed on reloading entity)
176
    public void initAuthorities() {
177
        authorities = new HashSet<GrantedAuthority>();
178
        authorities.addAll(grantedAuthorities);
179
        for(Group group : groups) {
180
            authorities.addAll(group.getGrantedAuthorities());
181
        }
182
    }
183

    
184
    /**
185
     * Implementation of {@link UserDetails#getAuthorities()}
186
     *
187
     * {@inheritDoc}
188
     *
189
     * @return returns all {@code Set<GrantedAuthority>} instances contained in
190
     *         the sets {@link #getGrantedAuthorities()} and
191
     *         {@link #getGroups()}
192
     */
193
    @Override
194
    @Transient
195
    public Collection<GrantedAuthority> getAuthorities() {
196
        if(authorities == null || authorities.size() == 0) {
197
            initAuthorities();
198
        }
199
        return authorities;
200
    }
201

    
202
    @Override
203
    public String getPassword() {
204
        return password;
205
    }
206

    
207
    @Override
208
    public String getUsername() {
209
        return username;
210
    }
211

    
212
    @Override
213
    public boolean isAccountNonExpired() {
214
        return accountNonExpired;
215
    }
216

    
217
    @Override
218
    public boolean isAccountNonLocked() {
219
        return accountNonLocked;
220
    }
221

    
222
    @Override
223
    public boolean isCredentialsNonExpired() {
224
        return credentialsNonExpired;
225
    }
226

    
227
    @Override
228
    public boolean isEnabled() {
229
        return enabled;
230
    }
231

    
232
    public String getEmailAddress() {
233
        return emailAddress;
234
    }
235

    
236
    public void setEmailAddress(String emailAddress) {
237
        this.emailAddress = emailAddress;
238
    }
239

    
240
    public Set<GrantedAuthority> getGrantedAuthorities() {
241
        return grantedAuthorities;
242
    }
243

    
244
    public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
245
        this.grantedAuthorities = grantedAuthorities;
246
        initAuthorities();
247
    }
248

    
249
    public void setUsername(String username) {
250
        this.username = username;
251
    }
252

    
253
    public void setPassword(String password) {
254
        this.password = password;
255
    }
256

    
257
    public void setEnabled(boolean enabled) {
258
        this.enabled = enabled;
259
    }
260

    
261
    public void setAccountNonExpired(boolean accountNonExpired) {
262
        this.accountNonExpired = accountNonExpired;
263
    }
264

    
265
    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
266
        this.credentialsNonExpired = credentialsNonExpired;
267
    }
268

    
269
    public void setAccountNonLocked(boolean accountNonLocked) {
270
        this.accountNonLocked = accountNonLocked;
271
    }
272

    
273
    protected void setGroups(Set<Group> groups) {
274
        this.groups = groups;
275
        initAuthorities();
276
    }
277

    
278
    public Set<Group> getGroups() {
279
        return groups;
280
    }
281

    
282

    
283
    public Person getPerson() {
284
        return person;
285
    }
286

    
287
    public void setPerson(Person person) {
288
        this.person = person;
289
    }
290

    
291
    public static User getCurrentAuthenticatedUser() {
292
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
293
        if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
294
            return (User)authentication.getPrincipal();
295
        }
296
        return null;
297
    }
298

    
299
//*********************** CLONE ********************************************************/
300

    
301
    /**
302
     * Clones <i>this</i> User. This is a shortcut that enables to create
303
     * a new instance that differs only slightly from <i>this</i> User.
304
     * The corresponding person is cloned.
305
     *
306
     * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
307
     * @see java.lang.Object#clone()
308
     */
309
    @Override
310
    public Object clone() {
311
        try{
312
            User result = (User)super.clone();
313
            result.setPerson((Person)this.person.clone());
314
            return result;
315
        } catch (CloneNotSupportedException e){
316
            logger.warn("Object does not implement cloneable");
317
            e.printStackTrace();
318
            return null;
319
        }
320

    
321

    
322
    }
323
}
(68-68/73)