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)
124 @Cascade(CascadeType
.SAVE_UPDATE
)
126 protected Set
<GrantedAuthority
> grantedAuthorities
= new HashSet
<GrantedAuthority
>();
128 @XmlElementWrapper(name
= "Groups")
129 @XmlElement(name
= "Group")
131 @XmlSchemaType(name
= "IDREF")
132 @ManyToMany(fetch
= FetchType
.LAZY
)
133 @IndexedEmbedded(depth
= 1)
135 protected Set
<Group
> groups
= new HashSet
<Group
>();
137 @XmlElement(name
= "Enabled")
138 protected boolean enabled
;
140 @XmlElement(name
= "AccountNonExpired")
141 protected boolean accountNonExpired
;
143 @XmlElement(name
= "CredentialsNonExpired")
144 protected boolean credentialsNonExpired
;
146 @XmlElement(name
= "AccountNonLocked")
147 protected boolean accountNonLocked
;
149 @XmlElement(name
= "Person")
151 @XmlSchemaType(name
= "IDREF")
152 @OneToOne(fetch
= FetchType
.LAZY
)
153 @Cascade({CascadeType
.SAVE_UPDATE
})
154 @IndexedEmbedded(depth
= 1)
155 protected Person person
;
159 private Set
<GrantedAuthority
> authorities
;
161 private void initAuthorities() {
162 authorities
= new HashSet
<GrantedAuthority
>();
163 authorities
.addAll(grantedAuthorities
);
164 for(Group group
: groups
) {
165 authorities
.addAll(group
.getGrantedAuthorities());
170 public Collection
<GrantedAuthority
> getAuthorities() {
171 if(authorities
== null) {
177 public String
getPassword() {
181 public String
getUsername() {
185 public boolean isAccountNonExpired() {
186 return accountNonExpired
;
189 public boolean isAccountNonLocked() {
190 return accountNonLocked
;
193 public boolean isCredentialsNonExpired() {
194 return credentialsNonExpired
;
197 public boolean isEnabled() {
201 public String
getEmailAddress() {
205 public void setEmailAddress(String emailAddress
) {
206 this.emailAddress
= emailAddress
;
209 public Set
<GrantedAuthority
> getGrantedAuthorities() {
210 return grantedAuthorities
;
213 public void setGrantedAuthorities(Set
<GrantedAuthority
> grantedAuthorities
) {
214 this.grantedAuthorities
= grantedAuthorities
;
218 public void setUsername(String username
) {
219 this.username
= username
;
222 public void setPassword(String password
) {
223 this.password
= password
;
226 public void setEnabled(boolean enabled
) {
227 this.enabled
= enabled
;
230 public void setAccountNonExpired(boolean accountNonExpired
) {
231 this.accountNonExpired
= accountNonExpired
;
234 public void setCredentialsNonExpired(boolean credentialsNonExpired
) {
235 this.credentialsNonExpired
= credentialsNonExpired
;
238 public void setAccountNonLocked(boolean accountNonLocked
) {
239 this.accountNonLocked
= accountNonLocked
;
242 protected void setGroups(Set
<Group
> groups
) {
243 this.groups
= groups
;
247 public Set
<Group
> getGroups() {
252 public Person
getPerson() {
256 public void setPerson(Person person
) {
257 this.person
= person
;
260 //*********************** CLONE ********************************************************/
263 * Clones <i>this</i> User. This is a shortcut that enables to create
264 * a new instance that differs only slightly from <i>this</i> User.
265 * The corresponding person is cloned.
267 * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
268 * @see java.lang.Object#clone()
271 public Object
clone() {
273 User result
= (User
)super.clone();
274 result
.setPerson((Person
)this.person
.clone());
276 } catch (CloneNotSupportedException e
){
277 logger
.warn("Object does not implement cloneable");