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.HashSet;
|
13
|
import java.util.Set;
|
14
|
import java.util.TreeSet;
|
15
|
|
16
|
import javax.persistence.Entity;
|
17
|
import javax.persistence.FetchType;
|
18
|
import javax.persistence.ManyToMany;
|
19
|
import javax.persistence.OneToOne;
|
20
|
import javax.persistence.Table;
|
21
|
import javax.persistence.Transient;
|
22
|
import javax.xml.bind.annotation.XmlAccessType;
|
23
|
import javax.xml.bind.annotation.XmlAccessorType;
|
24
|
import javax.xml.bind.annotation.XmlElement;
|
25
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
26
|
import javax.xml.bind.annotation.XmlIDREF;
|
27
|
import javax.xml.bind.annotation.XmlRootElement;
|
28
|
import javax.xml.bind.annotation.XmlSchemaType;
|
29
|
import javax.xml.bind.annotation.XmlTransient;
|
30
|
import javax.xml.bind.annotation.XmlType;
|
31
|
|
32
|
import org.apache.log4j.Logger;
|
33
|
import org.hibernate.annotations.Cascade;
|
34
|
import org.hibernate.annotations.CascadeType;
|
35
|
import org.hibernate.annotations.FetchMode;
|
36
|
import org.hibernate.annotations.Fetch;
|
37
|
import org.hibernate.annotations.NaturalId;
|
38
|
import org.hibernate.envers.Audited;
|
39
|
import org.hibernate.envers.NotAudited;
|
40
|
|
41
|
|
42
|
import org.hibernate.search.annotations.Field;
|
43
|
import org.hibernate.search.annotations.Index;
|
44
|
import org.hibernate.search.annotations.Indexed;
|
45
|
import org.hibernate.search.annotations.IndexedEmbedded;
|
46
|
import org.springframework.security.core.GrantedAuthority;
|
47
|
import org.springframework.security.core.userdetails.UserDetails;
|
48
|
|
49
|
import eu.etaxonomy.cdm.model.agent.Person;
|
50
|
|
51
|
@XmlAccessorType(XmlAccessType.FIELD)
|
52
|
@XmlType(name = "User", propOrder = {
|
53
|
"username",
|
54
|
"password",
|
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(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
|
@SuppressWarnings(value="unused")
|
72
|
private static final Logger logger = Logger.getLogger(User.class);
|
73
|
|
74
|
protected User(){
|
75
|
super();
|
76
|
}
|
77
|
|
78
|
public static User NewInstance(String username, String pwd){
|
79
|
User user = new User();
|
80
|
user.setUsername(username);
|
81
|
user.setPassword(pwd);
|
82
|
|
83
|
user.setAccountNonExpired(true);
|
84
|
user.setAccountNonLocked(true);
|
85
|
user.setCredentialsNonExpired(true);
|
86
|
user.setEnabled(true);
|
87
|
|
88
|
return user;
|
89
|
}
|
90
|
|
91
|
@XmlElement(name = "Username")
|
92
|
@NaturalId
|
93
|
@Field(index = Index.UN_TOKENIZED)
|
94
|
protected String username;
|
95
|
|
96
|
/**
|
97
|
* a salted, MD5 encoded hash of the plaintext password
|
98
|
*/
|
99
|
@XmlElement(name = "Password")
|
100
|
@NotAudited
|
101
|
protected String password;
|
102
|
|
103
|
@XmlElement(name = "EmailAddress")
|
104
|
protected String emailAddress;
|
105
|
|
106
|
@XmlElementWrapper(name = "GrantedAuthorities")
|
107
|
@XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
|
108
|
@XmlIDREF
|
109
|
@XmlSchemaType(name = "IDREF")
|
110
|
@ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
|
111
|
@NotAudited
|
112
|
protected Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
|
113
|
|
114
|
@XmlElementWrapper(name = "Groups")
|
115
|
@XmlElement(name = "Group")
|
116
|
@XmlIDREF
|
117
|
@XmlSchemaType(name = "IDREF")
|
118
|
@ManyToMany(fetch = FetchType.LAZY)
|
119
|
@IndexedEmbedded(depth = 1)
|
120
|
@NotAudited
|
121
|
protected Set<Group> groups = new HashSet<Group>();
|
122
|
|
123
|
@XmlElement(name = "Enabled")
|
124
|
protected boolean enabled;
|
125
|
|
126
|
@XmlElement(name = "AccountNonExpired")
|
127
|
protected boolean accountNonExpired;
|
128
|
|
129
|
@XmlElement(name = "CredentialsNonExpired")
|
130
|
protected boolean credentialsNonExpired;
|
131
|
|
132
|
@XmlElement(name = "AccountNonLocked")
|
133
|
protected boolean accountNonLocked;
|
134
|
|
135
|
@XmlElement(name = "Person")
|
136
|
@XmlIDREF
|
137
|
@XmlSchemaType(name = "IDREF")
|
138
|
@OneToOne(fetch = FetchType.LAZY)
|
139
|
@Cascade({CascadeType.SAVE_UPDATE})
|
140
|
@IndexedEmbedded(depth = 1)
|
141
|
protected Person person;
|
142
|
|
143
|
@XmlTransient
|
144
|
@Transient
|
145
|
private Set<GrantedAuthority> authorities;
|
146
|
|
147
|
private void initAuthorities() {
|
148
|
authorities = new HashSet<GrantedAuthority>();
|
149
|
authorities.addAll(grantedAuthorities);
|
150
|
for(Group group : groups) {
|
151
|
authorities.addAll(group.getGrantedAuthorities());
|
152
|
}
|
153
|
}
|
154
|
|
155
|
@Transient
|
156
|
public Set<GrantedAuthority> getAuthorities() {
|
157
|
if(authorities == null) initAuthorities();
|
158
|
return authorities;
|
159
|
}
|
160
|
|
161
|
public String getPassword() {
|
162
|
return password;
|
163
|
}
|
164
|
|
165
|
public String getUsername() {
|
166
|
return username;
|
167
|
}
|
168
|
|
169
|
public boolean isAccountNonExpired() {
|
170
|
return accountNonExpired;
|
171
|
}
|
172
|
|
173
|
public boolean isAccountNonLocked() {
|
174
|
return accountNonLocked;
|
175
|
}
|
176
|
|
177
|
public boolean isCredentialsNonExpired() {
|
178
|
return credentialsNonExpired;
|
179
|
}
|
180
|
|
181
|
public boolean isEnabled() {
|
182
|
return enabled;
|
183
|
}
|
184
|
|
185
|
public String getEmailAddress() {
|
186
|
return emailAddress;
|
187
|
}
|
188
|
|
189
|
public void setEmailAddress(String emailAddress) {
|
190
|
this.emailAddress = emailAddress;
|
191
|
}
|
192
|
|
193
|
public Set<GrantedAuthority> getGrantedAuthorities() {
|
194
|
return grantedAuthorities;
|
195
|
}
|
196
|
|
197
|
public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
|
198
|
this.grantedAuthorities = grantedAuthorities;
|
199
|
initAuthorities();
|
200
|
}
|
201
|
|
202
|
public void setUsername(String username) {
|
203
|
this.username = username;
|
204
|
}
|
205
|
|
206
|
public void setPassword(String password) {
|
207
|
this.password = password;
|
208
|
}
|
209
|
|
210
|
public void setEnabled(boolean enabled) {
|
211
|
this.enabled = enabled;
|
212
|
}
|
213
|
|
214
|
public void setAccountNonExpired(boolean accountNonExpired) {
|
215
|
this.accountNonExpired = accountNonExpired;
|
216
|
}
|
217
|
|
218
|
public void setCredentialsNonExpired(boolean credentialsNonExpired) {
|
219
|
this.credentialsNonExpired = credentialsNonExpired;
|
220
|
}
|
221
|
|
222
|
public void setAccountNonLocked(boolean accountNonLocked) {
|
223
|
this.accountNonLocked = accountNonLocked;
|
224
|
}
|
225
|
|
226
|
protected void setGroups(Set<Group> groups) {
|
227
|
this.groups = groups;
|
228
|
initAuthorities();
|
229
|
}
|
230
|
|
231
|
public Set<Group> getGroups() {
|
232
|
return groups;
|
233
|
}
|
234
|
|
235
|
|
236
|
public Person getPerson() {
|
237
|
return person;
|
238
|
}
|
239
|
|
240
|
public void setPerson(Person person) {
|
241
|
this.person = person;
|
242
|
}
|
243
|
}
|