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

1   package org.apache.archiva.redback.users.ldap;
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  
23  import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection;
24  import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
25  import org.apache.archiva.redback.common.ldap.user.LdapUser;
26  import org.apache.archiva.redback.common.ldap.user.UserMapper;
27  import org.apache.archiva.redback.configuration.UserConfiguration;
28  import org.apache.archiva.redback.configuration.UserConfigurationKeys;
29  import org.apache.archiva.redback.users.AbstractUserManager;
30  import org.apache.archiva.redback.users.User;
31  import org.apache.archiva.redback.users.UserManager;
32  import org.apache.archiva.redback.users.UserManagerException;
33  import org.apache.archiva.redback.users.UserNotFoundException;
34  import org.apache.archiva.redback.common.ldap.MappingException;
35  import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
36  import org.apache.archiva.redback.common.ldap.connection.LdapException;
37  import org.apache.archiva.redback.users.UserQuery;
38  import org.apache.archiva.redback.users.ldap.ctl.LdapController;
39  import org.apache.archiva.redback.users.ldap.ctl.LdapControllerException;
40  import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
41  import org.springframework.stereotype.Service;
42  
43  import javax.annotation.PostConstruct;
44  import javax.inject.Inject;
45  import javax.inject.Named;
46  import javax.naming.directory.DirContext;
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.List;
50  
51  /**
52   * @author <a href="jesse@codehaus.org"> jesse
53   */
54  @Service( "userManager#ldap" )
55  public class LdapUserManager
56      extends AbstractUserManager
57      implements UserManager
58  {
59      @Inject
60      @Named( value = "ldapConnectionFactory#configurable" )
61      private LdapConnectionFactory connectionFactory;
62  
63      @Inject
64      private LdapController controller;
65  
66      @Inject
67      @Named( value = "userMapper#ldap" )
68      private UserMapper mapper;
69  
70      @Inject
71      @Named( value = "userConfiguration#default" )
72      private UserConfiguration userConf;
73  
74      @Inject
75      private LdapCacheService ldapCacheService;
76  
77      private User guestUser;
78  
79      private boolean writableLdap = false;
80  
81      @PostConstruct
82      public void initialize()
83      {
84          this.writableLdap = userConf.getBoolean( UserConfigurationKeys.LDAP_WRITABLE, this.writableLdap );
85          controller.initialize();
86      }
87  
88      public boolean isReadOnly()
89      {
90          return !this.writableLdap;
91      }
92  
93      public User addUser( User user )
94          throws UserManagerException
95      {
96          try
97          {
98              return addUser( user, true );
99          }
100         catch ( LdapException e )
101         {
102             throw new UserManagerException( e.getMessage(), e );
103         }
104     }
105 
106     public void addUserUnchecked( User user )
107         throws UserManagerException
108     {
109         try
110         {
111             addUser( user, false );
112         }
113         catch ( LdapException e )
114         {
115             throw new UserManagerException( e.getMessage(), e );
116         }
117     }
118 
119     private User addUser( User user, boolean checked )
120         throws LdapException
121     {
122         if ( user == null )
123         {
124             return null;
125         }
126 
127         if ( isReadOnly() && GUEST_USERNAME.equals( user.getUsername() ) )
128         {
129             guestUser = user;
130             return guestUser;
131         }
132 
133         LdapConnection ldapConnection = getLdapConnection();
134         try
135         {
136             DirContext context = ldapConnection.getDirContext();
137             controller.createUser( user, context, checked );
138         }
139         catch ( LdapControllerException e )
140         {
141             log.error( "Error mapping user: " + user.getUsername() + " to LDAP attributes.", e );
142         }
143         catch ( MappingException e )
144         {
145             log.error( "Error mapping user: " + user.getUsername() + " to LDAP attributes.", e );
146         }
147         finally
148         {
149             closeLdapConnection( ldapConnection );
150         }
151         return user;
152     }
153 
154     public User createUser( String username, String fullName, String emailAddress )
155     {
156         return mapper.newUserInstance( username, fullName, emailAddress );
157     }
158 
159     public UserQuery createUserQuery()
160     {
161         return new LdapUserQuery();
162     }
163 
164 
165     public void deleteUser( String username )
166         throws UserNotFoundException, UserManagerException
167     {
168         if ( username != null )
169         {
170             clearFromCache( username );
171         }
172         LdapConnection ldapConnection = null;
173         try
174         {
175             ldapConnection = getLdapConnection();
176             DirContext context = ldapConnection.getDirContext();
177             controller.removeUser( username, context );
178         }
179         catch ( LdapControllerException e )
180         {
181             log.error( "Failed to delete user: " + username, e );
182         }
183         catch ( LdapException e )
184         {
185             throw new UserManagerException( e.getMessage(), e );
186         }
187         finally
188         {
189             closeLdapConnection( ldapConnection );
190         }
191     }
192 
193     public void eraseDatabase()
194     {
195         // TODO Implement erase!
196     }
197 
198     public User findUser( String username )
199         throws UserNotFoundException, UserManagerException
200     {
201         if ( username == null )
202         {
203             throw new UserNotFoundException( "Unable to find user based on null username." );
204         }
205 
206         // REDBACK-289/MRM-1488
207         // look for the user in the cache first
208         LdapUser ldapUser = ldapCacheService.getUser( username );
209         if ( ldapUser != null )
210         {
211             log.debug( "User {} found in cache.", username );
212             return ldapUser;
213         }
214 
215         LdapConnection ldapConnection = null;
216 
217         try
218         {
219             ldapConnection = getLdapConnection();
220             DirContext context = ldapConnection.getDirContext();
221             User user = controller.getUser( username, context );
222             if ( user == null )
223             {
224                 throw new UserNotFoundException( "user with name " + username + " not found " );
225             }
226 
227             // REDBACK-289/MRM-1488
228             log.debug( "Adding user {} to cache..", username );
229 
230             ldapCacheService.addUser( (LdapUser) user );
231 
232             return user;
233         }
234         catch ( LdapControllerException e )
235         {
236             log.error( "Failed to find user: {}", username, e );
237             return null;
238         }
239         catch ( LdapException e )
240         {
241             throw new UserManagerException( e.getMessage(), e );
242         }
243         catch ( MappingException e )
244         {
245             log.error( "Failed to map user: {}", username, e );
246             return null;
247         }
248         finally
249         {
250             closeLdapConnection( ldapConnection );
251         }
252     }
253 
254     public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
255         throws UserManagerException
256     {
257         LdapUserQuery query = new LdapUserQuery();
258         query.setEmail( emailKey );
259         query.setOrderBy( UserQuery.ORDER_BY_EMAIL );
260         query.setAscending( orderAscending );
261         return findUsersByQuery( query );
262     }
263 
264     public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
265         throws UserManagerException
266     {
267         LdapUserQuery query = new LdapUserQuery();
268         query.setFullName( fullNameKey );
269         query.setOrderBy( UserQuery.ORDER_BY_FULLNAME );
270         query.setAscending( orderAscending );
271         return findUsersByQuery( query );
272     }
273 
274     public List<User> findUsersByQuery( UserQuery query )
275         throws UserManagerException
276     {
277         if ( query == null )
278         {
279             return Collections.emptyList();
280         }
281 
282         LdapConnection ldapConnection = null;
283 
284         try
285         {
286             ldapConnection = getLdapConnection();
287             DirContext context = ldapConnection.getDirContext();
288             return controller.getUsersByQuery( (LdapUserQuery) query, context );
289         }
290         catch ( LdapControllerException e )
291         {
292             log.error( "Failed to find user", e );
293             return null;
294         }
295         catch ( MappingException e )
296         {
297             log.error( "Failed to map user", e );
298             return null;
299         }
300         catch ( LdapException e )
301         {
302             throw new UserManagerException( e.getMessage(), e );
303         }
304         finally
305         {
306             closeLdapConnection( ldapConnection );
307         }
308     }
309 
310     /**
311      * @see org.apache.archiva.redback.users.UserManager#findUsersByUsernameKey(java.lang.String, boolean)
312      */
313     public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
314         throws UserManagerException
315     {
316         LdapUserQuery query = new LdapUserQuery();
317         query.setUsername( usernameKey );
318         query.setOrderBy( UserQuery.ORDER_BY_USERNAME );
319         query.setAscending( orderAscending );
320         return findUsersByQuery( query );
321     }
322 
323     public String getId()
324     {
325         return "ldap";
326     }
327 
328     /**
329      * @see org.apache.archiva.redback.users.UserManager#getUsers()
330      */
331     public List<User> getUsers()
332     {
333         LdapConnection ldapConnection = null;
334 
335         try
336         {
337             ldapConnection = getLdapConnection();
338             DirContext context = ldapConnection.getDirContext();
339             List<User> users = new ArrayList<User>( controller.getUsers( context ) );
340             //We add the guest user because it isn't in LDAP
341             try
342             {
343                 User u = getGuestUser();
344                 if ( u != null )
345                 {
346                     users.add( u );
347                 }
348             }
349             catch ( UserNotFoundException e )
350             {
351                 //Nothing to do
352             }
353             return users;
354         }
355         /*catch ( LdapException e )
356         {
357             throw new UserManagerException( e.getMessage(), e );
358         }*/
359         catch ( Exception e )
360         {
361             log.error( e.getMessage(), e );
362         }
363         finally
364         {
365             closeLdapConnection( ldapConnection );
366         }
367         return Collections.emptyList();
368     }
369 
370     public List<User> getUsers( boolean orderAscending )
371     {
372         return getUsers();
373     }
374 
375     public User updateUser( User user )
376         throws UserNotFoundException, UserManagerException
377     {
378         return updateUser( user, false );
379     }
380 
381     public User updateUser( User user, boolean passwordChangeRequired )
382         throws UserNotFoundException, UserManagerException
383     {
384         if ( user != null )
385         {
386             clearFromCache( user.getUsername() );
387         }
388 
389         LdapConnection ldapConnection = null;
390 
391         try
392         {
393             ldapConnection = getLdapConnection();
394             DirContext context = ldapConnection.getDirContext();
395             controller.updateUser( user, context );
396         }
397         catch ( LdapControllerException e )
398         {
399             log.error( "Failed to update user: " + user.getUsername(), e );
400         }
401         catch ( MappingException e )
402         {
403             log.error( "Failed to update user: " + user.getUsername(), e );
404         }
405         catch ( LdapException e )
406         {
407             throw new UserManagerException( e.getMessage(), e );
408         }
409         finally
410         {
411             closeLdapConnection( ldapConnection );
412         }
413         return user;
414     }
415 
416     public boolean userExists( String principal )
417         throws UserManagerException
418     {
419         if ( principal == null )
420         {
421             return false;
422         }
423 
424         // REDBACK-289/MRM-1488
425         // look for the user in the cache first
426         LdapUser ldapUser = ldapCacheService.getUser( principal );
427         if ( ldapUser != null )
428         {
429             log.debug( "User {} found in cache.", principal );
430             return true;
431         }
432 
433         LdapConnection ldapConnection = null;
434 
435         try
436         {
437             ldapConnection = getLdapConnection();
438             DirContext context = ldapConnection.getDirContext();
439             return controller.userExists( principal, context );
440         }
441         catch ( LdapControllerException e )
442         {
443             log.warn( "Failed to search for user: " + principal, e );
444             return false;
445         }
446         catch ( LdapException e )
447         {
448             throw new UserManagerException( e.getMessage(), e );
449         }
450         finally
451         {
452             closeLdapConnection( ldapConnection );
453         }
454     }
455 
456     private LdapConnection getLdapConnection()
457         throws LdapException
458     {
459         try
460         {
461             return connectionFactory.getConnection();
462         }
463         catch ( LdapException e )
464         {
465             log.warn( "failed to get a ldap connection " + e.getMessage(), e );
466             throw new LdapException( "failed to get a ldap connection " + e.getMessage(), e );
467         }
468     }
469 
470     private void closeLdapConnection( LdapConnection ldapConnection )
471     {
472         if ( ldapConnection != null )
473         {
474             ldapConnection.close();
475         }
476     }
477 
478     // REDBACK-289/MRM-1488
479     private void clearFromCache( String username )
480     {
481         log.debug( "Removing user {} from cache..", username );
482         ldapCacheService.removeUser( username );
483 
484         log.debug( "Removing userDn for user {} from cache..", username );
485         ldapCacheService.removeLdapUserDn( username );
486     }
487 
488     public boolean isFinalImplementation()
489     {
490         return true;
491     }
492 
493     public String getDescriptionKey()
494     {
495         return "archiva.redback.usermanager.ldap";
496     }
497 }