View Javadoc

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  public class LdapUserDetailsImpl implements LdapUserDetails {
42      //~ Static fields/initializers =====================================================================================
43  
44      private static final long serialVersionUID = 1L;
45      private static final GrantedAuthority[] NO_AUTHORITIES = new GrantedAuthority[0];
46      private static final Control[] NO_CONTROLS = new Control[0];
47  
48      //~ Instance fields ================================================================================================
49  
50      private Attributes attributes = new BasicAttributes();
51      private String dn;
52      private String password;
53      private String username;
54      private GrantedAuthority[] authorities = NO_AUTHORITIES;
55      private Control[] controls = NO_CONTROLS;
56      private boolean accountNonExpired = true;
57      private boolean accountNonLocked = true;
58      private boolean credentialsNonExpired = true;
59      private boolean enabled = true;
60  
61      //~ Constructors ===================================================================================================
62  
63      protected LdapUserDetailsImpl() {}
64  
65      //~ Methods ========================================================================================================
66  
67      public Attributes getAttributes() {
68          return attributes;
69      }
70  
71      public GrantedAuthority[] getAuthorities() {
72          return authorities;
73      }
74  
75      public Control[] getControls() {
76          return controls;
77      }
78  
79      public String getDn() {
80          return dn;
81      }
82  
83      public String getPassword() {
84          return password;
85      }
86  
87      public String getUsername() {
88          return username;
89      }
90  
91      public boolean isAccountNonExpired() {
92          return accountNonExpired;
93      }
94  
95      public boolean isAccountNonLocked() {
96          return accountNonLocked;
97      }
98  
99      public boolean isCredentialsNonExpired() {
100         return credentialsNonExpired;
101     }
102 
103     public boolean isEnabled() {
104         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         private LdapUserDetailsImpl instance = createTarget();
114         private List mutableAuthorities = new ArrayList();
115 
116         public Essence() {}
117 
118         public Essence(LdapUserDetails copyMe) {
119             setDn(copyMe.getDn());
120             setAttributes(copyMe.getAttributes());
121             setUsername(copyMe.getUsername());
122             setPassword(copyMe.getPassword());
123             setEnabled(copyMe.isEnabled());
124             setAccountNonExpired(copyMe.isAccountNonExpired());
125             setCredentialsNonExpired(copyMe.isCredentialsNonExpired());
126             setAccountNonLocked(copyMe.isAccountNonLocked());
127             setControls(copyMe.getControls());
128             setAuthorities(copyMe.getAuthorities());
129         }
130 
131         LdapUserDetailsImpl createTarget() {
132             return new LdapUserDetailsImpl();
133         }
134 
135         public Essence addAuthority(GrantedAuthority a) {
136             mutableAuthorities.add(a);
137 
138             return this;
139         }
140 
141         public LdapUserDetails createUserDetails() {
142             //TODO: Validation of properties
143             Assert.notNull(instance, "Essence can only be used to create a single instance");
144 
145             instance.authorities = getGrantedAuthorities();
146 
147             LdapUserDetails newInstance = instance;
148 
149             instance = null;
150 
151             return newInstance;
152         }
153 
154         public GrantedAuthority[] getGrantedAuthorities() {
155             return (GrantedAuthority[]) mutableAuthorities.toArray(new GrantedAuthority[0]);
156         }
157 
158         public Essence setAccountNonExpired(boolean accountNonExpired) {
159             instance.accountNonExpired = accountNonExpired;
160 
161             return this;
162         }
163 
164         public Essence setAccountNonLocked(boolean accountNonLocked) {
165             instance.accountNonLocked = accountNonLocked;
166 
167             return this;
168         }
169 
170         public Essence setAttributes(Attributes attributes) {
171             instance.attributes = attributes;
172 
173             return this;
174         }
175 
176         public Essence setAuthorities(GrantedAuthority[] authorities) {
177             mutableAuthorities = new ArrayList(Arrays.asList(authorities));
178 
179             return this;
180         }
181 
182         public void setControls(Control[] controls) {
183             instance.controls = controls;
184         }
185 
186         public Essence setCredentialsNonExpired(boolean credentialsNonExpired) {
187             instance.credentialsNonExpired = credentialsNonExpired;
188 
189             return this;
190         }
191 
192         public Essence setDn(String dn) {
193             instance.dn = dn;
194 
195             return this;
196         }
197 
198         public Essence setEnabled(boolean enabled) {
199             instance.enabled = enabled;
200 
201             return this;
202         }
203 
204         public Essence setPassword(String password) {
205             instance.password = password;
206 
207             return this;
208         }
209 
210         public Essence setUsername(String username) {
211             instance.username = username;
212 
213             return this;
214         }
215     }
216 }