This project has retired. For details please refer to its Attic page.
LdapUserMapper xref
View Javadoc

1   package org.apache.archiva.redback.common.ldap.user;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.redback.common.ldap.LdapUtils;
23  import org.apache.archiva.redback.common.ldap.MappingException;
24  import org.apache.archiva.redback.configuration.UserConfiguration;
25  import org.apache.archiva.redback.configuration.UserConfigurationKeys;
26  import org.apache.archiva.redback.users.User;
27  import org.apache.commons.lang.StringUtils;
28  import org.springframework.stereotype.Service;
29  
30  import javax.annotation.PostConstruct;
31  import javax.inject.Inject;
32  import javax.inject.Named;
33  import javax.naming.directory.Attributes;
34  import javax.naming.directory.BasicAttributes;
35  import java.util.Date;
36  
37  /**
38   * @author <a href="jesse@codehaus.org"> jesse
39   */
40  @Service("userMapper#ldap")
41  public class LdapUserMapper
42      implements UserMapper
43  {
44      /**
45       *
46       */
47      String emailAttribute = "mail";
48  
49      /**
50       *
51       */
52      String fullNameAttribute = "givenName";
53  
54      /**
55       *
56       */
57      String passwordAttribute = "userPassword";
58  
59      /**
60       *
61       */
62      String userIdAttribute = "cn";
63  
64      /**
65       *
66       */
67      String userBaseDn;
68  
69      /**
70       *
71       */
72      String userObjectClass = "inetOrgPerson";
73  
74      /**
75       *
76       */
77      String userFilter;
78  
79      /**
80       *
81       */
82      int maxResultCount = 0;
83  
84      @Inject
85      @Named(value = "userConfiguration#default")
86      private UserConfiguration userConf;
87  
88      @PostConstruct
89      public void initialize()
90      {
91          emailAttribute = userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_EMAIL, emailAttribute );
92          fullNameAttribute =
93              userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_FULLNAME, fullNameAttribute );
94          passwordAttribute =
95              userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_PASSWORD, passwordAttribute );
96          userIdAttribute = userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_ID, userIdAttribute );
97          userBaseDn = userConf.getConcatenatedList( "ldap.config.mapper.attribute.user.base.dn",
98                                                     userConf.getConcatenatedList( "ldap.config.base.dn", userBaseDn ) );
99          userObjectClass =
100             userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_OBJECT_CLASS, userObjectClass );
101         userFilter = userConf.getString( UserConfigurationKeys.LDAP_MAPPER_USER_ATTRIBUTE_FILTER, userFilter );
102         maxResultCount = userConf.getInt( UserConfigurationKeys.LDAP_MAX_RESULT_COUNT, maxResultCount );
103     }
104 
105     public Attributes getCreationAttributes( User user, boolean encodePasswordIfChanged )
106         throws MappingException
107     {
108         Attributes userAttrs = new BasicAttributes();
109 
110         boolean passwordSet = false;
111 
112         if ( !passwordSet && ( user.getEncodedPassword() != null ) )
113         {
114             userAttrs.put( getPasswordAttribute(), user.getEncodedPassword() );
115         }
116 
117         if ( !StringUtils.isEmpty( user.getFullName() ) )
118         {
119             userAttrs.put( getUserFullNameAttribute(), user.getFullName() );
120         }
121 
122         if ( !StringUtils.isEmpty( user.getEmail() ) )
123         {
124             userAttrs.put( getEmailAddressAttribute(), user.getEmail() );
125         }
126 
127         return userAttrs;
128     }
129 
130     public String getEmailAddressAttribute()
131     {
132         return emailAttribute;
133     }
134 
135     public String getUserFullNameAttribute()
136     {
137         return fullNameAttribute;
138     }
139 
140     public String getPasswordAttribute()
141     {
142         return passwordAttribute;
143     }
144 
145     public String[] getUserAttributeNames()
146     {
147         return new String[]{ emailAttribute, fullNameAttribute, passwordAttribute, userIdAttribute };
148     }
149 
150     public int getMaxResultCount()
151     {
152         return maxResultCount;
153     }
154 
155     public UserUpdate getUpdate( LdapUser user )
156         throws MappingException
157     {
158 
159         Attributes addAttrs = new BasicAttributes();
160 
161         Attributes modAttrs = new BasicAttributes();
162 
163         if ( !StringUtils.isEmpty( user.getFullName() ) )
164         {
165             if ( user.getFullName() == null )
166             {
167                 addAttrs.put( getUserFullNameAttribute(), user.getFullName() );
168             }
169             else if ( !user.getFullName().equals( user.getFullName() ) )
170             {
171                 modAttrs.put( getUserFullNameAttribute(), user.getFullName() );
172             }
173         }
174 
175         if ( !StringUtils.isEmpty( user.getEmail() ) )
176         {
177             if ( user.getEmail() == null )
178             {
179                 addAttrs.put( getEmailAddressAttribute(), user.getEmail() );
180             }
181             else if ( !user.getEmail().equals( user.getEmail() ) )
182             {
183                 modAttrs.put( getEmailAddressAttribute(), user.getEmail() );
184             }
185         }
186 
187         return null;
188     }
189 
190     public LdapUser getUser( Attributes attributes )
191         throws MappingException
192     {
193         String userIdAttribute = getUserIdAttribute();
194         String emailAddressAttribute = getEmailAddressAttribute();
195         String nameAttribute = getUserFullNameAttribute();
196         String passwordAttribute = getPasswordAttribute();
197 
198         String userId = LdapUtils.getAttributeValue( attributes, userIdAttribute, "username" );
199 
200         LdapUser user = new LdapUser( userId );
201         user.setOriginalAttributes( attributes );
202 
203         user.setEmail( LdapUtils.getAttributeValue( attributes, emailAddressAttribute, "email address" ) );
204         user.setFullName( LdapUtils.getAttributeValue( attributes, nameAttribute, "name" ) );
205 
206         String encodedPassword = LdapUtils.getAttributeValueFromByteArray( attributes, passwordAttribute, "password" );
207 
208         // it seems to be a common convention for the password to come back prepended with the encoding type..
209         // however we deal with that via configuration right now so just smoke it.
210         if ( encodedPassword != null && encodedPassword.startsWith( "{" ) )
211         {
212             encodedPassword = encodedPassword.substring( encodedPassword.indexOf( '}' ) + 1 );
213         }
214 
215         user.setEncodedPassword( encodedPassword );
216 
217         // REDBACK-215: skip NPE
218         user.setLastPasswordChange( new Date() );
219 
220         return user;
221     }
222 
223     public String getUserIdAttribute()
224     {
225         return userIdAttribute;
226     }
227 
228     public String getEmailAttribute()
229     {
230         return emailAttribute;
231     }
232 
233     public void setEmailAttribute( String emailAttribute )
234     {
235         this.emailAttribute = emailAttribute;
236     }
237 
238     public String getFullNameAttribute()
239     {
240         return fullNameAttribute;
241     }
242 
243     public void setFullNameAttribute( String fullNameAttribute )
244     {
245         this.fullNameAttribute = fullNameAttribute;
246     }
247 
248     public void setMaxResultCount( int maxResultCount )
249     {
250         this.maxResultCount = maxResultCount;
251     }
252 
253     public String getUserBaseDn()
254     {
255         return userBaseDn;
256     }
257 
258     public void setUserBaseDn( String userBaseDn )
259     {
260         this.userBaseDn = userBaseDn;
261     }
262 
263     public String getUserObjectClass()
264     {
265         return userObjectClass;
266     }
267 
268     public String getUserFilter()
269     {
270         return userFilter;
271     }
272 
273     public void setUserFilter( String userFilter )
274     {
275         this.userFilter = userFilter;
276     }
277 
278     public void setUserObjectClass( String userObjectClass )
279     {
280         this.userObjectClass = userObjectClass;
281     }
282 
283     public void setPasswordAttribute( String passwordAttribute )
284     {
285         this.passwordAttribute = passwordAttribute;
286     }
287 
288     public void setUserIdAttribute( String userIdAttribute )
289     {
290         this.userIdAttribute = userIdAttribute;
291     }
292 
293     public LdapUser newUserInstance( String username, String fullName, String email )
294     {
295         return new LdapUser( username, fullName, email );
296     }
297 
298     public LdapUser newTemplateUserInstance()
299     {
300         return new LdapUser();
301     }
302 
303     public String[] getReturningAttributes()
304     {
305         return new String[]{ getUserIdAttribute(), getEmailAttribute(), getFullNameAttribute(),
306             getPasswordAttribute() };
307     }
308 
309     public UserConfiguration getUserConf()
310     {
311         return userConf;
312     }
313 
314     public void setUserConf( UserConfiguration userConf )
315     {
316         this.userConf = userConf;
317     }
318 }