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