2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
.common
;
12 import java
.util
.Collection
;
13 import java
.util
.HashSet
;
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
;
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
;
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
;
47 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
49 @XmlAccessorType(XmlAccessType
.FIELD
)
50 @XmlType(name
= "User", propOrder
= {
58 "credentialsNonExpired",
62 @XmlRootElement(name
= "User")
64 @Indexed(index
= "eu.etaxonomy.cdm.model.common.User")
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);
76 public static User
NewInstance(String username
, String pwd
){
77 User user
= new User();
78 user
.setUsername(username
);
79 user
.setPassword(pwd
);
81 user
.setAccountNonExpired(true);
82 user
.setAccountNonLocked(true);
83 user
.setCredentialsNonExpired(true);
84 user
.setEnabled(true);
89 public static User
NewInstance(String personTitle
, String username
, String pwd
){
90 User user
= new User();
91 user
.setUsername(username
);
92 user
.setPassword(pwd
);
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
);
104 @XmlElement(name
= "Username")
106 @Field(index
= Index
.UN_TOKENIZED
)
107 protected String username
;
110 * a salted, MD5 encoded hash of the plaintext password
112 @XmlElement(name
= "Password")
114 protected String password
;
116 @XmlElement(name
= "EmailAddress")
117 protected String emailAddress
;
119 @XmlElementWrapper(name
= "GrantedAuthorities")
120 @XmlElement(name
= "GrantedAuthority", type
= GrantedAuthorityImpl
.class)
122 @XmlSchemaType(name
= "IDREF")
123 @ManyToMany(fetch
= FetchType
.LAZY
, targetEntity
= GrantedAuthorityImpl
.class)
125 protected Set
<GrantedAuthority
> grantedAuthorities
= new HashSet
<GrantedAuthority
>();
127 @XmlElementWrapper(name
= "Groups")
128 @XmlElement(name
= "Group")
130 @XmlSchemaType(name
= "IDREF")
131 @ManyToMany(fetch
= FetchType
.LAZY
)
132 @IndexedEmbedded(depth
= 1)
134 protected Set
<Group
> groups
= new HashSet
<Group
>();
136 @XmlElement(name
= "Enabled")
137 protected boolean enabled
;
139 @XmlElement(name
= "AccountNonExpired")
140 protected boolean accountNonExpired
;
142 @XmlElement(name
= "CredentialsNonExpired")
143 protected boolean credentialsNonExpired
;
145 @XmlElement(name
= "AccountNonLocked")
146 protected boolean accountNonLocked
;
148 @XmlElement(name
= "Person")
150 @XmlSchemaType(name
= "IDREF")
151 @OneToOne(fetch
= FetchType
.LAZY
)
152 @Cascade({CascadeType
.SAVE_UPDATE
})
153 @IndexedEmbedded(depth
= 1)
154 protected Person person
;
158 private Set
<GrantedAuthority
> authorities
;
160 private void initAuthorities() {
161 authorities
= new HashSet
<GrantedAuthority
>();
162 authorities
.addAll(grantedAuthorities
);
163 for(Group group
: groups
) {
164 authorities
.addAll(group
.getGrantedAuthorities());
169 public Collection
<GrantedAuthority
> getAuthorities() {
170 if(authorities
== null) initAuthorities();
174 public String
getPassword() {
178 public String
getUsername() {
182 public boolean isAccountNonExpired() {
183 return accountNonExpired
;
186 public boolean isAccountNonLocked() {
187 return accountNonLocked
;
190 public boolean isCredentialsNonExpired() {
191 return credentialsNonExpired
;
194 public boolean isEnabled() {
198 public String
getEmailAddress() {
202 public void setEmailAddress(String emailAddress
) {
203 this.emailAddress
= emailAddress
;
206 public Set
<GrantedAuthority
> getGrantedAuthorities() {
207 return grantedAuthorities
;
210 public void setGrantedAuthorities(Set
<GrantedAuthority
> grantedAuthorities
) {
211 this.grantedAuthorities
= grantedAuthorities
;
215 public void setUsername(String username
) {
216 this.username
= username
;
219 public void setPassword(String password
) {
220 this.password
= password
;
223 public void setEnabled(boolean enabled
) {
224 this.enabled
= enabled
;
227 public void setAccountNonExpired(boolean accountNonExpired
) {
228 this.accountNonExpired
= accountNonExpired
;
231 public void setCredentialsNonExpired(boolean credentialsNonExpired
) {
232 this.credentialsNonExpired
= credentialsNonExpired
;
235 public void setAccountNonLocked(boolean accountNonLocked
) {
236 this.accountNonLocked
= accountNonLocked
;
239 protected void setGroups(Set
<Group
> groups
) {
240 this.groups
= groups
;
244 public Set
<Group
> getGroups() {
249 public Person
getPerson() {
253 public void setPerson(Person person
) {
254 this.person
= person
;