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
;
38 import org
.hibernate
.search
.annotations
.Analyze
;
39 import org
.hibernate
.search
.annotations
.Field
;
40 import org
.hibernate
.search
.annotations
.Indexed
;
41 import org
.hibernate
.search
.annotations
.IndexedEmbedded
;
42 import org
.springframework
.security
.core
.GrantedAuthority
;
43 import org
.springframework
.security
.core
.userdetails
.UserDetails
;
45 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
47 @XmlAccessorType(XmlAccessType
.FIELD
)
48 @XmlType(name
= "User", propOrder
= {
56 "credentialsNonExpired",
60 @XmlRootElement(name
= "User")
62 @Indexed(index
= "eu.etaxonomy.cdm.model.common.User")
64 @Table(name
= "UserAccount")
65 public class User
extends CdmBase
implements UserDetails
{
66 private static final long serialVersionUID
= 6582191171369439163L;
67 private static final Logger logger
= Logger
.getLogger(User
.class);
73 public static User
NewInstance(String username
, String pwd
){
74 User user
= new User();
75 user
.setUsername(username
);
76 user
.setPassword(pwd
);
78 user
.setAccountNonExpired(true);
79 user
.setAccountNonLocked(true);
80 user
.setCredentialsNonExpired(true);
81 user
.setEnabled(true);
86 public static User
NewInstance(String personTitle
, String username
, String pwd
){
87 User user
= new User();
88 user
.setUsername(username
);
89 user
.setPassword(pwd
);
91 user
.setAccountNonExpired(true);
92 user
.setAccountNonLocked(true);
93 user
.setCredentialsNonExpired(true);
94 user
.setEnabled(true);
95 Person userPerson
= Person
.NewTitledInstance(personTitle
);
96 user
.setPerson(userPerson
);
101 @XmlElement(name
= "Username")
103 @Field(analyze
= Analyze
.NO
)
104 protected String username
;
107 * a salted, MD5 encoded hash of the plaintext password
109 @XmlElement(name
= "Password")
111 protected String password
;
113 @XmlElement(name
= "EmailAddress")
114 protected String emailAddress
;
116 @XmlElementWrapper(name
= "GrantedAuthorities")
117 @XmlElement(name
= "GrantedAuthority", type
= GrantedAuthorityImpl
.class)
119 @XmlSchemaType(name
= "IDREF")
120 @ManyToMany(fetch
= FetchType
.LAZY
, targetEntity
= GrantedAuthorityImpl
.class)
121 @Cascade(CascadeType
.SAVE_UPDATE
)
123 protected Set
<GrantedAuthority
> grantedAuthorities
= new HashSet
<GrantedAuthority
>(); //authorities of this user only
125 @XmlElementWrapper(name
= "Groups")
126 @XmlElement(name
= "Group")
128 @XmlSchemaType(name
= "IDREF")
129 @ManyToMany(fetch
= FetchType
.LAZY
)
130 @IndexedEmbedded(depth
= 1)
132 protected Set
<Group
> groups
= new HashSet
<Group
>();
134 @XmlElement(name
= "Enabled")
135 protected boolean enabled
;
137 @XmlElement(name
= "AccountNonExpired")
138 protected boolean accountNonExpired
;
140 @XmlElement(name
= "CredentialsNonExpired")
141 protected boolean credentialsNonExpired
;
143 @XmlElement(name
= "AccountNonLocked")
144 protected boolean accountNonLocked
;
146 @XmlElement(name
= "Person")
148 @XmlSchemaType(name
= "IDREF")
149 @OneToOne(fetch
= FetchType
.LAZY
)
150 @Cascade({CascadeType
.SAVE_UPDATE
})
151 @IndexedEmbedded(depth
= 1)
152 protected Person person
;
156 private Set
<GrantedAuthority
> authorities
; //authorities of this user and of all groups the user belongs to
158 private void initAuthorities() {
159 authorities
= new HashSet
<GrantedAuthority
>();
160 authorities
.addAll(grantedAuthorities
);
161 for(Group group
: groups
) {
162 authorities
.addAll(group
.getGrantedAuthorities());
169 * @return returns a {@code Set<GrantedAuthority>} as Collection
173 public Collection
<GrantedAuthority
> getAuthorities() {
174 if(authorities
== null || authorities
.size() == 0) {
181 public String
getPassword() {
186 public String
getUsername() {
191 public boolean isAccountNonExpired() {
192 return accountNonExpired
;
196 public boolean isAccountNonLocked() {
197 return accountNonLocked
;
201 public boolean isCredentialsNonExpired() {
202 return credentialsNonExpired
;
206 public boolean isEnabled() {
210 public String
getEmailAddress() {
214 public void setEmailAddress(String emailAddress
) {
215 this.emailAddress
= emailAddress
;
218 public Set
<GrantedAuthority
> getGrantedAuthorities() {
219 return grantedAuthorities
;
222 public void setGrantedAuthorities(Set
<GrantedAuthority
> grantedAuthorities
) {
223 this.grantedAuthorities
= grantedAuthorities
;
227 public void setUsername(String username
) {
228 this.username
= username
;
231 public void setPassword(String password
) {
232 this.password
= password
;
235 public void setEnabled(boolean enabled
) {
236 this.enabled
= enabled
;
239 public void setAccountNonExpired(boolean accountNonExpired
) {
240 this.accountNonExpired
= accountNonExpired
;
243 public void setCredentialsNonExpired(boolean credentialsNonExpired
) {
244 this.credentialsNonExpired
= credentialsNonExpired
;
247 public void setAccountNonLocked(boolean accountNonLocked
) {
248 this.accountNonLocked
= accountNonLocked
;
251 protected void setGroups(Set
<Group
> groups
) {
252 this.groups
= groups
;
256 public Set
<Group
> getGroups() {
261 public Person
getPerson() {
265 public void setPerson(Person person
) {
266 this.person
= person
;
269 //*********************** CLONE ********************************************************/
272 * Clones <i>this</i> User. This is a shortcut that enables to create
273 * a new instance that differs only slightly from <i>this</i> User.
274 * The corresponding person is cloned.
276 * @see eu.etaxonomy.cdm.model.common.CdmBase#clone()
277 * @see java.lang.Object#clone()
280 public Object
clone() {
282 User result
= (User
)super.clone();
283 result
.setPerson((Person
)this.person
.clone());
285 } catch (CloneNotSupportedException e
){
286 logger
.warn("Object does not implement cloneable");