User.getAuthorities() made public as preliminary solution to #4053 (Transient field...
[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 import org.hibernate.search.annotations.Analyze;
39 import org.hibernate.search.annotations.Field;
40 import org.hibernate.search.annotations.Indexed;
41 import org.hibernate.search.annotations.IndexedEmbedded;
42 import org.springframework.security.core.GrantedAuthority;
43 import org.springframework.security.core.userdetails.UserDetails;
44
45 import eu.etaxonomy.cdm.model.agent.Person;
46
47 @XmlAccessorType(XmlAccessType.FIELD)
48 @XmlType(name = "User", propOrder = {
49 "username",
50 "password",
51 "emailAddress",
52 "grantedAuthorities",
53 "groups",
54 "enabled",
55 "accountNonExpired",
56 "credentialsNonExpired",
57 "accountNonLocked",
58 "person"
59 })
60 @XmlRootElement(name = "User")
61 @Entity
62 @Indexed(index = "eu.etaxonomy.cdm.model.common.User")
63 @Audited
64 @Table(name = "UserAccount")
65 public class User extends CdmBase implements UserDetails {
66 private static final long serialVersionUID = 6582191171369439163L;
67 private static final Logger logger = Logger.getLogger(User.class);
68
69 protected User(){
70 super();
71 }
72
73 public static User NewInstance(String username, String pwd){
74 User user = new User();
75 user.setUsername(username);
76 user.setPassword(pwd);
77
78 user.setAccountNonExpired(true);
79 user.setAccountNonLocked(true);
80 user.setCredentialsNonExpired(true);
81 user.setEnabled(true);
82
83 return user;
84 }
85
86 public static User NewInstance(String personTitle, String username, String pwd){
87 User user = new User();
88 user.setUsername(username);
89 user.setPassword(pwd);
90
91 user.setAccountNonExpired(true);
92 user.setAccountNonLocked(true);
93 user.setCredentialsNonExpired(true);
94 user.setEnabled(true);
95 Person userPerson = Person.NewTitledInstance(personTitle);
96 user.setPerson(userPerson);
97
98 return user;
99 }
100
101 @XmlElement(name = "Username")
102 @NaturalId
103 @Field(analyze = Analyze.NO)
104 protected String username;
105
106 /**
107 * a salted, MD5 encoded hash of the plaintext password
108 */
109 @XmlElement(name = "Password")
110 @NotAudited
111 protected String password;
112
113 @XmlElement(name = "EmailAddress")
114 protected String emailAddress;
115
116 @XmlElementWrapper(name = "GrantedAuthorities")
117 @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
118 @XmlIDREF
119 @XmlSchemaType(name = "IDREF")
120 @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
121 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.REFRESH}) // see #2414 (Group updating doesn't work)
122 @NotAudited
123 protected Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>(); //authorities of this user only
124
125 @XmlElementWrapper(name = "Groups")
126 @XmlElement(name = "Group")
127 @XmlIDREF
128 @XmlSchemaType(name = "IDREF")
129 @ManyToMany(fetch = FetchType.LAZY)
130 @Cascade(CascadeType.REFRESH) // see #2414 (Group updating doesn't work)
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; //authorities of this user and of all groups the user belongs to
158
159 /**
160 * Initializes or refreshes the collection of authorities, See
161 * {@link #getAuthorities()}
162 */
163 //FIXME made public as preliminary solution to #4053 (Transient field User.authorities not refreshed on reloading entity)
164 public void initAuthorities() {
165 authorities = new HashSet<GrantedAuthority>();
166 authorities.addAll(grantedAuthorities);
167 for(Group group : groups) {
168 authorities.addAll(group.getGrantedAuthorities());
169 }
170 }
171
172 /**
173 * Implementation of {@link UserDetails#getAuthorities()}
174 *
175 * {@inheritDoc}
176 *
177 * @return returns all {@code Set<GrantedAuthority>} instances contained in
178 * the sets {@link #getGrantedAuthorities()} and
179 * {@link #getGroups()}
180 */
181 @Override
182 @Transient
183 public Collection<GrantedAuthority> getAuthorities() {
184 if(authorities == null || authorities.size() == 0) {
185 initAuthorities();
186 }
187 return authorities;
188 }
189
190 @Override
191 public String getPassword() {
192 return password;
193 }
194
195 @Override
196 public String getUsername() {
197 return username;
198 }
199
200 @Override
201 public boolean isAccountNonExpired() {
202 return accountNonExpired;
203 }
204
205 @Override
206 public boolean isAccountNonLocked() {
207 return accountNonLocked;
208 }
209
210 @Override
211 public boolean isCredentialsNonExpired() {
212 return credentialsNonExpired;
213 }
214
215 @Override
216 public boolean isEnabled() {
217 return enabled;
218 }
219
220 public String getEmailAddress() {
221 return emailAddress;
222 }
223
224 public void setEmailAddress(String emailAddress) {
225 this.emailAddress = emailAddress;
226 }
227
228 public Set<GrantedAuthority> getGrantedAuthorities() {
229 return grantedAuthorities;
230 }
231
232 public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
233 this.grantedAuthorities = grantedAuthorities;
234 initAuthorities();
235 }
236
237 public void setUsername(String username) {
238 this.username = username;
239 }
240
241 public void setPassword(String password) {
242 this.password = password;
243 }
244
245 public void setEnabled(boolean enabled) {
246 this.enabled = enabled;
247 }
248
249 public void setAccountNonExpired(boolean accountNonExpired) {
250 this.accountNonExpired = accountNonExpired;
251 }
252
253 public void setCredentialsNonExpired(boolean credentialsNonExpired) {
254 this.credentialsNonExpired = credentialsNonExpired;
255 }
256
257 public void setAccountNonLocked(boolean accountNonLocked) {
258 this.accountNonLocked = accountNonLocked;
259 }
260
261 protected void setGroups(Set<Group> groups) {
262 this.groups = groups;
263 initAuthorities();
264 }
265
266 public Set<Group> getGroups() {
267 return groups;
268 }
269
270
271 public Person getPerson() {
272 return person;
273 }
274
275 public void setPerson(Person person) {
276 this.person = person;
277 }
278
279 //*********************** CLONE ********************************************************/
280
281 /**
282 * Clones <i>this</i> User. This is a shortcut that enables to create
283 * a new instance that differs only slightly from <i>this</i> User.
284 * The corresponding person is cloned.
285 *
286 * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
287 * @see java.lang.Object#clone()
288 */
289 @Override
290 public Object clone() {
291 try{
292 User result = (User)super.clone();
293 result.setPerson((Person)this.person.clone());
294 return result;
295 } catch (CloneNotSupportedException e){
296 logger.warn("Object does not implement cloneable");
297 e.printStackTrace();
298 return null;
299 }
300
301
302 }
303 }