From 6585c2b3917b668c1dd940637425e30387f5ef8d Mon Sep 17 00:00:00 2001 From: Ben Alex Date: Sat, 26 Nov 2005 13:27:30 +0000 Subject: [PATCH] Allow subclasses to make modifications to GrantedAuthority[]. --- .../org/acegisecurity/userdetails/User.java | 90 ++++++++++--------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/core/src/main/java/org/acegisecurity/userdetails/User.java b/core/src/main/java/org/acegisecurity/userdetails/User.java index db38f476ee..aea0163c89 100644 --- a/core/src/main/java/org/acegisecurity/userdetails/User.java +++ b/core/src/main/java/org/acegisecurity/userdetails/User.java @@ -29,9 +29,6 @@ import org.springframework.util.Assert; * a String). Developers may use this class directly, subclass * it, or write their own {@link UserDetails} implementation from scratch. *

- * - * @author Ben Alex - * @version $Id$ */ public class User implements UserDetails { //~ Instance fields ======================================================== @@ -46,6 +43,10 @@ public class User implements UserDetails { //~ Constructors =========================================================== + protected User() { + throw new IllegalArgumentException("Cannot use default constructor"); + } + /** * Construct the User with the details required by {@link * DaoAuthenticationProvider}. @@ -129,61 +130,22 @@ public class User implements UserDetails { boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, GrantedAuthority[] authorities) throws IllegalArgumentException { - if (((username == null) || "".equals(username)) || (password == null) - || (authorities == null)) { + if (((username == null) || "".equals(username)) || (password == null)) { throw new IllegalArgumentException( "Cannot pass null or empty values to constructor"); } - for (int i = 0; i < authorities.length; i++) { - Assert.notNull(authorities[i], - "Granted authority element " + i - + " is null - GrantedAuthority[] cannot contain any null elements"); - } - this.username = username; this.password = password; this.enabled = enabled; - this.authorities = authorities; this.accountNonExpired = accountNonExpired; this.credentialsNonExpired = credentialsNonExpired; this.accountNonLocked = accountNonLocked; - } - - protected User() { - throw new IllegalArgumentException("Cannot use default constructor"); + setAuthorities(authorities); } //~ Methods ================================================================ - public boolean isAccountNonExpired() { - return accountNonExpired; - } - - public boolean isAccountNonLocked() { - return this.accountNonLocked; - } - - public GrantedAuthority[] getAuthorities() { - return authorities; - } - - public boolean isCredentialsNonExpired() { - return credentialsNonExpired; - } - - public boolean isEnabled() { - return enabled; - } - - public String getPassword() { - return password; - } - - public String getUsername() { - return username; - } - public boolean equals(Object rhs) { if (!(rhs instanceof User) || (rhs == null)) { return false; @@ -211,6 +173,46 @@ public class User implements UserDetails { && (this.isEnabled() == user.isEnabled())); } + public GrantedAuthority[] getAuthorities() { + return authorities; + } + + public String getPassword() { + return password; + } + + public String getUsername() { + return username; + } + + public boolean isAccountNonExpired() { + return accountNonExpired; + } + + public boolean isAccountNonLocked() { + return this.accountNonLocked; + } + + public boolean isCredentialsNonExpired() { + return credentialsNonExpired; + } + + public boolean isEnabled() { + return enabled; + } + + protected void setAuthorities(GrantedAuthority[] authorities) { + 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 + + " is null - GrantedAuthority[] cannot contain any null elements"); + } + + this.authorities = authorities; + } + public String toString() { StringBuffer sb = new StringBuffer(); sb.append(super.toString() + ": ");