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)
123 @Cascade(CascadeType
.SAVE_UPDATE
)
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) {
176 public String
getPassword() {
180 public String
getUsername() {
184 public boolean isAccountNonExpired() {
185 return accountNonExpired
;
188 public boolean isAccountNonLocked() {
189 return accountNonLocked
;
192 public boolean isCredentialsNonExpired() {
193 return credentialsNonExpired
;
196 public boolean isEnabled() {
200 public String
getEmailAddress() {
204 public void setEmailAddress(String emailAddress
) {
205 this.emailAddress
= emailAddress
;
208 public Set
<GrantedAuthority
> getGrantedAuthorities() {
209 return grantedAuthorities
;
212 public void setGrantedAuthorities(Set
<GrantedAuthority
> grantedAuthorities
) {
213 this.grantedAuthorities
= grantedAuthorities
;
217 public void setUsername(String username
) {
218 this.username
= username
;
221 public void setPassword(String password
) {
222 this.password
= password
;
225 public void setEnabled(boolean enabled
) {
226 this.enabled
= enabled
;
229 public void setAccountNonExpired(boolean accountNonExpired
) {
230 this.accountNonExpired
= accountNonExpired
;
233 public void setCredentialsNonExpired(boolean credentialsNonExpired
) {
234 this.credentialsNonExpired
= credentialsNonExpired
;
237 public void setAccountNonLocked(boolean accountNonLocked
) {
238 this.accountNonLocked
= accountNonLocked
;
241 protected void setGroups(Set
<Group
> groups
) {
242 this.groups
= groups
;
246 public Set
<Group
> getGroups() {
251 public Person
getPerson() {
255 public void setPerson(Person person
) {
256 this.person
= person
;
259 //*********************** CLONE ********************************************************/
262 * Clones <i>this</i> User. This is a shortcut that enables to create
263 * a new instance that differs only slightly from <i>this</i> User.
264 * The corresponding person is cloned.
266 * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
267 * @see java.lang.Object#clone()
270 public Object
clone() {
272 User result
= (User
)super.clone();
273 result
.setPerson((Person
)this.person
.clone());
275 } catch (CloneNotSupportedException e
){
276 logger
.warn("Object does not implement cloneable");