Project

General

Profile

Download (9.3 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.Indexed;
42
import org.hibernate.search.annotations.IndexedEmbedded;
43
import org.springframework.security.core.GrantedAuthority;
44
import org.springframework.security.core.userdetails.UserDetails;
45

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

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

    
70
 // **************************** FACTORY *****************************************/   
71
    
72
    public static User NewInstance(String username, String pwd){
73
        User user = new User();
74
        user.setUsername(username);
75
        user.setPassword(pwd);
76

    
77
        user.setAccountNonExpired(true);
78
        user.setAccountNonLocked(true);
79
        user.setCredentialsNonExpired(true);
80
        user.setEnabled(true);
81

    
82
        return user;
83
    }
84

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

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

    
97
        return user;
98
    }
99

    
100
//***************************** Fields *********************** /
101
    
102
    @XmlElement(name = "Username")
103
    @Column(unique = true)
104
    @Field(analyze = Analyze.NO)
105
    @NotNull
106
    protected String username;
107

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

    
115
    @XmlElement(name = "EmailAddress")
116
    protected String emailAddress;
117

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

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

    
137
    @XmlElement(name = "Enabled")
138
    protected boolean enabled;
139

    
140
    @XmlElement(name = "AccountNonExpired")
141
    protected boolean accountNonExpired;
142

    
143
    @XmlElement(name = "CredentialsNonExpired")
144
    protected boolean credentialsNonExpired;
145

    
146
    @XmlElement(name = "AccountNonLocked")
147
    protected boolean accountNonLocked;
148

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

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

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

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

    
200
    @Override
201
    public String getPassword() {
202
        return password;
203
    }
204

    
205
    @Override
206
    public String getUsername() {
207
        return username;
208
    }
209

    
210
    @Override
211
    public boolean isAccountNonExpired() {
212
        return accountNonExpired;
213
    }
214

    
215
    @Override
216
    public boolean isAccountNonLocked() {
217
        return accountNonLocked;
218
    }
219

    
220
    @Override
221
    public boolean isCredentialsNonExpired() {
222
        return credentialsNonExpired;
223
    }
224

    
225
    @Override
226
    public boolean isEnabled() {
227
        return enabled;
228
    }
229

    
230
    public String getEmailAddress() {
231
        return emailAddress;
232
    }
233

    
234
    public void setEmailAddress(String emailAddress) {
235
        this.emailAddress = emailAddress;
236
    }
237

    
238
    public Set<GrantedAuthority> getGrantedAuthorities() {
239
        return grantedAuthorities;
240
    }
241

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

    
247
    public void setUsername(String username) {
248
        this.username = username;
249
    }
250

    
251
    public void setPassword(String password) {
252
        this.password = password;
253
    }
254

    
255
    public void setEnabled(boolean enabled) {
256
        this.enabled = enabled;
257
    }
258

    
259
    public void setAccountNonExpired(boolean accountNonExpired) {
260
        this.accountNonExpired = accountNonExpired;
261
    }
262

    
263
    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
264
        this.credentialsNonExpired = credentialsNonExpired;
265
    }
266

    
267
    public void setAccountNonLocked(boolean accountNonLocked) {
268
        this.accountNonLocked = accountNonLocked;
269
    }
270

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

    
276
    public Set<Group> getGroups() {
277
        return groups;
278
    }
279

    
280

    
281
    public Person getPerson() {
282
        return person;
283
    }
284

    
285
    public void setPerson(Person person) {
286
        this.person = person;
287
    }
288

    
289
//*********************** CLONE ********************************************************/
290

    
291
    /**
292
     * Clones <i>this</i> User. This is a shortcut that enables to create
293
     * a new instance that differs only slightly from <i>this</i> User.
294
     * The corresponding person is cloned.
295
     *
296
     * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
297
     * @see java.lang.Object#clone()
298
     */
299
    @Override
300
    public Object clone() {
301
        try{
302
            User result = (User)super.clone();
303
            result.setPerson((Person)this.person.clone());
304
            return result;
305
        } catch (CloneNotSupportedException e){
306
            logger.warn("Object does not implement cloneable");
307
            e.printStackTrace();
308
            return null;
309
        }
310

    
311

    
312
    }
313
}
(67-67/72)