Committing large number of changes relating to versioning implementation (#108)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / User.java
1 package eu.etaxonomy.cdm.model.common;
2
3 import java.util.HashSet;
4 import java.util.Set;
5 import java.util.TreeSet;
6
7 import javax.persistence.Entity;
8 import javax.persistence.FetchType;
9 import javax.persistence.ManyToMany;
10 import javax.persistence.Transient;
11
12 import org.hibernate.annotations.NaturalId;
13 import org.springframework.security.GrantedAuthority;
14 import org.springframework.security.userdetails.UserDetails;
15
16 @Entity
17 public class User extends CdmBase implements UserDetails {
18
19 /**
20 *
21 */
22 private static final long serialVersionUID = 6582191171369439163L;
23
24 @NaturalId
25 protected String username;
26
27 /**
28 * a salted, MD5 encoded hash of the plaintext password
29 */
30 protected String password;
31
32 protected String emailAddress;
33
34 @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
35 protected Set <GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
36
37 @ManyToMany(fetch = FetchType.LAZY)
38 protected Set<Group> groups = new HashSet<Group>();
39
40 protected boolean enabled;
41
42 protected boolean accountNonExpired;
43
44 protected boolean credentialsNonExpired;
45
46 protected boolean accountNonLocked;
47
48 @Transient
49 private GrantedAuthority[] authorities;
50
51 private void initAuthorities() {
52 Set<GrantedAuthority> allAuthorities = new TreeSet<GrantedAuthority>();
53 allAuthorities.addAll(grantedAuthorities);
54 for(Group group : groups) {
55 allAuthorities.addAll(group.getGrantedAuthorities());
56 }
57
58 authorities = allAuthorities.toArray(new GrantedAuthority[allAuthorities.size()]);
59 }
60
61 public GrantedAuthority[] getAuthorities() {
62 if(authorities == null) initAuthorities();
63 return authorities;
64 }
65
66 public String getPassword() {
67 return password;
68 }
69
70 public String getUsername() {
71 return username;
72 }
73
74 public boolean isAccountNonExpired() {
75 return accountNonExpired;
76 }
77
78 public boolean isAccountNonLocked() {
79 return accountNonLocked;
80 }
81
82 public boolean isCredentialsNonExpired() {
83 return credentialsNonExpired;
84 }
85
86 public boolean isEnabled() {
87 return enabled;
88 }
89
90 public String getEmailAddress() {
91 return emailAddress;
92 }
93
94 public void setEmailAddress(String emailAddress) {
95 this.emailAddress = emailAddress;
96 }
97
98 public Set<GrantedAuthority> getGrantedAuthorities() {
99 return grantedAuthorities;
100 }
101
102 public void setGrantedAuthorities(Set<GrantedAuthority> grantedAuthorities) {
103 this.grantedAuthorities = grantedAuthorities;
104 initAuthorities();
105 }
106
107 public void setUsername(String username) {
108 this.username = username;
109 }
110
111 public void setPassword(String password) {
112 this.password = password;
113 }
114
115 public void setEnabled(boolean enabled) {
116 this.enabled = enabled;
117 }
118
119 public void setAccountNonExpired(boolean accountNonExpired) {
120 this.accountNonExpired = accountNonExpired;
121 }
122
123 public void setCredentialsNonExpired(boolean credentialsNonExpired) {
124 this.credentialsNonExpired = credentialsNonExpired;
125 }
126
127 public void setAccountNonLocked(boolean accountNonLocked) {
128 this.accountNonLocked = accountNonLocked;
129 }
130
131 protected void setGroups(Set<Group> groups) {
132 this.groups = groups;
133 initAuthorities();
134 }
135
136 public Set<Group> getGroups() {
137 return groups;
138 }
139 }