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
.HashSet
;
14 import java
.util
.TreeSet
;
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
;
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
;
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
;
48 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
50 @XmlAccessorType(XmlAccessType
.FIELD
)
51 @XmlType(name
= "User", propOrder
= {
59 "credentialsNonExpired",
63 @XmlRootElement(name
= "User")
65 @Indexed(index
= "eu.etaxonomy.cdm.model.common.User")
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);
77 public static User
NewInstance(String username
, String pwd
){
78 User user
= new User();
79 user
.setUsername(username
);
80 user
.setPassword(pwd
);
82 user
.setAccountNonExpired(true);
83 user
.setAccountNonLocked(true);
84 user
.setCredentialsNonExpired(true);
85 user
.setEnabled(true);
90 @XmlElement(name
= "Username")
92 @Field(index
= Index
.UN_TOKENIZED
)
93 protected String username
;
96 * a salted, MD5 encoded hash of the plaintext password
98 @XmlElement(name
= "Password")
100 protected String password
;
102 @XmlElement(name
= "EmailAddress")
103 protected String emailAddress
;
105 @XmlElementWrapper(name
= "GrantedAuthorities")
106 @XmlElement(name
= "GrantedAuthority", type
= GrantedAuthorityImpl
.class)
108 @XmlSchemaType(name
= "IDREF")
109 @ManyToMany(fetch
= FetchType
.LAZY
, targetEntity
= GrantedAuthorityImpl
.class)
111 protected Set
<GrantedAuthority
> grantedAuthorities
= new HashSet
<GrantedAuthority
>();
113 @XmlElementWrapper(name
= "Groups")
114 @XmlElement(name
= "Group")
116 @XmlSchemaType(name
= "IDREF")
117 @ManyToMany(fetch
= FetchType
.LAZY
)
118 @IndexedEmbedded(depth
= 1)
120 protected Set
<Group
> groups
= new HashSet
<Group
>();
122 @XmlElement(name
= "Enabled")
123 protected boolean enabled
;
125 @XmlElement(name
= "AccountNonExpired")
126 protected boolean accountNonExpired
;
128 @XmlElement(name
= "CredentialsNonExpired")
129 protected boolean credentialsNonExpired
;
131 @XmlElement(name
= "AccountNonLocked")
132 protected boolean accountNonLocked
;
134 @XmlElement(name
= "Person")
136 @XmlSchemaType(name
= "IDREF")
137 @OneToOne(fetch
= FetchType
.LAZY
)
138 @Cascade({CascadeType
.SAVE_UPDATE
})
139 @IndexedEmbedded(depth
= 1)
140 protected Person person
;
144 private GrantedAuthority
[] authorities
;
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());
153 authorities
= allAuthorities
.toArray(new GrantedAuthority
[allAuthorities
.size()]);
157 public GrantedAuthority
[] getAuthorities() {
158 if(authorities
== null) initAuthorities();
162 public String
getPassword() {
166 public String
getUsername() {
170 public boolean isAccountNonExpired() {
171 return accountNonExpired
;
174 public boolean isAccountNonLocked() {
175 return accountNonLocked
;
178 public boolean isCredentialsNonExpired() {
179 return credentialsNonExpired
;
182 public boolean isEnabled() {
186 public String
getEmailAddress() {
190 public void setEmailAddress(String emailAddress
) {
191 this.emailAddress
= emailAddress
;
194 public Set
<GrantedAuthority
> getGrantedAuthorities() {
195 return grantedAuthorities
;
198 public void setGrantedAuthorities(Set
<GrantedAuthority
> grantedAuthorities
) {
199 this.grantedAuthorities
= grantedAuthorities
;
203 public void setUsername(String username
) {
204 this.username
= username
;
207 public void setPassword(String password
) {
208 this.password
= password
;
211 public void setEnabled(boolean enabled
) {
212 this.enabled
= enabled
;
215 public void setAccountNonExpired(boolean accountNonExpired
) {
216 this.accountNonExpired
= accountNonExpired
;
219 public void setCredentialsNonExpired(boolean credentialsNonExpired
) {
220 this.credentialsNonExpired
= credentialsNonExpired
;
223 public void setAccountNonLocked(boolean accountNonLocked
) {
224 this.accountNonLocked
= accountNonLocked
;
227 protected void setGroups(Set
<Group
> groups
) {
228 this.groups
= groups
;
232 public Set
<Group
> getGroups() {
237 public Person
getPerson() {
241 public void setPerson(Person person
) {
242 this.person
= person
;