1
2
3
4
5
6
7
8
9
10
11
12
13
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
33
34
35
36
37
38
39
40
41 public class LdapUserDetailsImpl implements LdapUserDetails {
42
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
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
62
63 protected LdapUserDetailsImpl() {}
64
65
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
108
109
110
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
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 }