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
|
"emailAddress",
|
55
|
"grantedAuthorities",
|
56
|
"groups",
|
57
|
"enabled",
|
58
|
"accountNonExpired",
|
59
|
"credentialsNonExpired",
|
60
|
"accountNonLocked",
|
61
|
"person"
|
62
|
})
|
63
|
@XmlRootElement(name = "User")
|
64
|
@Entity
|
65
|
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
|
66
|
//@Indexed(index = "eu.etaxonomy.cdm.model.common.User")
|
67
|
@Audited
|
68
|
@Table(name = "UserAccount")
|
69
|
public class User extends CdmBase implements UserDetails {
|
70
|
private static final long serialVersionUID = 6582191171369439163L;
|
71
|
private static final Logger logger = Logger.getLogger(User.class);
|
72
|
|
73
|
// **************************** FACTORY *****************************************/
|
74
|
|
75
|
public static User NewInstance(String username, String pwd){
|
76
|
User user = new User();
|
77
|
user.setUsername(username);
|
78
|
user.setPassword(pwd);
|
79
|
|
80
|
user.setAccountNonExpired(true);
|
81
|
user.setAccountNonLocked(true);
|
82
|
user.setCredentialsNonExpired(true);
|
83
|
user.setEnabled(true);
|
84
|
|
85
|
return user;
|
86
|
}
|
87
|
|
88
|
public static User NewInstance(String personTitle, String username, String pwd){
|
89
|
User user = new User();
|
90
|
user.setUsername(username);
|
91
|
user.setPassword(pwd);
|
92
|
|
93
|
user.setAccountNonExpired(true);
|
94
|
user.setAccountNonLocked(true);
|
95
|
user.setCredentialsNonExpired(true);
|
96
|
user.setEnabled(true);
|
97
|
Person userPerson = Person.NewTitledInstance(personTitle);
|
98
|
user.setPerson(userPerson);
|
99
|
|
100
|
return user;
|
101
|
}
|
102
|
|
103
|
//***************************** Fields *********************** /
|
104
|
|
105
|
@XmlElement(name = "Username")
|
106
|
@Column(unique = true)
|
107
|
@Field(analyze = Analyze.NO)
|
108
|
@NotNull
|
109
|
protected String username;
|
110
|
|
111
|
/**
|
112
|
* a salted, MD5 encoded hash of the plain text password
|
113
|
*/
|
114
|
@XmlElement(name = "Password")
|
115
|
@NotAudited
|
116
|
protected String password;
|
117
|
|
118
|
@XmlElement(name = "EmailAddress")
|
119
|
protected String emailAddress;
|
120
|
|
121
|
@XmlElementWrapper(name = "GrantedAuthorities")
|
122
|
@XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
|
123
|
@XmlIDREF
|
124
|
@XmlSchemaType(name = "IDREF")
|
125
|
@ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
|
126
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.REFRESH}) // see #2414 (Group updating doesn't work)
|
127
|
@NotAudited
|
128
|
protected Set<GrantedAuthority> grantedAuthorities = new HashSet<>(); //authorities of this user only
|
129
|
|
130
|
@XmlElementWrapper(name = "Groups")
|
131
|
@XmlElement(name = "Group")
|
132
|
@XmlIDREF
|
133
|
@XmlSchemaType(name = "IDREF")
|
134
|
@ManyToMany(fetch = FetchType.LAZY)
|
135
|
@Cascade({CascadeType.REFRESH, CascadeType.SAVE_UPDATE,CascadeType.MERGE}) // see #2414 (Group updating doesn't work)
|
136
|
@IndexedEmbedded(depth = 1)
|
137
|
@NotAudited
|
138
|
protected Set<Group> groups = new HashSet<>();
|
139
|
|
140
|
@XmlElement(name = "Enabled")
|
141
|
protected boolean enabled;
|
142
|
|
143
|
@XmlElement(name = "AccountNonExpired")
|
144
|
protected boolean accountNonExpired;
|
145
|
|
146
|
@XmlElement(name = "CredentialsNonExpired")
|
147
|
protected boolean credentialsNonExpired;
|
148
|
|
149
|
@XmlElement(name = "AccountNonLocked")
|
150
|
protected boolean accountNonLocked;
|
151
|
|
152
|
@XmlElement(name = "Person")
|
153
|
@XmlIDREF
|
154
|
@XmlSchemaType(name = "IDREF")
|
155
|
@OneToOne(fetch = FetchType.LAZY)
|
156
|
@Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
|
157
|
@IndexedEmbedded(depth = 1)
|
158
|
protected Person person;
|
159
|
|
160
|
@XmlTransient
|
161
|
@Transient
|
162
|
private Set<GrantedAuthority> authorities; //authorities of this user and of all groups the user belongs to
|
163
|
|
164
|
//***************************** Constructor *********************** /
|
165
|
|
166
|
protected User(){
|
167
|
super();
|
168
|
}
|
169
|
|
170
|
// ***************************** METHODS ******************************/
|
171
|
|
172
|
/**
|
173
|
* Initializes or refreshes the collection of authorities, See
|
174
|
* {@link #getAuthorities()}
|
175
|
*/
|
176
|
//FIXME made public as preliminary solution to #4053 (Transient field User.authorities not refreshed on reloading entity)
|
177
|
public void initAuthorities() {
|
178
|
authorities = new HashSet<>();
|
179
|
authorities.addAll(grantedAuthorities);
|
180
|
for(Group group : groups) {
|
181
|
authorities.addAll(group.getGrantedAuthorities());
|
182
|
}
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* Implementation of {@link UserDetails#getAuthorities()}
|
187
|
*
|
188
|
* {@inheritDoc}
|
189
|
*
|
190
|
* @return returns all {@code Set<GrantedAuthority>} instances contained in
|
191
|
* the sets {@link #getGrantedAuthorities()} and
|
192
|
* {@link #getGroups()}
|
193
|
*/
|
194
|
@Override
|
195
|
@Transient
|
196
|
public Collection<GrantedAuthority> getAuthorities() {
|
197
|
if(authorities == null || authorities.size() == 0) {
|
198
|
initAuthorities();
|
199
|
}
|
200
|
return authorities;
|
201
|
}
|
202
|
|
203
|
@Override
|
204
|
public String getPassword() {
|
205
|
return password;
|
206
|
}
|
207
|
|
208
|
@Override
|
209
|
public String getUsername() {
|
210
|
return username;
|
211
|
}
|
212
|
|
213
|
@Override
|
214
|
public boolean isAccountNonExpired() {
|
215
|
return accountNonExpired;
|
216
|
}
|
217
|
|
218
|
@Override
|
219
|
public boolean isAccountNonLocked() {
|
220
|
return accountNonLocked;
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public boolean isCredentialsNonExpired() {
|
225
|
return credentialsNonExpired;
|
226
|
}
|
227
|
|
228
|
@Override
|
229
|
public boolean isEnabled() {
|
230
|
return enabled;
|
231
|
}
|
232
|
|
233
|
public String getEmailAddress() {
|
234
|
return emailAddress;
|
235
|
}
|
236
|
|
237
|
public void setEmailAddress(String emailAddress) {
|
238
|
this.emailAddress = emailAddress;
|
239
|
}
|
240
|
|
241
|
public Set<GrantedAuthority> getGrantedAuthorities() {
|
242
|
return grantedAuthorities;
|
243
|
}
|
244
|
|
245
|
public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
|
246
|
this.grantedAuthorities = grantedAuthorities;
|
247
|
initAuthorities();
|
248
|
}
|
249
|
|
250
|
public void setUsername(String username) {
|
251
|
this.username = username;
|
252
|
}
|
253
|
|
254
|
public void setPassword(String password) {
|
255
|
this.password = password;
|
256
|
}
|
257
|
|
258
|
public void setEnabled(boolean enabled) {
|
259
|
this.enabled = enabled;
|
260
|
}
|
261
|
|
262
|
public void setAccountNonExpired(boolean accountNonExpired) {
|
263
|
this.accountNonExpired = accountNonExpired;
|
264
|
}
|
265
|
|
266
|
public void setCredentialsNonExpired(boolean credentialsNonExpired) {
|
267
|
this.credentialsNonExpired = credentialsNonExpired;
|
268
|
}
|
269
|
|
270
|
public void setAccountNonLocked(boolean accountNonLocked) {
|
271
|
this.accountNonLocked = accountNonLocked;
|
272
|
}
|
273
|
|
274
|
protected void setGroups(Set<Group> groups) {
|
275
|
this.groups = groups;
|
276
|
initAuthorities();
|
277
|
}
|
278
|
|
279
|
public Set<Group> getGroups() {
|
280
|
return groups;
|
281
|
}
|
282
|
|
283
|
|
284
|
public Person getPerson() {
|
285
|
return person;
|
286
|
}
|
287
|
|
288
|
public void setPerson(Person person) {
|
289
|
this.person = person;
|
290
|
}
|
291
|
|
292
|
public static User getCurrentAuthenticatedUser() {
|
293
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
294
|
if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
|
295
|
return (User)authentication.getPrincipal();
|
296
|
}
|
297
|
return null;
|
298
|
}
|
299
|
|
300
|
//*********************** CLONE ********************************************************/
|
301
|
|
302
|
/**
|
303
|
* Clones <i>this</i> User. This is a shortcut that enables to create
|
304
|
* a new instance that differs only slightly from <i>this</i> User.
|
305
|
* The corresponding person is cloned.
|
306
|
*
|
307
|
* @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
|
308
|
* @see java.lang.Object#clone()
|
309
|
*/
|
310
|
@Override
|
311
|
public Object clone() {
|
312
|
try{
|
313
|
User result = (User)super.clone();
|
314
|
result.setPerson((Person)this.person.clone());
|
315
|
return result;
|
316
|
} catch (CloneNotSupportedException e){
|
317
|
logger.warn("Object does not implement cloneable");
|
318
|
e.printStackTrace();
|
319
|
return null;
|
320
|
}
|
321
|
|
322
|
|
323
|
}
|
324
|
|
325
|
//************************************** toString ***************************************
|
326
|
|
327
|
@Override
|
328
|
public String toString() {
|
329
|
if (StringUtils.isNotBlank(username)){
|
330
|
return username;
|
331
|
}else{
|
332
|
return super.toString();
|
333
|
}
|
334
|
}
|
335
|
}
|