Project

General

Profile

Download (6.26 KB) Statistics
| Branch: | Tag: | Revision:
1 2faf407b Andreas Müller
/**
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 76d5535d ben.clark
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 903a851d ben.clark
import javax.persistence.OneToOne;
20 4208745a ben.clark
import javax.persistence.Table;
21 76d5535d ben.clark
import javax.persistence.Transient;
22 903a851d ben.clark
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 76d5535d ben.clark
32 5644153a Andreas Müller
import org.apache.log4j.Logger;
33 4208745a ben.clark
import org.hibernate.annotations.Cascade;
34
import org.hibernate.annotations.CascadeType;
35 e499bab9 Katja Luther
import org.hibernate.annotations.FetchMode;
36
import org.hibernate.annotations.Fetch;
37 76d5535d ben.clark
import org.hibernate.annotations.NaturalId;
38 903a851d ben.clark
import org.hibernate.envers.Audited;
39
import org.hibernate.envers.NotAudited;
40 a784f00f Katja Luther
41
42 51db8d4a ben.clark
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 a784f00f Katja Luther
import org.springframework.security.core.GrantedAuthority;
47
import org.springframework.security.core.userdetails.UserDetails;
48 76d5535d ben.clark
49 903a851d ben.clark
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 76d5535d ben.clark
@Entity
66 51db8d4a ben.clark
@Indexed(index = "eu.etaxonomy.cdm.model.common.User")
67 903a851d ben.clark
@Audited
68 4208745a ben.clark
@Table(name = "UserAccount")
69 76d5535d ben.clark
public class User extends CdmBase implements UserDetails {
70
	private static final long serialVersionUID = 6582191171369439163L;
71 5644153a Andreas Müller
	@SuppressWarnings(value="unused")
72
	private static final Logger logger = Logger.getLogger(User.class);
73
	
74 e499bab9 Katja Luther
	protected User(){
75
		super();
76
	}
77
	
78 5644153a Andreas Müller
	public static User NewInstance(String username, String pwd){
79
		User user = new User();
80
		user.setUsername(username);
81
		user.setPassword(pwd);
82 d3354527 n.hoffmann
		
83
		user.setAccountNonExpired(true);
84
		user.setAccountNonLocked(true);
85
		user.setCredentialsNonExpired(true);
86
		user.setEnabled(true);
87
		
88 5644153a Andreas Müller
		return user;
89
	}
90
	
91 903a851d ben.clark
	@XmlElement(name = "Username")
92 ee91bcd9 ben.clark
	@NaturalId
93 51db8d4a ben.clark
	@Field(index = Index.UN_TOKENIZED)
94 76d5535d ben.clark
	protected String username;
95
	
96
	/**
97
	 * a salted, MD5 encoded hash of the plaintext password
98
	 */
99 903a851d ben.clark
	@XmlElement(name = "Password")
100
	@NotAudited
101 76d5535d ben.clark
	protected String password;
102
	
103 903a851d ben.clark
	@XmlElement(name = "EmailAddress")
104 76d5535d ben.clark
	protected String emailAddress;
105
	
106 903a851d ben.clark
	@XmlElementWrapper(name = "GrantedAuthorities")
107
	@XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
108
	@XmlIDREF
109
	@XmlSchemaType(name = "IDREF")
110 ee91bcd9 ben.clark
	@ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
111 903a851d ben.clark
	@NotAudited
112
	protected Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
113 76d5535d ben.clark
	
114 903a851d ben.clark
	@XmlElementWrapper(name = "Groups")
115
	@XmlElement(name = "Group")
116
	@XmlIDREF
117
	@XmlSchemaType(name = "IDREF")
118 ee91bcd9 ben.clark
	@ManyToMany(fetch = FetchType.LAZY)
119 51db8d4a ben.clark
	@IndexedEmbedded(depth = 1)
120 903a851d ben.clark
	@NotAudited
121 76d5535d ben.clark
	protected Set<Group> groups = new HashSet<Group>();
122
	
123 903a851d ben.clark
	@XmlElement(name = "Enabled")
124 76d5535d ben.clark
	protected boolean enabled;
125
	
126 903a851d ben.clark
	@XmlElement(name = "AccountNonExpired")
127 76d5535d ben.clark
	protected boolean accountNonExpired;
128 903a851d ben.clark
129
	@XmlElement(name = "CredentialsNonExpired")
130 76d5535d ben.clark
	protected boolean credentialsNonExpired;
131
	
132 903a851d ben.clark
	@XmlElement(name = "AccountNonLocked")
133 76d5535d ben.clark
	protected boolean accountNonLocked;	
134
	
135 903a851d ben.clark
	@XmlElement(name = "Person")
136
	@XmlIDREF
137
	@XmlSchemaType(name = "IDREF")
138 a81e4f61 Katja Luther
	@OneToOne(fetch = FetchType.LAZY)
139 4208745a ben.clark
	@Cascade({CascadeType.SAVE_UPDATE})
140 51db8d4a ben.clark
	@IndexedEmbedded(depth = 1)
141 903a851d ben.clark
	protected Person person;
142
	
143
	@XmlTransient
144 ee91bcd9 ben.clark
	@Transient
145 a784f00f Katja Luther
	private Set<GrantedAuthority> authorities;
146 76d5535d ben.clark
	
147
	private void initAuthorities() {
148 a784f00f Katja Luther
		authorities = new HashSet<GrantedAuthority>();
149
		authorities.addAll(grantedAuthorities);
150 76d5535d ben.clark
		for(Group group : groups) {
151 a784f00f Katja Luther
			authorities.addAll(group.getGrantedAuthorities());
152 76d5535d ben.clark
		}
153
	}
154
	
155 b70a7f94 Andreas Kohlbecker
	@Transient
156 a784f00f Katja Luther
	public Set<GrantedAuthority> getAuthorities() {
157 76d5535d ben.clark
		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 903a851d ben.clark
	
235 e499bab9 Katja Luther
	
236 903a851d ben.clark
	public Person getPerson() {
237
		return person;
238
	}
239
	
240
	public void setPerson(Person person) {
241
		this.person = person;
242
	}
243 76d5535d ben.clark
}