1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.userdetails;
17
18 import org.acegisecurity.GrantedAuthority;
19 import org.acegisecurity.providers.dao.DaoAuthenticationProvider;
20 import org.springframework.util.Assert;
21
22 /***
23 * Models core user information retieved by an {@link UserDetailsService}.
24 *
25 * <P>
26 * Implemented with value object semantics (immutable after construction, like a
27 * <code>String</code>). Developers may use this class directly, subclass it,
28 * or write their own {@link UserDetails} implementation from scratch.
29 * </p>
30 */
31 public class User implements UserDetails {
32
33
34
35 private String password;
36
37 private String username;
38
39 private GrantedAuthority[] authorities;
40
41 private boolean accountNonExpired;
42
43 private boolean accountNonLocked;
44
45 private boolean credentialsNonExpired;
46
47 private boolean enabled;
48
49
50
51
52 protected User() {
53 throw new IllegalArgumentException("Cannot use default constructor");
54 }
55
56 /***
57 * Construct the <code>User</code> with the details required by {@link
58 * DaoAuthenticationProvider}.
59 *
60 * @param username
61 * the username presented to the
62 * <code>DaoAuthenticationProvider</code>
63 * @param password
64 * the password that should be presented to the
65 * <code>DaoAuthenticationProvider</code>
66 * @param enabled
67 * set to <code>true</code> if the user is enabled
68 * @param authorities
69 * the authorities that should be granted to the caller if they
70 * presented the correct username and password and the user is
71 * enabled
72 *
73 * @throws IllegalArgumentException
74 * if a <code>null</code> value was passed either as a
75 * parameter or as an element in the
76 * <code>GrantedAuthority[]</code> array
77 *
78 * @deprecated use new constructor with extended properties (this
79 * constructor will be removed from release 1.0.0)
80 */
81 public User(String username, String password, boolean enabled,
82 GrantedAuthority[] authorities) throws IllegalArgumentException {
83 this(username, password, enabled, true, true, authorities);
84 }
85
86 /***
87 * Construct the <code>User</code> with the details required by {@link
88 * DaoAuthenticationProvider}.
89 *
90 * @param username
91 * the username presented to the
92 * <code>DaoAuthenticationProvider</code>
93 * @param password
94 * the password that should be presented to the
95 * <code>DaoAuthenticationProvider</code>
96 * @param enabled
97 * set to <code>true</code> if the user is enabled
98 * @param accountNonExpired
99 * set to <code>true</code> if the account has not expired
100 * @param credentialsNonExpired
101 * set to <code>true</code> if the credentials have not expired
102 * @param authorities
103 * the authorities that should be granted to the caller if they
104 * presented the correct username and password and the user is
105 * enabled
106 *
107 * @throws IllegalArgumentException
108 * if a <code>null</code> value was passed either as a
109 * parameter or as an element in the
110 * <code>GrantedAuthority[]</code> array
111 *
112 * @deprecated use new constructor with extended properties (this
113 * constructor will be removed from release 1.0.0)
114 */
115 public User(String username, String password, boolean enabled,
116 boolean accountNonExpired, boolean credentialsNonExpired,
117 GrantedAuthority[] authorities) throws IllegalArgumentException {
118 this(username, password, enabled, accountNonExpired,
119 credentialsNonExpired, true, authorities);
120 }
121
122 /***
123 * Construct the <code>User</code> with the details required by {@link
124 * DaoAuthenticationProvider}.
125 *
126 * @param username
127 * the username presented to the
128 * <code>DaoAuthenticationProvider</code>
129 * @param password
130 * the password that should be presented to the
131 * <code>DaoAuthenticationProvider</code>
132 * @param enabled
133 * set to <code>true</code> if the user is enabled
134 * @param accountNonExpired
135 * set to <code>true</code> if the account has not expired
136 * @param credentialsNonExpired
137 * set to <code>true</code> if the credentials have not expired
138 * @param accountNonLocked
139 * set to <code>true</code> if the account is not locked
140 * @param authorities
141 * the authorities that should be granted to the caller if they
142 * presented the correct username and password and the user is
143 * enabled
144 *
145 * @throws IllegalArgumentException
146 * if a <code>null</code> value was passed either as a
147 * parameter or as an element in the
148 * <code>GrantedAuthority[]</code> array
149 */
150 public User(String username, String password, boolean enabled,
151 boolean accountNonExpired, boolean credentialsNonExpired,
152 boolean accountNonLocked, GrantedAuthority[] authorities)
153 throws IllegalArgumentException {
154 if (((username == null) || "".equals(username)) || (password == null)) {
155 throw new IllegalArgumentException(
156 "Cannot pass null or empty values to constructor");
157 }
158
159 this.username = username;
160 this.password = password;
161 this.enabled = enabled;
162 this.accountNonExpired = accountNonExpired;
163 this.credentialsNonExpired = credentialsNonExpired;
164 this.accountNonLocked = accountNonLocked;
165 setAuthorities(authorities);
166 }
167
168
169
170
171 public boolean equals(Object rhs) {
172 if (!(rhs instanceof User) || (rhs == null)) {
173 return false;
174 }
175
176 User user = (User) rhs;
177
178
179
180 if (user.getAuthorities().length != this.getAuthorities().length) {
181 return false;
182 }
183
184 for (int i = 0; i < this.getAuthorities().length; i++) {
185 if (!this.getAuthorities()[i].equals(user.getAuthorities()[i])) {
186 return false;
187 }
188 }
189
190
191 return (this.getPassword().equals(user.getPassword())
192 && this.getUsername().equals(user.getUsername())
193 && (this.isAccountNonExpired() == user.isAccountNonExpired())
194 && (this.isAccountNonLocked() == user.isAccountNonLocked())
195 && (this.isCredentialsNonExpired() == user
196 .isCredentialsNonExpired()) && (this.isEnabled() == user
197 .isEnabled()));
198 }
199
200 public GrantedAuthority[] getAuthorities() {
201 return authorities;
202 }
203
204 public String getPassword() {
205 return password;
206 }
207
208 public String getUsername() {
209 return username;
210 }
211
212 public boolean isAccountNonExpired() {
213 return accountNonExpired;
214 }
215
216 public boolean isAccountNonLocked() {
217 return this.accountNonLocked;
218 }
219
220 public boolean isCredentialsNonExpired() {
221 return credentialsNonExpired;
222 }
223
224 public boolean isEnabled() {
225 return enabled;
226 }
227
228 protected void setAuthorities(GrantedAuthority[] authorities) {
229 Assert
230 .notNull(authorities,
231 "Cannot pass a null GrantedAuthority array");
232
233 for (int i = 0; i < authorities.length; i++) {
234 Assert
235 .notNull(
236 authorities[i],
237 "Granted authority element "
238 + i
239 + " is null - GrantedAuthority[] cannot contain any null elements");
240 }
241
242 this.authorities = authorities;
243 }
244
245 public String toString() {
246 StringBuffer sb = new StringBuffer();
247 sb.append(super.toString() + ": ");
248 sb.append("Username: " + this.username + "; ");
249 sb.append("Password: [PROTECTED]; ");
250 sb.append("Enabled: " + this.enabled + "; ");
251 sb.append("AccountNonExpired: " + this.accountNonExpired + "; ");
252 sb
253 .append("credentialsNonExpired: " + this.credentialsNonExpired
254 + "; ");
255 sb.append("AccountNonLocked: " + this.accountNonLocked + "; ");
256
257 if (this.getAuthorities() != null) {
258 sb.append("Granted Authorities: ");
259
260 for (int i = 0; i < this.getAuthorities().length; i++) {
261 if (i > 0) {
262 sb.append(", ");
263 }
264
265 sb.append(this.getAuthorities()[i].toString());
266 }
267 } else {
268 sb.append("Not granted any authorities");
269 }
270
271 return sb.toString();
272 }
273 }