SEC-177: Add hashCode() method.

This commit is contained in:
Ben Alex 2006-02-09 03:45:47 +00:00
parent c9cee6651c
commit 74de83e5f1
1 changed files with 223 additions and 207 deletions

View File

@ -1,4 +1,4 @@
/* Copyright 2004, 2005 Acegi Technology Pty Limited
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,41 +16,39 @@
package org.acegisecurity.userdetails;
import org.acegisecurity.GrantedAuthority;
import org.springframework.util.Assert;
/**
* Models core user information retieved by an {@link UserDetailsService}.
*
* <p>
* Implemented with value object semantics (immutable after construction, like a
* <code>String</code>). Developers may use this class directly, subclass it,
* or write their own {@link UserDetails} implementation from scratch.
* Implemented with value object semantics (immutable after construction, like
* a <code>String</code>). Developers may use this class directly, subclass
* it, or write their own {@link UserDetails} implementation from scratch.
* </p>
*
* @author Ben Alex
* @version $Id$
*/
public class User implements UserDetails {
//~ Instance fields ========================================================
// ~ Instance fields
// ========================================================
private String password;
private String username;
private GrantedAuthority[] authorities;
private boolean accountNonExpired;
private boolean accountNonLocked;
private boolean credentialsNonExpired;
private boolean enabled;
//~ Constructors ===========================================================
// ~ Constructors
// ===========================================================
protected User() {
throw new IllegalArgumentException("Cannot use default constructor");
}
@ -59,22 +57,17 @@ public class User implements UserDetails {
* Construct the <code>User</code> with the details required by {@link
* DaoAuthenticationProvider}.
*
* @param username
* the username presented to the
* @param username the username presented to the
* <code>DaoAuthenticationProvider</code>
* @param password
* the password that should be presented to the
* @param password the password that should be presented to the
* <code>DaoAuthenticationProvider</code>
* @param enabled
* set to <code>true</code> if the user is enabled
* @param authorities
* the authorities that should be granted to the caller if they
* presented the correct username and password and the user is
* enabled
* @param enabled set to <code>true</code> if the user is enabled
* @param authorities the authorities that should be granted to the caller
* if they presented the correct username and password and the user
* is enabled
*
* @throws IllegalArgumentException
* if a <code>null</code> value was passed either as a
* parameter or as an element in the
* @throws IllegalArgumentException if a <code>null</code> value was passed
* either as a parameter or as an element in the
* <code>GrantedAuthority[]</code> array
*
* @deprecated use new constructor with extended properties (this
@ -89,26 +82,21 @@ public class User implements UserDetails {
* Construct the <code>User</code> with the details required by {@link
* DaoAuthenticationProvider}.
*
* @param username
* the username presented to the
* @param username the username presented to the
* <code>DaoAuthenticationProvider</code>
* @param password
* the password that should be presented to the
* @param password the password that should be presented to the
* <code>DaoAuthenticationProvider</code>
* @param enabled
* set to <code>true</code> if the user is enabled
* @param accountNonExpired
* set to <code>true</code> if the account has not expired
* @param credentialsNonExpired
* set to <code>true</code> if the credentials have not expired
* @param authorities
* the authorities that should be granted to the caller if they
* presented the correct username and password and the user is
* enabled
* @param enabled set to <code>true</code> if the user is enabled
* @param accountNonExpired set to <code>true</code> if the account has not
* expired
* @param credentialsNonExpired set to <code>true</code> if the credentials
* have not expired
* @param authorities the authorities that should be granted to the caller
* if they presented the correct username and password and the user
* is enabled
*
* @throws IllegalArgumentException
* if a <code>null</code> value was passed either as a
* parameter or as an element in the
* @throws IllegalArgumentException if a <code>null</code> value was passed
* either as a parameter or as an element in the
* <code>GrantedAuthority[]</code> array
*
* @deprecated use new constructor with extended properties (this
@ -125,28 +113,23 @@ public class User implements UserDetails {
* Construct the <code>User</code> with the details required by {@link
* DaoAuthenticationProvider}.
*
* @param username
* the username presented to the
* @param username the username presented to the
* <code>DaoAuthenticationProvider</code>
* @param password
* the password that should be presented to the
* @param password the password that should be presented to the
* <code>DaoAuthenticationProvider</code>
* @param enabled
* set to <code>true</code> if the user is enabled
* @param accountNonExpired
* set to <code>true</code> if the account has not expired
* @param credentialsNonExpired
* set to <code>true</code> if the credentials have not expired
* @param accountNonLocked
* set to <code>true</code> if the account is not locked
* @param authorities
* the authorities that should be granted to the caller if they
* presented the correct username and password and the user is
* enabled
* @param enabled set to <code>true</code> if the user is enabled
* @param accountNonExpired set to <code>true</code> if the account has not
* expired
* @param credentialsNonExpired set to <code>true</code> if the credentials
* have not expired
* @param accountNonLocked set to <code>true</code> if the account is not
* locked
* @param authorities the authorities that should be granted to the caller
* if they presented the correct username and password and the user
* is enabled
*
* @throws IllegalArgumentException
* if a <code>null</code> value was passed either as a
* parameter or as an element in the
* @throws IllegalArgumentException if a <code>null</code> value was passed
* either as a parameter or as an element in the
* <code>GrantedAuthority[]</code> array
*/
public User(String username, String password, boolean enabled,
@ -167,8 +150,7 @@ public class User implements UserDetails {
setAuthorities(authorities);
}
// ~ Methods
// ================================================================
//~ Methods ================================================================
public boolean equals(Object rhs) {
if (!(rhs instanceof User) || (rhs == null)) {
@ -194,9 +176,8 @@ public class User implements UserDetails {
&& this.getUsername().equals(user.getUsername())
&& (this.isAccountNonExpired() == user.isAccountNonExpired())
&& (this.isAccountNonLocked() == user.isAccountNonLocked())
&& (this.isCredentialsNonExpired() == user
.isCredentialsNonExpired()) && (this.isEnabled() == user
.isEnabled()));
&& (this.isCredentialsNonExpired() == user.isCredentialsNonExpired())
&& (this.isEnabled() == user.isEnabled()));
}
public GrantedAuthority[] getAuthorities() {
@ -211,6 +192,44 @@ public class User implements UserDetails {
return username;
}
// ~ Methods
// ================================================================
public int hashCode() {
int code = 9792;
if (this.getAuthorities() != null) {
for (int i = 0; i < this.getAuthorities().length; i++) {
code = code * (this.getAuthorities()[i].hashCode() % 7);
}
}
if (this.getPassword() != null) {
code = code * (this.getPassword().hashCode() % 7);
}
if (this.getUsername() != null) {
code = code * (this.getUsername().hashCode() % 7);
}
if (this.isAccountNonExpired()) {
code = code * -2;
}
if (this.isAccountNonLocked()) {
code = code * -3;
}
if (this.isCredentialsNonExpired()) {
code = code * -5;
}
if (this.isEnabled()) {
code = code * -7;
}
return code;
}
public boolean isAccountNonExpired() {
return accountNonExpired;
}
@ -228,13 +247,11 @@ public class User implements UserDetails {
}
protected void setAuthorities(GrantedAuthority[] authorities) {
Assert.notNull(authorities,
"Cannot pass a null GrantedAuthority array");
Assert.notNull(authorities, "Cannot pass a null GrantedAuthority array");
for (int i = 0; i < authorities.length; i++) {
Assert.notNull(authorities[i],
"Granted authority element "
+ i
"Granted authority element " + i
+ " is null - GrantedAuthority[] cannot contain any null elements");
}
@ -248,8 +265,7 @@ public class User implements UserDetails {
sb.append("Password: [PROTECTED]; ");
sb.append("Enabled: " + this.enabled + "; ");
sb.append("AccountNonExpired: " + this.accountNonExpired + "; ");
sb.append("credentialsNonExpired: " + this.credentialsNonExpired
+ "; ");
sb.append("credentialsNonExpired: " + this.credentialsNonExpired + "; ");
sb.append("AccountNonLocked: " + this.accountNonLocked + "; ");
if (this.getAuthorities() != null) {