adding permission handling and writing a test for usermanagement
[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 @SuppressWarnings(value="unused")
70 private static final Logger logger = Logger.getLogger(User.class);
71
72 protected User(){
73 super();
74 }
75
76 public static User NewInstance(String username, String pwd){
77 User user = new User();
78 user.setUsername(username);
79 user.setPassword(pwd);
80
81 user.setAccountNonExpired(true);
82 user.setAccountNonLocked(true);
83 user.setCredentialsNonExpired(true);
84 user.setEnabled(true);
85
86 return user;
87 }
88
89 public static User NewInstance(String personTitle, String username, String pwd){
90 User user = new User();
91 user.setUsername(username);
92 user.setPassword(pwd);
93
94 user.setAccountNonExpired(true);
95 user.setAccountNonLocked(true);
96 user.setCredentialsNonExpired(true);
97 user.setEnabled(true);
98 Person userPerson = Person.NewTitledInstance(personTitle);
99 user.setPerson(userPerson);
100
101 return user;
102 }
103
104 @XmlElement(name = "Username")
105 @NaturalId
106 @Field(index = Index.UN_TOKENIZED)
107 protected String username;
108
109 /**
110 * a salted, MD5 encoded hash of the plaintext password
111 */
112 @XmlElement(name = "Password")
113 @NotAudited
114 protected String password;
115
116 @XmlElement(name = "EmailAddress")
117 protected String emailAddress;
118
119 @XmlElementWrapper(name = "GrantedAuthorities")
120 @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
121 @XmlIDREF
122 @XmlSchemaType(name = "IDREF")
123 @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
124 @Cascade(CascadeType.SAVE_UPDATE)
125 @NotAudited
126 protected Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
127
128 @XmlElementWrapper(name = "Groups")
129 @XmlElement(name = "Group")
130 @XmlIDREF
131 @XmlSchemaType(name = "IDREF")
132 @ManyToMany(fetch = FetchType.LAZY)
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})
154 @IndexedEmbedded(depth = 1)
155 protected Person person;
156
157 @XmlTransient
158 @Transient
159 private Set<GrantedAuthority> authorities;
160
161 private void initAuthorities() {
162 authorities = new HashSet<GrantedAuthority>();
163 authorities.addAll(grantedAuthorities);
164 for(Group group : groups) {
165 authorities.addAll(group.getGrantedAuthorities());
166 }
167 }
168
169 @Transient
170 public Collection<GrantedAuthority> getAuthorities() {
171 if(authorities == null) {
172 initAuthorities();
173 }
174 return authorities;
175 }
176
177 public String getPassword() {
178 return password;
179 }
180
181 public String getUsername() {
182 return username;
183 }
184
185 public boolean isAccountNonExpired() {
186 return accountNonExpired;
187 }
188
189 public boolean isAccountNonLocked() {
190 return accountNonLocked;
191 }
192
193 public boolean isCredentialsNonExpired() {
194 return credentialsNonExpired;
195 }
196
197 public boolean isEnabled() {
198 return enabled;
199 }
200
201 public String getEmailAddress() {
202 return emailAddress;
203 }
204
205 public void setEmailAddress(String emailAddress) {
206 this.emailAddress = emailAddress;
207 }
208
209 public Set<GrantedAuthority> getGrantedAuthorities() {
210 return grantedAuthorities;
211 }
212
213 public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
214 this.grantedAuthorities = grantedAuthorities;
215 initAuthorities();
216 }
217
218 public void setUsername(String username) {
219 this.username = username;
220 }
221
222 public void setPassword(String password) {
223 this.password = password;
224 }
225
226 public void setEnabled(boolean enabled) {
227 this.enabled = enabled;
228 }
229
230 public void setAccountNonExpired(boolean accountNonExpired) {
231 this.accountNonExpired = accountNonExpired;
232 }
233
234 public void setCredentialsNonExpired(boolean credentialsNonExpired) {
235 this.credentialsNonExpired = credentialsNonExpired;
236 }
237
238 public void setAccountNonLocked(boolean accountNonLocked) {
239 this.accountNonLocked = accountNonLocked;
240 }
241
242 protected void setGroups(Set<Group> groups) {
243 this.groups = groups;
244 initAuthorities();
245 }
246
247 public Set<Group> getGroups() {
248 return groups;
249 }
250
251
252 public Person getPerson() {
253 return person;
254 }
255
256 public void setPerson(Person person) {
257 this.person = person;
258 }
259
260 //*********************** CLONE ********************************************************/
261
262 /**
263 * Clones <i>this</i> User. This is a shortcut that enables to create
264 * a new instance that differs only slightly from <i>this</i> User.
265 * The corresponding person is cloned.
266 *
267 * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
268 * @see java.lang.Object#clone()
269 */
270 @Override
271 public Object clone() {
272 try{
273 User result = (User)super.clone();
274 result.setPerson((Person)this.person.clone());
275 return result;
276 } catch (CloneNotSupportedException e){
277 logger.warn("Object does not implement cloneable");
278 e.printStackTrace();
279 return null;
280 }
281
282
283 }
284 }