001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.userdetails.ldap;
017:
018: import org.acegisecurity.GrantedAuthority;
019:
020: import org.springframework.util.Assert;
021:
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.List;
025:
026: import javax.naming.directory.Attributes;
027: import javax.naming.directory.BasicAttributes;
028: import javax.naming.ldap.Control;
029:
030: /**
031: * A UserDetails implementation which is used internally by the Ldap services. It also contains the user's
032: * distinguished name and a set of attributes that have been retrieved from the Ldap server.<p>An instance may be
033: * created as the result of a search, or when user information is retrieved during authentication.</p>
034: * <p>An instance of this class will be used by the <tt>LdapAuthenticationProvider</tt> to construct the final
035: * user details object that it returns.</p>
036: *
037: * @author Luke Taylor
038: * @version $Id$
039: */
040: public class LdapUserDetailsImpl implements LdapUserDetails {
041: //~ Static fields/initializers =====================================================================================
042:
043: private static final long serialVersionUID = 1L;
044: private static final GrantedAuthority[] NO_AUTHORITIES = new GrantedAuthority[0];
045: private static final Control[] NO_CONTROLS = new Control[0];
046:
047: //~ Instance fields ================================================================================================
048:
049: private Attributes attributes = new BasicAttributes();
050: private String dn;
051: private String password;
052: private String username;
053: private GrantedAuthority[] authorities = NO_AUTHORITIES;
054: private Control[] controls = NO_CONTROLS;
055: private boolean accountNonExpired = true;
056: private boolean accountNonLocked = true;
057: private boolean credentialsNonExpired = true;
058: private boolean enabled = true;
059:
060: //~ Constructors ===================================================================================================
061:
062: protected LdapUserDetailsImpl() {
063: }
064:
065: //~ Methods ========================================================================================================
066:
067: public Attributes getAttributes() {
068: return attributes;
069: }
070:
071: public GrantedAuthority[] getAuthorities() {
072: return authorities;
073: }
074:
075: public Control[] getControls() {
076: return controls;
077: }
078:
079: public String getDn() {
080: return dn;
081: }
082:
083: public String getPassword() {
084: return password;
085: }
086:
087: public String getUsername() {
088: return username;
089: }
090:
091: public boolean isAccountNonExpired() {
092: return accountNonExpired;
093: }
094:
095: public boolean isAccountNonLocked() {
096: return accountNonLocked;
097: }
098:
099: 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:
119: public Essence(LdapUserDetails copyMe) {
120: setDn(copyMe.getDn());
121: setAttributes(copyMe.getAttributes());
122: setUsername(copyMe.getUsername());
123: setPassword(copyMe.getPassword());
124: setEnabled(copyMe.isEnabled());
125: setAccountNonExpired(copyMe.isAccountNonExpired());
126: setCredentialsNonExpired(copyMe.isCredentialsNonExpired());
127: setAccountNonLocked(copyMe.isAccountNonLocked());
128: setControls(copyMe.getControls());
129: setAuthorities(copyMe.getAuthorities());
130: }
131:
132: LdapUserDetailsImpl createTarget() {
133: return new LdapUserDetailsImpl();
134: }
135:
136: public Essence addAuthority(GrantedAuthority a) {
137: mutableAuthorities.add(a);
138:
139: return this ;
140: }
141:
142: public LdapUserDetails createUserDetails() {
143: //TODO: Validation of properties
144: Assert
145: .notNull(instance,
146: "Essence can only be used to create a single instance");
147:
148: instance.authorities = getGrantedAuthorities();
149:
150: LdapUserDetails newInstance = instance;
151:
152: instance = null;
153:
154: return newInstance;
155: }
156:
157: public GrantedAuthority[] getGrantedAuthorities() {
158: return (GrantedAuthority[]) mutableAuthorities
159: .toArray(new GrantedAuthority[0]);
160: }
161:
162: public Essence setAccountNonExpired(boolean accountNonExpired) {
163: instance.accountNonExpired = accountNonExpired;
164:
165: return this ;
166: }
167:
168: public Essence setAccountNonLocked(boolean accountNonLocked) {
169: instance.accountNonLocked = accountNonLocked;
170:
171: return this ;
172: }
173:
174: public Essence setAttributes(Attributes attributes) {
175: instance.attributes = attributes;
176:
177: return this ;
178: }
179:
180: public Essence setAuthorities(GrantedAuthority[] authorities) {
181: mutableAuthorities = new ArrayList(Arrays
182: .asList(authorities));
183:
184: return this ;
185: }
186:
187: public void setControls(Control[] controls) {
188: instance.controls = controls;
189: }
190:
191: public Essence setCredentialsNonExpired(
192: boolean credentialsNonExpired) {
193: instance.credentialsNonExpired = credentialsNonExpired;
194:
195: return this ;
196: }
197:
198: public Essence setDn(String dn) {
199: instance.dn = dn;
200:
201: return this ;
202: }
203:
204: public Essence setEnabled(boolean enabled) {
205: instance.enabled = enabled;
206:
207: return this ;
208: }
209:
210: public Essence setPassword(String password) {
211: instance.password = password;
212:
213: return this ;
214: }
215:
216: public Essence setUsername(String username) {
217: instance.username = username;
218:
219: return this;
220: }
221: }
222: }
|