461810302537b997b81b8fdf62b64d47a642610e
[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 @XmlElement(name = "Username")
90 @NaturalId
91 @Field(index = Index.UN_TOKENIZED)
92 protected String username;
93
94 /**
95 * a salted, MD5 encoded hash of the plaintext password
96 */
97 @XmlElement(name = "Password")
98 @NotAudited
99 protected String password;
100
101 @XmlElement(name = "EmailAddress")
102 protected String emailAddress;
103
104 @XmlElementWrapper(name = "GrantedAuthorities")
105 @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
106 @XmlIDREF
107 @XmlSchemaType(name = "IDREF")
108 @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
109 @NotAudited
110 protected Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
111
112 @XmlElementWrapper(name = "Groups")
113 @XmlElement(name = "Group")
114 @XmlIDREF
115 @XmlSchemaType(name = "IDREF")
116 @ManyToMany(fetch = FetchType.LAZY)
117 @IndexedEmbedded(depth = 1)
118 @NotAudited
119 protected Set<Group> groups = new HashSet<Group>();
120
121 @XmlElement(name = "Enabled")
122 protected boolean enabled;
123
124 @XmlElement(name = "AccountNonExpired")
125 protected boolean accountNonExpired;
126
127 @XmlElement(name = "CredentialsNonExpired")
128 protected boolean credentialsNonExpired;
129
130 @XmlElement(name = "AccountNonLocked")
131 protected boolean accountNonLocked;
132
133 @XmlElement(name = "Person")
134 @XmlIDREF
135 @XmlSchemaType(name = "IDREF")
136 @OneToOne(fetch = FetchType.LAZY)
137 @Cascade({CascadeType.SAVE_UPDATE})
138 @IndexedEmbedded(depth = 1)
139 protected Person person;
140
141 @XmlTransient
142 @Transient
143 private Set<GrantedAuthority> authorities;
144
145 private void initAuthorities() {
146 authorities = new HashSet<GrantedAuthority>();
147 authorities.addAll(grantedAuthorities);
148 for(Group group : groups) {
149 authorities.addAll(group.getGrantedAuthorities());
150 }
151 }
152
153 @Transient
154 public Collection<GrantedAuthority> getAuthorities() {
155 if(authorities == null) initAuthorities();
156 return authorities;
157 }
158
159 public String getPassword() {
160 return password;
161 }
162
163 public String getUsername() {
164 return username;
165 }
166
167 public boolean isAccountNonExpired() {
168 return accountNonExpired;
169 }
170
171 public boolean isAccountNonLocked() {
172 return accountNonLocked;
173 }
174
175 public boolean isCredentialsNonExpired() {
176 return credentialsNonExpired;
177 }
178
179 public boolean isEnabled() {
180 return enabled;
181 }
182
183 public String getEmailAddress() {
184 return emailAddress;
185 }
186
187 public void setEmailAddress(String emailAddress) {
188 this.emailAddress = emailAddress;
189 }
190
191 public Set<GrantedAuthority> getGrantedAuthorities() {
192 return grantedAuthorities;
193 }
194
195 public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
196 this.grantedAuthorities = grantedAuthorities;
197 initAuthorities();
198 }
199
200 public void setUsername(String username) {
201 this.username = username;
202 }
203
204 public void setPassword(String password) {
205 this.password = password;
206 }
207
208 public void setEnabled(boolean enabled) {
209 this.enabled = enabled;
210 }
211
212 public void setAccountNonExpired(boolean accountNonExpired) {
213 this.accountNonExpired = accountNonExpired;
214 }
215
216 public void setCredentialsNonExpired(boolean credentialsNonExpired) {
217 this.credentialsNonExpired = credentialsNonExpired;
218 }
219
220 public void setAccountNonLocked(boolean accountNonLocked) {
221 this.accountNonLocked = accountNonLocked;
222 }
223
224 protected void setGroups(Set<Group> groups) {
225 this.groups = groups;
226 initAuthorities();
227 }
228
229 public Set<Group> getGroups() {
230 return groups;
231 }
232
233
234 public Person getPerson() {
235 return person;
236 }
237
238 public void setPerson(Person person) {
239 this.person = person;
240 }
241 }