diff --git a/core/src/main/java/org/acegisecurity/userdetails/User.java b/core/src/main/java/org/acegisecurity/userdetails/User.java index 5f44bd0509..a61326deda 100644 --- a/core/src/main/java/org/acegisecurity/userdetails/User.java +++ b/core/src/main/java/org/acegisecurity/userdetails/User.java @@ -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,256 +16,272 @@ package org.acegisecurity.userdetails; import org.acegisecurity.GrantedAuthority; + import org.springframework.util.Assert; + /** * Models core user information retieved by an {@link UserDetailsService}. * *
- * Implemented with value object semantics (immutable after construction, like a
- * String). 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 String). Developers may use this class directly, subclass
+ * it, or write their own {@link UserDetails} implementation from scratch.
*
User with the details required by {@link
+ * DaoAuthenticationProvider}.
+ *
+ * @param username the username presented to the
+ * DaoAuthenticationProvider
+ * @param password the password that should be presented to the
+ * DaoAuthenticationProvider
+ * @param enabled set to true 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 null value was passed
+ * either as a parameter or as an element in the
+ * GrantedAuthority[] array
+ *
+ * @deprecated use new constructor with extended properties (this
+ * constructor will be removed from release 1.0.0)
+ */
+ public User(String username, String password, boolean enabled,
+ GrantedAuthority[] authorities) throws IllegalArgumentException {
+ this(username, password, enabled, true, true, authorities);
+ }
- private boolean accountNonLocked;
+ /**
+ * Construct the User with the details required by {@link
+ * DaoAuthenticationProvider}.
+ *
+ * @param username the username presented to the
+ * DaoAuthenticationProvider
+ * @param password the password that should be presented to the
+ * DaoAuthenticationProvider
+ * @param enabled set to true if the user is enabled
+ * @param accountNonExpired set to true if the account has not
+ * expired
+ * @param credentialsNonExpired set to true 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 null value was passed
+ * either as a parameter or as an element in the
+ * GrantedAuthority[] array
+ *
+ * @deprecated use new constructor with extended properties (this
+ * constructor will be removed from release 1.0.0)
+ */
+ public User(String username, String password, boolean enabled,
+ boolean accountNonExpired, boolean credentialsNonExpired,
+ GrantedAuthority[] authorities) throws IllegalArgumentException {
+ this(username, password, enabled, accountNonExpired,
+ credentialsNonExpired, true, authorities);
+ }
- private boolean credentialsNonExpired;
+ /**
+ * Construct the User with the details required by {@link
+ * DaoAuthenticationProvider}.
+ *
+ * @param username the username presented to the
+ * DaoAuthenticationProvider
+ * @param password the password that should be presented to the
+ * DaoAuthenticationProvider
+ * @param enabled set to true if the user is enabled
+ * @param accountNonExpired set to true if the account has not
+ * expired
+ * @param credentialsNonExpired set to true if the credentials
+ * have not expired
+ * @param accountNonLocked set to true 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 null value was passed
+ * either as a parameter or as an element in the
+ * GrantedAuthority[] array
+ */
+ public User(String username, String password, boolean enabled,
+ boolean accountNonExpired, boolean credentialsNonExpired,
+ boolean accountNonLocked, GrantedAuthority[] authorities)
+ throws IllegalArgumentException {
+ if (((username == null) || "".equals(username)) || (password == null)) {
+ throw new IllegalArgumentException(
+ "Cannot pass null or empty values to constructor");
+ }
- private boolean enabled;
+ this.username = username;
+ this.password = password;
+ this.enabled = enabled;
+ this.accountNonExpired = accountNonExpired;
+ this.credentialsNonExpired = credentialsNonExpired;
+ this.accountNonLocked = accountNonLocked;
+ setAuthorities(authorities);
+ }
- // ~ Constructors
- // ===========================================================
+ //~ Methods ================================================================
- protected User() {
- throw new IllegalArgumentException("Cannot use default constructor");
- }
+ public boolean equals(Object rhs) {
+ if (!(rhs instanceof User) || (rhs == null)) {
+ return false;
+ }
- /**
- * Construct the User with the details required by {@link
- * DaoAuthenticationProvider}.
- *
- * @param username
- * the username presented to the
- * DaoAuthenticationProvider
- * @param password
- * the password that should be presented to the
- * DaoAuthenticationProvider
- * @param enabled
- * set to true 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 null value was passed either as a
- * parameter or as an element in the
- * GrantedAuthority[] array
- *
- * @deprecated use new constructor with extended properties (this
- * constructor will be removed from release 1.0.0)
- */
- public User(String username, String password, boolean enabled,
- GrantedAuthority[] authorities) throws IllegalArgumentException {
- this(username, password, enabled, true, true, authorities);
- }
+ User user = (User) rhs;
- /**
- * Construct the User with the details required by {@link
- * DaoAuthenticationProvider}.
- *
- * @param username
- * the username presented to the
- * DaoAuthenticationProvider
- * @param password
- * the password that should be presented to the
- * DaoAuthenticationProvider
- * @param enabled
- * set to true if the user is enabled
- * @param accountNonExpired
- * set to true if the account has not expired
- * @param credentialsNonExpired
- * set to true 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 null value was passed either as a
- * parameter or as an element in the
- * GrantedAuthority[] array
- *
- * @deprecated use new constructor with extended properties (this
- * constructor will be removed from release 1.0.0)
- */
- public User(String username, String password, boolean enabled,
- boolean accountNonExpired, boolean credentialsNonExpired,
- GrantedAuthority[] authorities) throws IllegalArgumentException {
- this(username, password, enabled, accountNonExpired,
- credentialsNonExpired, true, authorities);
- }
+ // We rely on constructor to guarantee any User has non-null and >0
+ // authorities
+ if (user.getAuthorities().length != this.getAuthorities().length) {
+ return false;
+ }
- /**
- * Construct the User with the details required by {@link
- * DaoAuthenticationProvider}.
- *
- * @param username
- * the username presented to the
- * DaoAuthenticationProvider
- * @param password
- * the password that should be presented to the
- * DaoAuthenticationProvider
- * @param enabled
- * set to true if the user is enabled
- * @param accountNonExpired
- * set to true if the account has not expired
- * @param credentialsNonExpired
- * set to true if the credentials have not expired
- * @param accountNonLocked
- * set to true 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 null value was passed either as a
- * parameter or as an element in the
- * GrantedAuthority[] array
- */
- public User(String username, String password, boolean enabled,
- boolean accountNonExpired, boolean credentialsNonExpired,
- boolean accountNonLocked, GrantedAuthority[] authorities)
- throws IllegalArgumentException {
- if (((username == null) || "".equals(username)) || (password == null)) {
- throw new IllegalArgumentException(
- "Cannot pass null or empty values to constructor");
- }
+ for (int i = 0; i < this.getAuthorities().length; i++) {
+ if (!this.getAuthorities()[i].equals(user.getAuthorities()[i])) {
+ return false;
+ }
+ }
- this.username = username;
- this.password = password;
- this.enabled = enabled;
- this.accountNonExpired = accountNonExpired;
- this.credentialsNonExpired = credentialsNonExpired;
- this.accountNonLocked = accountNonLocked;
- setAuthorities(authorities);
- }
+ // We rely on constructor to guarantee non-null username and password
+ return (this.getPassword().equals(user.getPassword())
+ && this.getUsername().equals(user.getUsername())
+ && (this.isAccountNonExpired() == user.isAccountNonExpired())
+ && (this.isAccountNonLocked() == user.isAccountNonLocked())
+ && (this.isCredentialsNonExpired() == user.isCredentialsNonExpired())
+ && (this.isEnabled() == user.isEnabled()));
+ }
- // ~ Methods
- // ================================================================
+ public GrantedAuthority[] getAuthorities() {
+ return authorities;
+ }
- public boolean equals(Object rhs) {
- if (!(rhs instanceof User) || (rhs == null)) {
- return false;
- }
+ public String getPassword() {
+ return password;
+ }
- User user = (User) rhs;
+ public String getUsername() {
+ return username;
+ }
- // We rely on constructor to guarantee any User has non-null and >0
- // authorities
- if (user.getAuthorities().length != this.getAuthorities().length) {
- return false;
- }
+ // ~ Methods
+ // ================================================================
+ public int hashCode() {
+ int code = 9792;
- for (int i = 0; i < this.getAuthorities().length; i++) {
- if (!this.getAuthorities()[i].equals(user.getAuthorities()[i])) {
- return false;
- }
- }
+ if (this.getAuthorities() != null) {
+ for (int i = 0; i < this.getAuthorities().length; i++) {
+ code = code * (this.getAuthorities()[i].hashCode() % 7);
+ }
+ }
- // We rely on constructor to guarantee non-null username and password
- return (this.getPassword().equals(user.getPassword())
- && this.getUsername().equals(user.getUsername())
- && (this.isAccountNonExpired() == user.isAccountNonExpired())
- && (this.isAccountNonLocked() == user.isAccountNonLocked())
- && (this.isCredentialsNonExpired() == user
- .isCredentialsNonExpired()) && (this.isEnabled() == user
- .isEnabled()));
- }
+ if (this.getPassword() != null) {
+ code = code * (this.getPassword().hashCode() % 7);
+ }
- public GrantedAuthority[] getAuthorities() {
- return authorities;
- }
+ if (this.getUsername() != null) {
+ code = code * (this.getUsername().hashCode() % 7);
+ }
- public String getPassword() {
- return password;
- }
+ if (this.isAccountNonExpired()) {
+ code = code * -2;
+ }
- public String getUsername() {
- return username;
- }
+ if (this.isAccountNonLocked()) {
+ code = code * -3;
+ }
- public boolean isAccountNonExpired() {
- return accountNonExpired;
- }
+ if (this.isCredentialsNonExpired()) {
+ code = code * -5;
+ }
- public boolean isAccountNonLocked() {
- return this.accountNonLocked;
- }
+ if (this.isEnabled()) {
+ code = code * -7;
+ }
- public boolean isCredentialsNonExpired() {
- return credentialsNonExpired;
- }
+ return code;
+ }
- public boolean isEnabled() {
- return enabled;
- }
+ public boolean isAccountNonExpired() {
+ return accountNonExpired;
+ }
- protected void setAuthorities(GrantedAuthority[] authorities) {
- Assert.notNull(authorities,
- "Cannot pass a null GrantedAuthority array");
+ public boolean isAccountNonLocked() {
+ return this.accountNonLocked;
+ }
- for (int i = 0; i < authorities.length; i++) {
- Assert.notNull( authorities[i],
- "Granted authority element "
- + i
- + " is null - GrantedAuthority[] cannot contain any null elements");
- }
+ public boolean isCredentialsNonExpired() {
+ return credentialsNonExpired;
+ }
- this.authorities = authorities;
- }
+ public boolean isEnabled() {
+ return enabled;
+ }
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append(super.toString() + ": ");
- sb.append("Username: " + this.username + "; ");
- sb.append("Password: [PROTECTED]; ");
- sb.append("Enabled: " + this.enabled + "; ");
- sb.append("AccountNonExpired: " + this.accountNonExpired + "; ");
- sb.append("credentialsNonExpired: " + this.credentialsNonExpired
- + "; ");
- sb.append("AccountNonLocked: " + this.accountNonLocked + "; ");
+ protected void setAuthorities(GrantedAuthority[] authorities) {
+ Assert.notNull(authorities, "Cannot pass a null GrantedAuthority array");
- if (this.getAuthorities() != null) {
- sb.append("Granted Authorities: ");
+ for (int i = 0; i < authorities.length; i++) {
+ Assert.notNull(authorities[i],
+ "Granted authority element " + i
+ + " is null - GrantedAuthority[] cannot contain any null elements");
+ }
- for (int i = 0; i < this.getAuthorities().length; i++) {
- if (i > 0) {
- sb.append(", ");
- }
+ this.authorities = authorities;
+ }
- sb.append(this.getAuthorities()[i].toString());
- }
- } else {
- sb.append("Not granted any authorities");
- }
+ public String toString() {
+ StringBuffer sb = new StringBuffer();
+ sb.append(super.toString() + ": ");
+ sb.append("Username: " + this.username + "; ");
+ sb.append("Password: [PROTECTED]; ");
+ sb.append("Enabled: " + this.enabled + "; ");
+ sb.append("AccountNonExpired: " + this.accountNonExpired + "; ");
+ sb.append("credentialsNonExpired: " + this.credentialsNonExpired + "; ");
+ sb.append("AccountNonLocked: " + this.accountNonLocked + "; ");
- return sb.toString();
- }
+ if (this.getAuthorities() != null) {
+ sb.append("Granted Authorities: ");
+
+ for (int i = 0; i < this.getAuthorities().length; i++) {
+ if (i > 0) {
+ sb.append(", ");
+ }
+
+ sb.append(this.getAuthorities()[i].toString());
+ }
+ } else {
+ sb.append("Not granted any authorities");
+ }
+
+ return sb.toString();
+ }
}