浏览代码

Allow subclasses to make modifications to GrantedAuthority[].

Ben Alex 20 年之前
父节点
当前提交
6585c2b391
共有 1 个文件被更改,包括 46 次插入44 次删除
  1. 46 44
      core/src/main/java/org/acegisecurity/userdetails/User.java

+ 46 - 44
core/src/main/java/org/acegisecurity/userdetails/User.java

@@ -29,9 +29,6 @@ import org.springframework.util.Assert;
  * 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 ========================================================
@@ -46,6 +43,10 @@ public class User implements UserDetails {
 
     //~ Constructors ===========================================================
 
+    protected User() {
+        throw new IllegalArgumentException("Cannot use default constructor");
+    }
+
     /**
      * Construct the <code>User</code> 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() + ": ");