Coverage Report - org.acegisecurity.userdetails.ldap.LdapUserDetailsImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
LdapUserDetailsImpl
100% 
N/A 
1
 
 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.ldap;
 17  
 
 18  
 import org.acegisecurity.GrantedAuthority;
 19  
 
 20  
 import org.springframework.util.Assert;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.Arrays;
 24  
 import java.util.List;
 25  
 
 26  
 import javax.naming.directory.Attributes;
 27  
 import javax.naming.directory.BasicAttributes;
 28  
 import javax.naming.ldap.Control;
 29  
 
 30  
 
 31  
 /**
 32  
  * A UserDetails implementation which is used internally by the Ldap services. It also contains the user's
 33  
  * distinguished name and a set of attributes that have been retrieved from the Ldap server.<p>An instance may be
 34  
  * created as the result of a search, or when user information is retrieved during authentication.</p>
 35  
  *  <p>An instance of this class will be used by the <tt>LdapAuthenticationProvider</tt> to construct the final
 36  
  * user details object that it returns.</p>
 37  
  *
 38  
  * @author Luke Taylor
 39  
  * @version $Id$
 40  
  */
 41  152
 public class LdapUserDetailsImpl implements LdapUserDetails {
 42  
     //~ Static fields/initializers =====================================================================================
 43  
 
 44  
     private static final long serialVersionUID = 1L;
 45  1
     private static final GrantedAuthority[] NO_AUTHORITIES = new GrantedAuthority[0];
 46  1
     private static final Control[] NO_CONTROLS = new Control[0];
 47  
 
 48  
     //~ Instance fields ================================================================================================
 49  
 
 50  31
     private Attributes attributes = new BasicAttributes();
 51  
     private String dn;
 52  
     private String password;
 53  
     private String username;
 54  31
     private GrantedAuthority[] authorities = NO_AUTHORITIES;
 55  31
     private Control[] controls = NO_CONTROLS;
 56  31
     private boolean accountNonExpired = true;
 57  31
     private boolean accountNonLocked = true;
 58  31
     private boolean credentialsNonExpired = true;
 59  31
     private boolean enabled = true;
 60  
 
 61  
     //~ Constructors ===================================================================================================
 62  
 
 63  62
     protected LdapUserDetailsImpl() {}
 64  
 
 65  
     //~ Methods ========================================================================================================
 66  
 
 67  
     public Attributes getAttributes() {
 68  11
         return attributes;
 69  
     }
 70  
 
 71  
     public GrantedAuthority[] getAuthorities() {
 72  10
         return authorities;
 73  
     }
 74  
 
 75  
     public Control[] getControls() {
 76  3
         return controls;
 77  
     }
 78  
 
 79  
     public String getDn() {
 80  10
         return dn;
 81  
     }
 82  
 
 83  
     public String getPassword() {
 84  17
         return password;
 85  
     }
 86  
 
 87  
     public String getUsername() {
 88  13
         return username;
 89  
     }
 90  
 
 91  
     public boolean isAccountNonExpired() {
 92  3
         return accountNonExpired;
 93  
     }
 94  
 
 95  
     public boolean isAccountNonLocked() {
 96  3
         return accountNonLocked;
 97  
     }
 98  
 
 99  
     public boolean isCredentialsNonExpired() {
 100  3
         return credentialsNonExpired;
 101  
     }
 102  
 
 103  
     public boolean isEnabled() {
 104  3
         return enabled;
 105  
     }
 106  
 
 107  
     //~ Inner Classes ==================================================================================================
 108  
 
 109  
     /**
 110  
      * Variation of essence pattern. Used to create mutable intermediate object
 111  
      */
 112  
     public static class Essence {
 113  31
         private LdapUserDetailsImpl instance = createTarget();
 114  31
         private List mutableAuthorities = new ArrayList();
 115  
 
 116  56
         public Essence() {}
 117  
 
 118  3
         public Essence(LdapUserDetails copyMe) {
 119  3
             setDn(copyMe.getDn());
 120  3
             setAttributes(copyMe.getAttributes());
 121  3
             setUsername(copyMe.getUsername());
 122  3
             setPassword(copyMe.getPassword());
 123  3
             setEnabled(copyMe.isEnabled());
 124  3
             setAccountNonExpired(copyMe.isAccountNonExpired());
 125  3
             setCredentialsNonExpired(copyMe.isCredentialsNonExpired());
 126  3
             setAccountNonLocked(copyMe.isAccountNonLocked());
 127  3
             setControls(copyMe.getControls());
 128  3
             setAuthorities(copyMe.getAuthorities());
 129  3
         }
 130  
 
 131  
         LdapUserDetailsImpl createTarget() {
 132  31
             return new LdapUserDetailsImpl();
 133  
         }
 134  
 
 135  
         public Essence addAuthority(GrantedAuthority a) {
 136  10
             mutableAuthorities.add(a);
 137  
 
 138  10
             return this;
 139  
         }
 140  
 
 141  
         public LdapUserDetails createUserDetails() {
 142  
             //TODO: Validation of properties
 143  28
             Assert.notNull(instance, "Essence can only be used to create a single instance");
 144  
 
 145  28
             instance.authorities = getGrantedAuthorities();
 146  
 
 147  28
             LdapUserDetails newInstance = instance;
 148  
 
 149  28
             instance = null;
 150  
 
 151  28
             return newInstance;
 152  
         }
 153  
 
 154  
         public GrantedAuthority[] getGrantedAuthorities() {
 155  32
             return (GrantedAuthority[]) mutableAuthorities.toArray(new GrantedAuthority[0]);
 156  
         }
 157  
 
 158  
         public Essence setAccountNonExpired(boolean accountNonExpired) {
 159  3
             instance.accountNonExpired = accountNonExpired;
 160  
 
 161  3
             return this;
 162  
         }
 163  
 
 164  
         public Essence setAccountNonLocked(boolean accountNonLocked) {
 165  3
             instance.accountNonLocked = accountNonLocked;
 166  
 
 167  3
             return this;
 168  
         }
 169  
 
 170  
         public Essence setAttributes(Attributes attributes) {
 171  26
             instance.attributes = attributes;
 172  
 
 173  26
             return this;
 174  
         }
 175  
 
 176  
         public Essence setAuthorities(GrantedAuthority[] authorities) {
 177  3
             mutableAuthorities = new ArrayList(Arrays.asList(authorities));
 178  
 
 179  3
             return this;
 180  
         }
 181  
 
 182  
         public void setControls(Control[] controls) {
 183  3
             instance.controls = controls;
 184  3
         }
 185  
 
 186  
         public Essence setCredentialsNonExpired(boolean credentialsNonExpired) {
 187  3
             instance.credentialsNonExpired = credentialsNonExpired;
 188  
 
 189  3
             return this;
 190  
         }
 191  
 
 192  
         public Essence setDn(String dn) {
 193  31
             instance.dn = dn;
 194  
 
 195  31
             return this;
 196  
         }
 197  
 
 198  
         public Essence setEnabled(boolean enabled) {
 199  3
             instance.enabled = enabled;
 200  
 
 201  3
             return this;
 202  
         }
 203  
 
 204  
         public Essence setPassword(String password) {
 205  27
             instance.password = password;
 206  
 
 207  27
             return this;
 208  
         }
 209  
 
 210  
         public Essence setUsername(String username) {
 211  25
             instance.username = username;
 212  
 
 213  25
             return this;
 214  
         }
 215  
     }
 216  
 }