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 private static final Logger logger
= Logger
.getLogger(User
.class);
75 public static User
NewInstance(String username
, String pwd
){
76 User user
= new User();
77 user
.setUsername(username
);
78 user
.setPassword(pwd
);
80 user
.setAccountNonExpired(true);
81 user
.setAccountNonLocked(true);
82 user
.setCredentialsNonExpired(true);
83 user
.setEnabled(true);
88 public static User
NewInstance(String personTitle
, String username
, String pwd
){
89 User user
= new User();
90 user
.setUsername(username
);
91 user
.setPassword(pwd
);
93 user
.setAccountNonExpired(true);
94 user
.setAccountNonLocked(true);
95 user
.setCredentialsNonExpired(true);
96 user
.setEnabled(true);
97 Person userPerson
= Person
.NewTitledInstance(personTitle
);
98 user
.setPerson(userPerson
);
103 @XmlElement(name
= "Username")
105 @Field(index
= Index
.UN_TOKENIZED
)
106 protected String username
;
109 * a salted, MD5 encoded hash of the plaintext password
111 @XmlElement(name
= "Password")
113 protected String password
;
115 @XmlElement(name
= "EmailAddress")
116 protected String emailAddress
;
118 @XmlElementWrapper(name
= "GrantedAuthorities")
119 @XmlElement(name
= "GrantedAuthority", type
= GrantedAuthorityImpl
.class)
121 @XmlSchemaType(name
= "IDREF")
122 @ManyToMany(fetch
= FetchType
.LAZY
, targetEntity
= GrantedAuthorityImpl
.class)
124 protected Set
<GrantedAuthority
> grantedAuthorities
= new HashSet
<GrantedAuthority
>();
126 @XmlElementWrapper(name
= "Groups")
127 @XmlElement(name
= "Group")
129 @XmlSchemaType(name
= "IDREF")
130 @ManyToMany(fetch
= FetchType
.LAZY
)
131 @IndexedEmbedded(depth
= 1)
133 protected Set
<Group
> groups
= new HashSet
<Group
>();
135 @XmlElement(name
= "Enabled")
136 protected boolean enabled
;
138 @XmlElement(name
= "AccountNonExpired")
139 protected boolean accountNonExpired
;
141 @XmlElement(name
= "CredentialsNonExpired")
142 protected boolean credentialsNonExpired
;
144 @XmlElement(name
= "AccountNonLocked")
145 protected boolean accountNonLocked
;
147 @XmlElement(name
= "Person")
149 @XmlSchemaType(name
= "IDREF")
150 @OneToOne(fetch
= FetchType
.LAZY
)
151 @Cascade({CascadeType
.SAVE_UPDATE
})
152 @IndexedEmbedded(depth
= 1)
153 protected Person person
;
157 private Set
<GrantedAuthority
> authorities
;
159 private void initAuthorities() {
160 authorities
= new HashSet
<GrantedAuthority
>();
161 authorities
.addAll(grantedAuthorities
);
162 for(Group group
: groups
) {
163 authorities
.addAll(group
.getGrantedAuthorities());
168 public Collection
<GrantedAuthority
> getAuthorities() {
169 if(authorities
== null) {
175 public String
getPassword() {
179 public String
getUsername() {
183 public boolean isAccountNonExpired() {
184 return accountNonExpired
;
187 public boolean isAccountNonLocked() {
188 return accountNonLocked
;
191 public boolean isCredentialsNonExpired() {
192 return credentialsNonExpired
;
195 public boolean isEnabled() {
199 public String
getEmailAddress() {
203 public void setEmailAddress(String emailAddress
) {
204 this.emailAddress
= emailAddress
;
207 public Set
<GrantedAuthority
> getGrantedAuthorities() {
208 return grantedAuthorities
;
211 public void setGrantedAuthorities(Set
<GrantedAuthority
> grantedAuthorities
) {
212 this.grantedAuthorities
= grantedAuthorities
;
216 public void setUsername(String username
) {
217 this.username
= username
;
220 public void setPassword(String password
) {
221 this.password
= password
;
224 public void setEnabled(boolean enabled
) {
225 this.enabled
= enabled
;
228 public void setAccountNonExpired(boolean accountNonExpired
) {
229 this.accountNonExpired
= accountNonExpired
;
232 public void setCredentialsNonExpired(boolean credentialsNonExpired
) {
233 this.credentialsNonExpired
= credentialsNonExpired
;
236 public void setAccountNonLocked(boolean accountNonLocked
) {
237 this.accountNonLocked
= accountNonLocked
;
240 protected void setGroups(Set
<Group
> groups
) {
241 this.groups
= groups
;
245 public Set
<Group
> getGroups() {
250 public Person
getPerson() {
254 public void setPerson(Person person
) {
255 this.person
= person
;
258 //*********************** CLONE ********************************************************/
261 * Clones <i>this</i> User. This is a shortcut that enables to create
262 * a new instance that differs only slightly from <i>this</i> User.
263 * The corresponding person is cloned.
265 * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
266 * @see java.lang.Object#clone()
269 public Object
clone() {
271 User result
= (User
)super.clone();
272 result
.setPerson((Person
)this.person
.clone());
274 } catch (CloneNotSupportedException e
){
275 logger
.warn("Object does not implement cloneable");