TermBase and descendants indexed using hibernate search
[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.HashSet;
13 import java.util.Set;
14 import java.util.TreeSet;
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
23 import javax.xml.bind.annotation.XmlAccessType;
24 import javax.xml.bind.annotation.XmlAccessorType;
25 import javax.xml.bind.annotation.XmlElement;
26 import javax.xml.bind.annotation.XmlElementWrapper;
27 import javax.xml.bind.annotation.XmlIDREF;
28 import javax.xml.bind.annotation.XmlRootElement;
29 import javax.xml.bind.annotation.XmlSchemaType;
30 import javax.xml.bind.annotation.XmlTransient;
31 import javax.xml.bind.annotation.XmlType;
32
33 import org.apache.log4j.Logger;
34 import org.hibernate.annotations.Cascade;
35 import org.hibernate.annotations.CascadeType;
36 import org.hibernate.annotations.FetchMode;
37 import org.hibernate.annotations.Fetch;
38 import org.hibernate.annotations.NaturalId;
39 import org.hibernate.envers.Audited;
40 import org.hibernate.envers.NotAudited;
41 import org.hibernate.search.annotations.Field;
42 import org.hibernate.search.annotations.Index;
43 import org.hibernate.search.annotations.Indexed;
44 import org.hibernate.search.annotations.IndexedEmbedded;
45 import org.springframework.security.GrantedAuthority;
46 import org.springframework.security.userdetails.UserDetails;
47
48 import eu.etaxonomy.cdm.model.agent.Person;
49
50 @XmlAccessorType(XmlAccessType.FIELD)
51 @XmlType(name = "User", propOrder = {
52 "username",
53 "password",
54 "emailAddress",
55 "grantedAuthorities",
56 "groups",
57 "enabled",
58 "accountNonExpired",
59 "credentialsNonExpired",
60 "accountNonLocked",
61 "person"
62 })
63 @XmlRootElement(name = "User")
64 @Entity
65 @Indexed(index = "eu.etaxonomy.cdm.model.common.User")
66 @Audited
67 @Table(name = "UserAccount")
68 public class User extends CdmBase implements UserDetails {
69 private static final long serialVersionUID = 6582191171369439163L;
70 @SuppressWarnings(value="unused")
71 private static final Logger logger = Logger.getLogger(User.class);
72
73 protected User(){
74 super();
75 }
76
77 public static User NewInstance(String username, String pwd){
78 User user = new User();
79 user.setUsername(username);
80 user.setPassword(pwd);
81
82 user.setAccountNonExpired(true);
83 user.setAccountNonLocked(true);
84 user.setCredentialsNonExpired(true);
85 user.setEnabled(true);
86
87 return user;
88 }
89
90 @XmlElement(name = "Username")
91 @NaturalId
92 @Field(index = Index.UN_TOKENIZED)
93 protected String username;
94
95 /**
96 * a salted, MD5 encoded hash of the plaintext password
97 */
98 @XmlElement(name = "Password")
99 @NotAudited
100 protected String password;
101
102 @XmlElement(name = "EmailAddress")
103 protected String emailAddress;
104
105 @XmlElementWrapper(name = "GrantedAuthorities")
106 @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
107 @XmlIDREF
108 @XmlSchemaType(name = "IDREF")
109 @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
110 @NotAudited
111 protected Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
112
113 @XmlElementWrapper(name = "Groups")
114 @XmlElement(name = "Group")
115 @XmlIDREF
116 @XmlSchemaType(name = "IDREF")
117 @ManyToMany(fetch = FetchType.LAZY)
118 @IndexedEmbedded(depth = 1)
119 @NotAudited
120 protected Set<Group> groups = new HashSet<Group>();
121
122 @XmlElement(name = "Enabled")
123 protected boolean enabled;
124
125 @XmlElement(name = "AccountNonExpired")
126 protected boolean accountNonExpired;
127
128 @XmlElement(name = "CredentialsNonExpired")
129 protected boolean credentialsNonExpired;
130
131 @XmlElement(name = "AccountNonLocked")
132 protected boolean accountNonLocked;
133
134 @XmlElement(name = "Person")
135 @XmlIDREF
136 @XmlSchemaType(name = "IDREF")
137 @OneToOne(fetch = FetchType.LAZY)
138 @Cascade({CascadeType.SAVE_UPDATE})
139 @IndexedEmbedded(depth = 1)
140 protected Person person;
141
142 @XmlTransient
143 @Transient
144 private GrantedAuthority[] authorities;
145
146 private void initAuthorities() {
147 Set<GrantedAuthority> allAuthorities = new TreeSet<GrantedAuthority>();
148 allAuthorities.addAll(grantedAuthorities);
149 for(Group group : groups) {
150 allAuthorities.addAll(group.getGrantedAuthorities());
151 }
152
153 authorities = allAuthorities.toArray(new GrantedAuthority[allAuthorities.size()]);
154 }
155
156 @Transient
157 public GrantedAuthority[] getAuthorities() {
158 if(authorities == null) initAuthorities();
159 return authorities;
160 }
161
162 public String getPassword() {
163 return password;
164 }
165
166 public String getUsername() {
167 return username;
168 }
169
170 public boolean isAccountNonExpired() {
171 return accountNonExpired;
172 }
173
174 public boolean isAccountNonLocked() {
175 return accountNonLocked;
176 }
177
178 public boolean isCredentialsNonExpired() {
179 return credentialsNonExpired;
180 }
181
182 public boolean isEnabled() {
183 return enabled;
184 }
185
186 public String getEmailAddress() {
187 return emailAddress;
188 }
189
190 public void setEmailAddress(String emailAddress) {
191 this.emailAddress = emailAddress;
192 }
193
194 public Set<GrantedAuthority> getGrantedAuthorities() {
195 return grantedAuthorities;
196 }
197
198 public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
199 this.grantedAuthorities = grantedAuthorities;
200 initAuthorities();
201 }
202
203 public void setUsername(String username) {
204 this.username = username;
205 }
206
207 public void setPassword(String password) {
208 this.password = password;
209 }
210
211 public void setEnabled(boolean enabled) {
212 this.enabled = enabled;
213 }
214
215 public void setAccountNonExpired(boolean accountNonExpired) {
216 this.accountNonExpired = accountNonExpired;
217 }
218
219 public void setCredentialsNonExpired(boolean credentialsNonExpired) {
220 this.credentialsNonExpired = credentialsNonExpired;
221 }
222
223 public void setAccountNonLocked(boolean accountNonLocked) {
224 this.accountNonLocked = accountNonLocked;
225 }
226
227 protected void setGroups(Set<Group> groups) {
228 this.groups = groups;
229 initAuthorities();
230 }
231
232 public Set<Group> getGroups() {
233 return groups;
234 }
235
236
237 public Person getPerson() {
238 return person;
239 }
240
241 public void setPerson(Person person) {
242 this.person = person;
243 }
244 }