Coverage Report - org.acegisecurity.userdetails.User
 
Classes in this File Line Coverage Branch Coverage Complexity
User
91% 
100% 
2.5
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.userdetails;
 17  
 
 18  
 import org.acegisecurity.GrantedAuthority;
 19  
 
 20  
 import org.springframework.util.Assert;
 21  
 
 22  
 
 23  
 /**
 24  
  * Models core user information retieved by an {@link UserDetailsService}.<p>Implemented with value object
 25  
  * semantics (immutable after construction, like a <code>String</code>). Developers may use this class directly,
 26  
  * subclass it, or write their own {@link UserDetails} implementation from scratch.</p>
 27  
  *
 28  
  * @author Ben Alex
 29  
  * @version $Id: User.java 1784 2007-02-24 21:00:24Z luke_t $
 30  
  */
 31  
 public class User implements UserDetails {
 32  
     //~ Instance fields ================================================================================================
 33  
 
 34  
     private static final long serialVersionUID = 1L;
 35  
     private String password;
 36  
     private String username;
 37  
     private GrantedAuthority[] authorities;
 38  
     private boolean accountNonExpired;
 39  
     private boolean accountNonLocked;
 40  
     private boolean credentialsNonExpired;
 41  
     private boolean enabled;
 42  
 
 43  
     //~ Constructors ===================================================================================================
 44  
 
 45  
     /**
 46  
      * Construct the <code>User</code> with the details required by
 47  
      * {@link org.acegisecurity.providers.dao.DaoAuthenticationProvider}.
 48  
      *
 49  
      * @param username the username presented to the
 50  
      *        <code>DaoAuthenticationProvider</code>
 51  
      * @param password the password that should be presented to the
 52  
      *        <code>DaoAuthenticationProvider</code>
 53  
      * @param enabled set to <code>true</code> if the user is enabled
 54  
      * @param authorities the authorities that should be granted to the caller
 55  
      *        if they presented the correct username and password and the user
 56  
      *        is enabled
 57  
      *
 58  
      * @throws IllegalArgumentException if a <code>null</code> value was passed
 59  
      *         either as a parameter or as an element in the
 60  
      *         <code>GrantedAuthority[]</code> array
 61  
      *
 62  
      * @deprecated use new constructor with extended properties (this
 63  
      *             constructor will be removed from release 1.0.0)
 64  
      */
 65  
     public User(String username, String password, boolean enabled, GrantedAuthority[] authorities)
 66  
         throws IllegalArgumentException {
 67  0
         this(username, password, enabled, true, true, authorities);
 68  0
     }
 69  
 
 70  
     /**
 71  
      * Construct the <code>User</code> with the details required by
 72  
      * {@link org.acegisecurity.providers.dao.DaoAuthenticationProvider}.
 73  
      *
 74  
      * @param username the username presented to the
 75  
      *        <code>DaoAuthenticationProvider</code>
 76  
      * @param password the password that should be presented to the
 77  
      *        <code>DaoAuthenticationProvider</code>
 78  
      * @param enabled set to <code>true</code> if the user is enabled
 79  
      * @param accountNonExpired set to <code>true</code> if the account has not
 80  
      *        expired
 81  
      * @param credentialsNonExpired set to <code>true</code> if the credentials
 82  
      *        have not expired
 83  
      * @param authorities the authorities that should be granted to the caller
 84  
      *        if they presented the correct username and password and the user
 85  
      *        is enabled
 86  
      *
 87  
      * @throws IllegalArgumentException if a <code>null</code> value was passed
 88  
      *         either as a parameter or as an element in the
 89  
      *         <code>GrantedAuthority[]</code> array
 90  
      *
 91  
      * @deprecated use new constructor with extended properties (this
 92  
      *             constructor will be removed from release 1.0.0)
 93  
      */
 94  
     public User(String username, String password, boolean enabled, boolean accountNonExpired,
 95  
         boolean credentialsNonExpired, GrantedAuthority[] authorities)
 96  
         throws IllegalArgumentException {
 97  0
         this(username, password, enabled, accountNonExpired, credentialsNonExpired, true, authorities);
 98  0
     }
 99  
 
 100  
     /**
 101  
      * Construct the <code>User</code> with the details required by
 102  
      * {@link org.acegisecurity.providers.dao.DaoAuthenticationProvider}.
 103  
      *
 104  
      * @param username the username presented to the
 105  
      *        <code>DaoAuthenticationProvider</code>
 106  
      * @param password the password that should be presented to the
 107  
      *        <code>DaoAuthenticationProvider</code>
 108  
      * @param enabled set to <code>true</code> if the user is enabled
 109  
      * @param accountNonExpired set to <code>true</code> if the account has not
 110  
      *        expired
 111  
      * @param credentialsNonExpired set to <code>true</code> if the credentials
 112  
      *        have not expired
 113  
      * @param accountNonLocked set to <code>true</code> if the account is not
 114  
      *        locked
 115  
      * @param authorities the authorities that should be granted to the caller
 116  
      *        if they presented the correct username and password and the user
 117  
      *        is enabled
 118  
      *
 119  
      * @throws IllegalArgumentException if a <code>null</code> value was passed
 120  
      *         either as a parameter or as an element in the
 121  
      *         <code>GrantedAuthority[]</code> array
 122  
      */
 123  
     public User(String username, String password, boolean enabled, boolean accountNonExpired,
 124  
         boolean credentialsNonExpired, boolean accountNonLocked, GrantedAuthority[] authorities)
 125  205
         throws IllegalArgumentException {
 126  205
         if (((username == null) || "".equals(username)) || (password == null)) {
 127  3
             throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
 128  
         }
 129  
 
 130  202
         this.username = username;
 131  202
         this.password = password;
 132  202
         this.enabled = enabled;
 133  202
         this.accountNonExpired = accountNonExpired;
 134  202
         this.credentialsNonExpired = credentialsNonExpired;
 135  202
         this.accountNonLocked = accountNonLocked;
 136  202
         setAuthorities(authorities);
 137  200
     }
 138  
 
 139  
     //~ Methods ========================================================================================================
 140  
 
 141  
     public boolean equals(Object rhs) {
 142  48
         if (!(rhs instanceof User) || (rhs == null)) {
 143  23
             return false;
 144  
         }
 145  
 
 146  25
         User user = (User) rhs;
 147  
 
 148  
         // We rely on constructor to guarantee any User has non-null and >0
 149  
         // authorities
 150  25
         if (user.getAuthorities().length != this.getAuthorities().length) {
 151  1
             return false;
 152  
         }
 153  
 
 154  69
         for (int i = 0; i < this.getAuthorities().length; i++) {
 155  45
             if (!this.getAuthorities()[i].equals(user.getAuthorities()[i])) {
 156  0
                 return false;
 157  
             }
 158  
         }
 159  
 
 160  
         // We rely on constructor to guarantee non-null username and password
 161  24
         return (this.getPassword().equals(user.getPassword()) && this.getUsername().equals(user.getUsername())
 162  
                 && (this.isAccountNonExpired() == user.isAccountNonExpired())
 163  
                 && (this.isAccountNonLocked() == user.isAccountNonLocked())
 164  
                 && (this.isCredentialsNonExpired() == user.isCredentialsNonExpired())
 165  
                 && (this.isEnabled() == user.isEnabled()));
 166  
     }
 167  
 
 168  
     public GrantedAuthority[] getAuthorities() {
 169  958
         return authorities;
 170  
     }
 171  
 
 172  
     public String getPassword() {
 173  225
         return password;
 174  
     }
 175  
 
 176  
     public String getUsername() {
 177  320
         return username;
 178  
     }
 179  
 
 180  
     public int hashCode() {
 181  58
         int code = 9792;
 182  
 
 183  58
         if (this.getAuthorities() != null) {
 184  175
             for (int i = 0; i < this.getAuthorities().length; i++) {
 185  117
                 code = code * (this.getAuthorities()[i].hashCode() % 7);
 186  
             }
 187  
         }
 188  
 
 189  58
         if (this.getPassword() != null) {
 190  58
             code = code * (this.getPassword().hashCode() % 7);
 191  
         }
 192  
 
 193  58
         if (this.getUsername() != null) {
 194  58
             code = code * (this.getUsername().hashCode() % 7);
 195  
         }
 196  
 
 197  58
         if (this.isAccountNonExpired()) {
 198  58
             code = code * -2;
 199  
         }
 200  
 
 201  58
         if (this.isAccountNonLocked()) {
 202  58
             code = code * -3;
 203  
         }
 204  
 
 205  58
         if (this.isCredentialsNonExpired()) {
 206  58
             code = code * -5;
 207  
         }
 208  
 
 209  58
         if (this.isEnabled()) {
 210  57
             code = code * -7;
 211  
         }
 212  
 
 213  58
         return code;
 214  
     }
 215  
 
 216  
     public boolean isAccountNonExpired() {
 217  140
         return accountNonExpired;
 218  
     }
 219  
 
 220  
     public boolean isAccountNonLocked() {
 221  141
         return this.accountNonLocked;
 222  
     }
 223  
 
 224  
     public boolean isCredentialsNonExpired() {
 225  126
         return credentialsNonExpired;
 226  
     }
 227  
 
 228  
     public boolean isEnabled() {
 229  149
         return enabled;
 230  
     }
 231  
 
 232  
     protected void setAuthorities(GrantedAuthority[] authorities) {
 233  202
         Assert.notNull(authorities, "Cannot pass a null GrantedAuthority array");
 234  
 
 235  559
         for (int i = 0; i < authorities.length; i++) {
 236  359
             Assert.notNull(authorities[i],
 237  
                 "Granted authority element " + i + " is null - GrantedAuthority[] cannot contain any null elements");
 238  
         }
 239  
 
 240  200
         this.authorities = authorities;
 241  200
     }
 242  
 
 243  
     public String toString() {
 244  57
         StringBuffer sb = new StringBuffer();
 245  57
         sb.append(super.toString()).append(": ");
 246  57
         sb.append("Username: ").append(this.username).append("; ");
 247  57
         sb.append("Password: [PROTECTED]; ");
 248  57
         sb.append("Enabled: ").append(this.enabled).append("; ");
 249  57
         sb.append("AccountNonExpired: ").append(this.accountNonExpired).append("; ");
 250  57
         sb.append("credentialsNonExpired: ").append(this.credentialsNonExpired).append("; ");
 251  57
         sb.append("AccountNonLocked: ").append(this.accountNonLocked).append("; ");
 252  
 
 253  57
         if (this.getAuthorities() != null) {
 254  57
             sb.append("Granted Authorities: ");
 255  
 
 256  172
             for (int i = 0; i < this.getAuthorities().length; i++) {
 257  115
                 if (i > 0) {
 258  58
                     sb.append(", ");
 259  
                 }
 260  
 
 261  115
                 sb.append(this.getAuthorities()[i].toString());
 262  
             }
 263  
         } else {
 264  0
             sb.append("Not granted any authorities");
 265  
         }
 266  
 
 267  57
         return sb.toString();
 268  
     }
 269  
 }