minor
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / User.java
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.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.NaturalId;
36 import org.hibernate.envers.Audited;
37 import org.hibernate.envers.NotAudited;
38
39
40 import org.hibernate.search.annotations.Field;
41 import org.hibernate.search.annotations.Index;
42 import org.hibernate.search.annotations.Indexed;
43 import org.hibernate.search.annotations.IndexedEmbedded;
44 import org.springframework.security.core.GrantedAuthority;
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(index = "eu.etaxonomy.cdm.model.common.User")
65 @Audited
66 @Table(name = "UserAccount")
67 public class User extends CdmBase implements UserDetails {
68 private static final long serialVersionUID = 6582191171369439163L;
69 private static final Logger logger = Logger.getLogger(User.class);
70
71 protected User(){
72 super();
73 }
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 @XmlElement(name = "Username")
104 @NaturalId
105 @Field(index = Index.UN_TOKENIZED)
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 @NotAudited
124 protected Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
125
126 @XmlElementWrapper(name = "Groups")
127 @XmlElement(name = "Group")
128 @XmlIDREF
129 @XmlSchemaType(name = "IDREF")
130 @ManyToMany(fetch = FetchType.LAZY)
131 @IndexedEmbedded(depth = 1)
132 @NotAudited
133 protected Set<Group> groups = new HashSet<Group>();
134
135 @XmlElement(name = "Enabled")
136 protected boolean enabled;
137
138 @XmlElement(name = "AccountNonExpired")
139 protected boolean accountNonExpired;
140
141 @XmlElement(name = "CredentialsNonExpired")
142 protected boolean credentialsNonExpired;
143
144 @XmlElement(name = "AccountNonLocked")
145 protected boolean accountNonLocked;
146
147 @XmlElement(name = "Person")
148 @XmlIDREF
149 @XmlSchemaType(name = "IDREF")
150 @OneToOne(fetch = FetchType.LAZY)
151 @Cascade({CascadeType.SAVE_UPDATE})
152 @IndexedEmbedded(depth = 1)
153 protected Person person;
154
155 @XmlTransient
156 @Transient
157 private Set<GrantedAuthority> authorities;
158
159 private void initAuthorities() {
160 authorities = new HashSet<GrantedAuthority>();
161 authorities.addAll(grantedAuthorities);
162 for(Group group : groups) {
163 authorities.addAll(group.getGrantedAuthorities());
164 }
165 }
166
167 @Transient
168 public Collection<GrantedAuthority> getAuthorities() {
169 if(authorities == null) {
170 initAuthorities();
171 }
172 return authorities;
173 }
174
175 public String getPassword() {
176 return password;
177 }
178
179 public String getUsername() {
180 return username;
181 }
182
183 public boolean isAccountNonExpired() {
184 return accountNonExpired;
185 }
186
187 public boolean isAccountNonLocked() {
188 return accountNonLocked;
189 }
190
191 public boolean isCredentialsNonExpired() {
192 return credentialsNonExpired;
193 }
194
195 public boolean isEnabled() {
196 return enabled;
197 }
198
199 public String getEmailAddress() {
200 return emailAddress;
201 }
202
203 public void setEmailAddress(String emailAddress) {
204 this.emailAddress = emailAddress;
205 }
206
207 public Set<GrantedAuthority> getGrantedAuthorities() {
208 return grantedAuthorities;
209 }
210
211 public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
212 this.grantedAuthorities = grantedAuthorities;
213 initAuthorities();
214 }
215
216 public void setUsername(String username) {
217 this.username = username;
218 }
219
220 public void setPassword(String password) {
221 this.password = password;
222 }
223
224 public void setEnabled(boolean enabled) {
225 this.enabled = enabled;
226 }
227
228 public void setAccountNonExpired(boolean accountNonExpired) {
229 this.accountNonExpired = accountNonExpired;
230 }
231
232 public void setCredentialsNonExpired(boolean credentialsNonExpired) {
233 this.credentialsNonExpired = credentialsNonExpired;
234 }
235
236 public void setAccountNonLocked(boolean accountNonLocked) {
237 this.accountNonLocked = accountNonLocked;
238 }
239
240 protected void setGroups(Set<Group> groups) {
241 this.groups = groups;
242 initAuthorities();
243 }
244
245 public Set<Group> getGroups() {
246 return groups;
247 }
248
249
250 public Person getPerson() {
251 return person;
252 }
253
254 public void setPerson(Person person) {
255 this.person = person;
256 }
257
258 //*********************** CLONE ********************************************************/
259
260 /**
261 * Clones <i>this</i> User. This is a shortcut that enables to create
262 * a new instance that differs only slightly from <i>this</i> User.
263 * The corresponding person is cloned.
264 *
265 * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
266 * @see java.lang.Object#clone()
267 */
268 @Override
269 public Object clone() {
270 try{
271 User result = (User)super.clone();
272 result.setPerson((Person)this.person.clone());
273 return result;
274 } catch (CloneNotSupportedException e){
275 logger.warn("Object does not implement cloneable");
276 e.printStackTrace();
277 return null;
278 }
279
280
281 }
282 }