001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.userdetails;
017:
018: import org.acegisecurity.GrantedAuthority;
019:
020: import org.springframework.util.Assert;
021:
022: /**
023: * Models core user information retieved by an {@link UserDetailsService}.<p>Implemented with value object
024: * semantics (immutable after construction, like a <code>String</code>). Developers may use this class directly,
025: * subclass it, or write their own {@link UserDetails} implementation from scratch.</p>
026: *
027: * @author Ben Alex
028: * @version $Id: User.java 1784 2007-02-24 21:00:24Z luke_t $
029: */
030: public class User implements UserDetails {
031: //~ Instance fields ================================================================================================
032:
033: private static final long serialVersionUID = 1L;
034: private String password;
035: private String username;
036: private GrantedAuthority[] authorities;
037: private boolean accountNonExpired;
038: private boolean accountNonLocked;
039: private boolean credentialsNonExpired;
040: private boolean enabled;
041:
042: //~ Constructors ===================================================================================================
043:
044: /**
045: * Construct the <code>User</code> with the details required by
046: * {@link org.acegisecurity.providers.dao.DaoAuthenticationProvider}.
047: *
048: * @param username the username presented to the
049: * <code>DaoAuthenticationProvider</code>
050: * @param password the password that should be presented to the
051: * <code>DaoAuthenticationProvider</code>
052: * @param enabled set to <code>true</code> if the user is enabled
053: * @param authorities the authorities that should be granted to the caller
054: * if they presented the correct username and password and the user
055: * is enabled
056: *
057: * @throws IllegalArgumentException if a <code>null</code> value was passed
058: * either as a parameter or as an element in the
059: * <code>GrantedAuthority[]</code> array
060: *
061: * @deprecated use new constructor with extended properties (this
062: * constructor will be removed from release 1.0.0)
063: */
064: public User(String username, String password, boolean enabled,
065: GrantedAuthority[] authorities)
066: throws IllegalArgumentException {
067: this (username, password, enabled, true, true, authorities);
068: }
069:
070: /**
071: * Construct the <code>User</code> with the details required by
072: * {@link org.acegisecurity.providers.dao.DaoAuthenticationProvider}.
073: *
074: * @param username the username presented to the
075: * <code>DaoAuthenticationProvider</code>
076: * @param password the password that should be presented to the
077: * <code>DaoAuthenticationProvider</code>
078: * @param enabled set to <code>true</code> if the user is enabled
079: * @param accountNonExpired set to <code>true</code> if the account has not
080: * expired
081: * @param credentialsNonExpired set to <code>true</code> if the credentials
082: * have not expired
083: * @param authorities the authorities that should be granted to the caller
084: * if they presented the correct username and password and the user
085: * is enabled
086: *
087: * @throws IllegalArgumentException if a <code>null</code> value was passed
088: * either as a parameter or as an element in the
089: * <code>GrantedAuthority[]</code> array
090: *
091: * @deprecated use new constructor with extended properties (this
092: * constructor will be removed from release 1.0.0)
093: */
094: public User(String username, String password, boolean enabled,
095: boolean accountNonExpired, boolean credentialsNonExpired,
096: GrantedAuthority[] authorities)
097: throws IllegalArgumentException {
098: this (username, password, enabled, accountNonExpired,
099: credentialsNonExpired, true, authorities);
100: }
101:
102: /**
103: * Construct the <code>User</code> with the details required by
104: * {@link org.acegisecurity.providers.dao.DaoAuthenticationProvider}.
105: *
106: * @param username the username presented to the
107: * <code>DaoAuthenticationProvider</code>
108: * @param password the password that should be presented to the
109: * <code>DaoAuthenticationProvider</code>
110: * @param enabled set to <code>true</code> if the user is enabled
111: * @param accountNonExpired set to <code>true</code> if the account has not
112: * expired
113: * @param credentialsNonExpired set to <code>true</code> if the credentials
114: * have not expired
115: * @param accountNonLocked set to <code>true</code> if the account is not
116: * locked
117: * @param authorities the authorities that should be granted to the caller
118: * if they presented the correct username and password and the user
119: * is enabled
120: *
121: * @throws IllegalArgumentException if a <code>null</code> value was passed
122: * either as a parameter or as an element in the
123: * <code>GrantedAuthority[]</code> array
124: */
125: public User(String username, String password, boolean enabled,
126: boolean accountNonExpired, boolean credentialsNonExpired,
127: boolean accountNonLocked, GrantedAuthority[] authorities)
128: throws IllegalArgumentException {
129: if (((username == null) || "".equals(username))
130: || (password == null)) {
131: throw new IllegalArgumentException(
132: "Cannot pass null or empty values to constructor");
133: }
134:
135: this .username = username;
136: this .password = password;
137: this .enabled = enabled;
138: this .accountNonExpired = accountNonExpired;
139: this .credentialsNonExpired = credentialsNonExpired;
140: this .accountNonLocked = accountNonLocked;
141: setAuthorities(authorities);
142: }
143:
144: //~ Methods ========================================================================================================
145:
146: public boolean equals(Object rhs) {
147: if (!(rhs instanceof User) || (rhs == null)) {
148: return false;
149: }
150:
151: User user = (User) rhs;
152:
153: // We rely on constructor to guarantee any User has non-null and >0
154: // authorities
155: if (user.getAuthorities().length != this .getAuthorities().length) {
156: return false;
157: }
158:
159: for (int i = 0; i < this .getAuthorities().length; i++) {
160: if (!this .getAuthorities()[i]
161: .equals(user.getAuthorities()[i])) {
162: return false;
163: }
164: }
165:
166: // We rely on constructor to guarantee non-null username and password
167: return (this .getPassword().equals(user.getPassword())
168: && this .getUsername().equals(user.getUsername())
169: && (this .isAccountNonExpired() == user
170: .isAccountNonExpired())
171: && (this .isAccountNonLocked() == user
172: .isAccountNonLocked())
173: && (this .isCredentialsNonExpired() == user
174: .isCredentialsNonExpired()) && (this
175: .isEnabled() == user.isEnabled()));
176: }
177:
178: public GrantedAuthority[] getAuthorities() {
179: return authorities;
180: }
181:
182: public String getPassword() {
183: return password;
184: }
185:
186: public String getUsername() {
187: return username;
188: }
189:
190: public int hashCode() {
191: int code = 9792;
192:
193: if (this .getAuthorities() != null) {
194: for (int i = 0; i < this .getAuthorities().length; i++) {
195: code = code * (this .getAuthorities()[i].hashCode() % 7);
196: }
197: }
198:
199: if (this .getPassword() != null) {
200: code = code * (this .getPassword().hashCode() % 7);
201: }
202:
203: if (this .getUsername() != null) {
204: code = code * (this .getUsername().hashCode() % 7);
205: }
206:
207: if (this .isAccountNonExpired()) {
208: code = code * -2;
209: }
210:
211: if (this .isAccountNonLocked()) {
212: code = code * -3;
213: }
214:
215: if (this .isCredentialsNonExpired()) {
216: code = code * -5;
217: }
218:
219: if (this .isEnabled()) {
220: code = code * -7;
221: }
222:
223: return code;
224: }
225:
226: public boolean isAccountNonExpired() {
227: return accountNonExpired;
228: }
229:
230: public boolean isAccountNonLocked() {
231: return this .accountNonLocked;
232: }
233:
234: public boolean isCredentialsNonExpired() {
235: return credentialsNonExpired;
236: }
237:
238: public boolean isEnabled() {
239: return enabled;
240: }
241:
242: protected void setAuthorities(GrantedAuthority[] authorities) {
243: Assert.notNull(authorities,
244: "Cannot pass a null GrantedAuthority array");
245:
246: for (int i = 0; i < authorities.length; i++) {
247: Assert
248: .notNull(
249: authorities[i],
250: "Granted authority element "
251: + i
252: + " is null - GrantedAuthority[] cannot contain any null elements");
253: }
254:
255: this .authorities = authorities;
256: }
257:
258: public String toString() {
259: StringBuffer sb = new StringBuffer();
260: sb.append(super .toString()).append(": ");
261: sb.append("Username: ").append(this .username).append("; ");
262: sb.append("Password: [PROTECTED]; ");
263: sb.append("Enabled: ").append(this .enabled).append("; ");
264: sb.append("AccountNonExpired: ").append(this .accountNonExpired)
265: .append("; ");
266: sb.append("credentialsNonExpired: ").append(
267: this .credentialsNonExpired).append("; ");
268: sb.append("AccountNonLocked: ").append(this .accountNonLocked)
269: .append("; ");
270:
271: if (this .getAuthorities() != null) {
272: sb.append("Granted Authorities: ");
273:
274: for (int i = 0; i < this .getAuthorities().length; i++) {
275: if (i > 0) {
276: sb.append(", ");
277: }
278:
279: sb.append(this .getAuthorities()[i].toString());
280: }
281: } else {
282: sb.append("Not granted any authorities");
283: }
284:
285: return sb.toString();
286: }
287: }
|