import org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionFactory;
import org.codehaus.plexus.redback.common.ldap.connection.LdapException;
import org.codehaus.plexus.redback.configuration.UserConfiguration;
-import org.codehaus.plexus.redback.users.ldap.service.LdapCacheService;
+import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
*/
import junit.framework.TestCase;
-import org.codehaus.plexus.cache.builder.CacheBuilder;
import org.codehaus.plexus.redback.authentication.AuthenticationResult;
import org.codehaus.plexus.redback.authentication.PasswordBasedAuthenticationDataSource;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnection;
import org.codehaus.plexus.redback.policy.PasswordEncoder;
import org.codehaus.plexus.redback.policy.encoders.SHA1PasswordEncoder;
-import org.codehaus.plexus.redback.users.ldap.service.LdapCacheService;
+import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
import org.codehaus.redback.components.apacheds.ApacheDs;
import org.junit.After;
import org.junit.Before;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.util.Calendar;
-import java.util.Date;
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = "classpath*:/META-INF/spring-context.xml" )
--- /dev/null
+package org.apache.archiva.redback.users.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import org.apache.archiva.redback.users.AbstractUserManager;
+import org.apache.archiva.redback.users.User;
+import org.apache.archiva.redback.users.UserNotFoundException;
+import org.codehaus.plexus.redback.common.ldap.LdapUser;
+import org.codehaus.plexus.redback.common.ldap.MappingException;
+import org.codehaus.plexus.redback.common.ldap.UserMapper;
+import org.codehaus.plexus.redback.common.ldap.connection.LdapConnection;
+import org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionFactory;
+import org.codehaus.plexus.redback.common.ldap.connection.LdapException;
+import org.apache.archiva.redback.users.UserQuery;
+import org.apache.archiva.redback.users.ldap.ctl.LdapController;
+import org.apache.archiva.redback.users.ldap.ctl.LdapControllerException;
+import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
+import org.springframework.stereotype.Service;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.naming.directory.DirContext;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author <a href="jesse@codehaus.org"> jesse
+ * @version "$Id$"
+ */
+@Service( "userManager#ldap" )
+public class LdapUserManager
+ extends AbstractUserManager
+{
+ @Inject
+ @Named( value = "ldapConnectionFactory#configurable" )
+ private LdapConnectionFactory connectionFactory;
+
+ @Inject
+ private LdapController controller;
+
+ @Inject
+ @Named( value = "userMapper#ldap" )
+ private UserMapper mapper;
+
+ @Inject
+ private LdapCacheService ldapCacheService;
+
+ private User guestUser;
+
+ public boolean isReadOnly()
+ {
+ return true;
+ }
+
+ public User addUser( User user )
+ {
+ return addUser( user, true );
+ }
+
+ public void addUserUnchecked( User user )
+ {
+ addUser( user, false );
+ }
+
+ private User addUser( User user, boolean checked )
+ {
+ if ( user == null )
+ {
+ return null;
+ }
+
+ if ( GUEST_USERNAME.equals( user.getUsername() ) )
+ {
+ guestUser = user;
+ return guestUser;
+ }
+
+ LdapConnection ldapConnection = getLdapConnection();
+ try
+ {
+ DirContext context = ldapConnection.getDirContext();
+ controller.createUser( user, context, checked );
+ }
+ catch ( LdapControllerException e )
+ {
+ log.error( "Error mapping user: " + user.getPrincipal() + " to LDAP attributes.", e );
+ }
+ catch ( MappingException e )
+ {
+ log.error( "Error mapping user: " + user.getPrincipal() + " to LDAP attributes.", e );
+ }
+ finally
+ {
+ closeLdapConnection( ldapConnection );
+ }
+ return user;
+ }
+
+ public User createUser( String username, String fullName, String emailAddress )
+ {
+ return mapper.newUserInstance( username, fullName, emailAddress );
+ }
+
+ public UserQuery createUserQuery()
+ {
+ return new LdapUserQuery();
+ }
+
+ public void deleteUser( Object principal )
+ throws UserNotFoundException
+ {
+ if ( principal != null )
+ {
+ clearFromCache( principal.toString() );
+ }
+
+ LdapConnection ldapConnection = getLdapConnection();
+ try
+ {
+ DirContext context = ldapConnection.getDirContext();
+ controller.removeUser( principal, context );
+ }
+ catch ( LdapControllerException e )
+ {
+ log.error( "Failed to delete user: {}", principal, e );
+ }
+ finally
+ {
+ closeLdapConnection( ldapConnection );
+ }
+ }
+
+ public void deleteUser( String username )
+ throws UserNotFoundException
+ {
+ if ( username != null )
+ {
+ clearFromCache( username );
+ }
+
+ LdapConnection ldapConnection = getLdapConnection();
+ try
+ {
+ DirContext context = ldapConnection.getDirContext();
+ controller.removeUser( username, context );
+ }
+ catch ( LdapControllerException e )
+ {
+ log.error( "Failed to delete user: " + username, e );
+ }
+ finally
+ {
+ closeLdapConnection( ldapConnection );
+ }
+ }
+
+ public void eraseDatabase()
+ {
+ // TODO Implement erase!
+ }
+
+ public User findUser( String username )
+ throws UserNotFoundException
+ {
+ if ( username == null )
+ {
+ throw new UserNotFoundException( "Unable to find user based on null username." );
+ }
+
+ if ( GUEST_USERNAME.equals( username ) )
+ {
+ return getGuestUser();
+ }
+
+ // REDBACK-289/MRM-1488
+ // look for the user in the cache first
+ LdapUser ldapUser = ldapCacheService.getUser( username );
+ if ( ldapUser != null )
+ {
+ log.debug( "User {} found in cache.", username );
+ return ldapUser;
+ }
+
+ LdapConnection ldapConnection = getLdapConnection();
+ try
+ {
+ DirContext context = ldapConnection.getDirContext();
+ User user = controller.getUser( username, context );
+ if ( user == null )
+ {
+ throw new UserNotFoundException( "user with name " + username + " not found " );
+ }
+
+ // REDBACK-289/MRM-1488
+ log.debug( "Adding user {} to cache..", username );
+
+ ldapCacheService.addUser( (LdapUser) user );
+
+ return user;
+ }
+ catch ( LdapControllerException e )
+ {
+ log.error( "Failed to find user: {}", username, e );
+ return null;
+ }
+ catch ( MappingException e )
+ {
+ log.error( "Failed to map user: {}", username, e );
+ return null;
+ }
+ finally
+ {
+ closeLdapConnection( ldapConnection );
+ }
+ }
+
+ public User getGuestUser()
+ throws UserNotFoundException
+ {
+ if ( guestUser == null )
+ {
+ throw new UserNotFoundException( "Guest user doesn't exist." );
+ }
+ return guestUser;
+ }
+
+ public User findUser( Object principal )
+ throws UserNotFoundException
+ {
+ if ( principal == null )
+ {
+ throw new UserNotFoundException( "Unable to find user based on null principal." );
+ }
+
+ if ( GUEST_USERNAME.equals( principal.toString() ) )
+ {
+ return getGuestUser();
+ }
+
+ // REDBACK-289/MRM-1488
+ // look for the user in the cache first
+ LdapUser ldapUser = ldapCacheService.getUser( principal.toString() );
+ if ( ldapUser != null )
+ {
+ log.debug( "User {} found in cache.", principal );
+ return ldapUser;
+ }
+
+ LdapConnection ldapConnection = getLdapConnection();
+ try
+ {
+ DirContext context = ldapConnection.getDirContext();
+
+ User user = controller.getUser( principal, context );
+
+ // REDBACK-289/MRM-1488
+ log.debug( "Adding user {} to cache..", principal );
+
+ ldapCacheService.addUser( (LdapUser) user );
+
+ return user;
+ }
+ catch ( LdapControllerException e )
+ {
+ log.error( "Failed to find user: {}", principal, e );
+ return null;
+ }
+ catch ( MappingException e )
+ {
+ log.error( "Failed to map user: {}", principal, e );
+ return null;
+ }
+ finally
+ {
+ closeLdapConnection( ldapConnection );
+ }
+ }
+
+ public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
+ {
+ LdapUserQuery query = new LdapUserQuery();
+ query.setEmail( emailKey );
+ query.setOrderBy( UserQuery.ORDER_BY_EMAIL );
+ query.setAscending( orderAscending );
+ return findUsersByQuery( query );
+ }
+
+ public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
+ {
+ LdapUserQuery query = new LdapUserQuery();
+ query.setFullName( fullNameKey );
+ query.setOrderBy( UserQuery.ORDER_BY_FULLNAME );
+ query.setAscending( orderAscending );
+ return findUsersByQuery( query );
+ }
+
+ public List<User> findUsersByQuery( UserQuery query )
+ {
+ if ( query == null )
+ {
+ return Collections.emptyList();
+ }
+
+ LdapConnection ldapConnection = getLdapConnection();
+ try
+ {
+ DirContext context = ldapConnection.getDirContext();
+ return controller.getUsersByQuery( (LdapUserQuery) query, context );
+ }
+ catch ( LdapControllerException e )
+ {
+ log.error( "Failed to find user", e );
+ return null;
+ }
+ catch ( MappingException e )
+ {
+ log.error( "Failed to map user", e );
+ return null;
+ }
+ finally
+ {
+ closeLdapConnection( ldapConnection );
+ }
+ }
+
+ /**
+ * @see org.apache.archiva.redback.users.UserManager#findUsersByUsernameKey(java.lang.String, boolean)
+ */
+ public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
+ {
+ LdapUserQuery query = new LdapUserQuery();
+ query.setUsername( usernameKey );
+ query.setOrderBy( UserQuery.ORDER_BY_USERNAME );
+ query.setAscending( orderAscending );
+ return findUsersByQuery( query );
+ }
+
+ public String getId()
+ {
+ return "LDAP User-Manager: " + getClass().getName();
+ }
+
+ /**
+ * @see org.apache.archiva.redback.users.UserManager#getUsers()
+ */
+ public List<User> getUsers()
+ {
+ LdapConnection ldapConnection = getLdapConnection();
+ try
+ {
+ DirContext context = ldapConnection.getDirContext();
+ List<User> users = new ArrayList<User>( controller.getUsers( context ) );
+ //We add the guest user because it isn't in LDAP
+ try
+ {
+ User u = getGuestUser();
+ if ( u != null )
+ {
+ users.add( u );
+ }
+ }
+ catch ( UserNotFoundException e )
+ {
+ //Nothing to do
+ }
+ return users;
+ }
+ catch ( Exception e )
+ {
+ log.error( e.getMessage(), e );
+ }
+ finally
+ {
+ closeLdapConnection( ldapConnection );
+ }
+ return Collections.emptyList();
+ }
+
+ public List<User> getUsers( boolean orderAscending )
+ {
+ return getUsers();
+ }
+
+ public User updateUser( User user )
+ throws UserNotFoundException
+ {
+ return updateUser( user, false );
+ }
+
+ public User updateUser( User user, boolean passwordChangeRequired )
+ throws UserNotFoundException
+ {
+ if ( user != null )
+ {
+ clearFromCache( user.getUsername() );
+ }
+
+ LdapConnection ldapConnection = getLdapConnection();
+ try
+ {
+ DirContext context = ldapConnection.getDirContext();
+ controller.updateUser( user, context );
+ }
+ catch ( LdapControllerException e )
+ {
+ log.error( "Failed to update user: " + user.getPrincipal(), e );
+ }
+ catch ( MappingException e )
+ {
+ log.error( "Failed to update user: " + user.getPrincipal(), e );
+ }
+ finally
+ {
+ closeLdapConnection( ldapConnection );
+ }
+ return user;
+ }
+
+ public boolean userExists( Object principal )
+ {
+ if ( principal == null )
+ {
+ return false;
+ }
+
+ // REDBACK-289/MRM-1488
+ // look for the user in the cache first
+ LdapUser ldapUser = ldapCacheService.getUser( principal.toString() );
+ if ( ldapUser != null )
+ {
+ log.debug( "User {} found in cache.", principal );
+ return true;
+ }
+
+ LdapConnection ldapConnection = getLdapConnection();
+ try
+ {
+ DirContext context = ldapConnection.getDirContext();
+ return controller.userExists( principal, context );
+ }
+ catch ( LdapControllerException e )
+ {
+ log.warn( "Failed to search for user: " + principal, e );
+ return false;
+ }
+ finally
+ {
+ closeLdapConnection( ldapConnection );
+ }
+ }
+
+ private LdapConnection getLdapConnection()
+ {
+ try
+ {
+ return connectionFactory.getConnection();
+ }
+ catch ( LdapException e )
+ {
+ log.warn( "failed to get a ldap connection " + e.getMessage(), e );
+ throw new RuntimeException( "failed to get a ldap connection " + e.getMessage(), e );
+ }
+ }
+
+ private void closeLdapConnection( LdapConnection ldapConnection )
+ {
+ if ( ldapConnection != null )
+ {
+ ldapConnection.close();
+ }
+ }
+
+ // REDBACK-289/MRM-1488
+ private void clearFromCache( String username )
+ {
+ log.debug( "Removing user {} from cache..", username );
+ ldapCacheService.removeUser( username );
+
+ log.debug( "Removing userDn for user {} from cache..", username );
+ ldapCacheService.removeLdapUserDn( username );
+ }
+
+}
--- /dev/null
+package org.apache.archiva.redback.users.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.codehaus.plexus.redback.common.ldap.UserMapper;
+import org.apache.archiva.redback.users.AbstractUserQuery;
+
+public class LdapUserQuery
+ extends AbstractUserQuery
+{
+
+ public void setFirstResult( int firstResult )
+ {
+ super.setFirstResult( firstResult );
+ throw new UnsupportedOperationException( "Result limiting is not yet supported for LDAP." );
+ }
+
+ public void setMaxResults( int maxResults )
+ {
+ super.setMaxResults( maxResults );
+ throw new UnsupportedOperationException( "Result limiting is not yet supported for LDAP." );
+ }
+
+ public void setOrderBy( String orderBy )
+ {
+ super.setOrderBy( orderBy );
+ throw new UnsupportedOperationException( "Free-form ordering is not yet supported for LDAP." );
+ }
+
+ public String getLdapFilter( UserMapper mapper )
+ {
+ String filter = "";
+ if (this.getEmail() != null )
+ {
+ filter += "(" + mapper.getEmailAddressAttribute() + "=" + this.getEmail() + ")";
+ }
+ if ( this.getFullName() != null )
+ {
+ filter += "(" + mapper.getUserFullNameAttribute() + "=" + this.getFullName() + ")";
+ }
+ filter += "(" + mapper.getUserIdAttribute() + "=" + ( this.getUsername() != null ? this.getUsername() : "*" ) + ")";
+
+ return filter;
+ }
+
+}
--- /dev/null
+package org.apache.archiva.redback.users.ldap.ctl;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+
+import org.apache.archiva.redback.users.User;
+import org.apache.archiva.redback.users.UserManager;
+import org.codehaus.plexus.redback.common.ldap.LdapUser;
+import org.codehaus.plexus.redback.common.ldap.LdapUserMapper;
+import org.codehaus.plexus.redback.common.ldap.MappingException;
+import org.codehaus.plexus.redback.common.ldap.UserMapper;
+import org.apache.archiva.redback.users.ldap.LdapUserQuery;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author <a href="jesse@codehaus.org"> jesse
+ * @version "$Id$"
+ */
+@Service
+public class DefaultLdapController
+ implements LdapController
+{
+
+ private Logger log = LoggerFactory.getLogger( getClass() );
+
+ @Inject
+ @Named(value = "userMapper#ldap")
+ private UserMapper mapper;
+
+ /**
+ * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#removeUser(java.lang.Object, javax.naming.directory.DirContext)
+ */
+ public void removeUser( Object principal, DirContext context )
+ throws LdapControllerException
+ {
+
+ }
+
+ /**
+ * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#updateUser(org.apache.archiva.redback.users.User, javax.naming.directory.DirContext)
+ */
+ public void updateUser( User user, DirContext context )
+ throws LdapControllerException, MappingException
+ {
+
+ }
+
+ /**
+ * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#userExists(java.lang.Object, javax.naming.directory.DirContext)
+ */
+ public boolean userExists( Object key, DirContext context )
+ throws LdapControllerException
+ {
+ NamingEnumeration<SearchResult> results = null;
+ try
+ {
+ results = searchUsers( key, context );
+ return results.hasMoreElements();
+ }
+ catch ( NamingException e )
+ {
+ throw new LdapControllerException( "Error searching for the existence of user: " + key, e );
+ }
+ finally
+ {
+ if ( results != null )
+ try
+ {
+ results.close();
+ }
+ catch ( NamingException e )
+ {
+ log.warn( "Error closing search results", e );
+ }
+ }
+ }
+
+ protected NamingEnumeration<SearchResult> searchUsers( Object key, DirContext context )
+ throws NamingException
+ {
+ LdapUserQuery query = new LdapUserQuery();
+ query.setUsername( "" + key );
+ return searchUsers( context, null, query );
+ }
+
+ protected NamingEnumeration<SearchResult> searchUsers( DirContext context )
+ throws NamingException
+ {
+ return searchUsers( context, null, null );
+ }
+
+ protected NamingEnumeration<SearchResult> searchUsers( DirContext context, String[] returnAttributes )
+ throws NamingException
+ {
+ return searchUsers( context, returnAttributes, null );
+ }
+
+ protected NamingEnumeration<SearchResult> searchUsers( DirContext context, String[] returnAttributes, LdapUserQuery query )
+ throws NamingException
+ {
+ if ( query == null )
+ {
+ query = new LdapUserQuery();
+ }
+ SearchControls ctls = new SearchControls();
+
+ ctls.setDerefLinkFlag( true );
+ ctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
+ ctls.setReturningAttributes( mapper.getReturningAttributes() );
+ ctls.setCountLimit( ( ( LdapUserMapper ) mapper ).getMaxResultCount() );
+
+ String finalFilter = "(&(objectClass=" + mapper.getUserObjectClass() + ")" +
+ ( mapper.getUserFilter() != null ? mapper.getUserFilter() : "" ) + query.getLdapFilter(mapper) + ")";
+
+ log.info( "Searching for users with filter: \'{}\'" + " from base dn: {}",finalFilter, mapper.getUserBaseDn() );
+
+ return context.search( mapper.getUserBaseDn(), finalFilter, ctls );
+ }
+
+ /**
+ * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#getUsers(javax.naming.directory.DirContext)
+ */
+ public Collection<User> getUsers( DirContext context )
+ throws LdapControllerException, MappingException
+ {
+ NamingEnumeration<SearchResult> results = null;
+ try
+ {
+ results = searchUsers( context, null, null );
+ Set<User> users = new LinkedHashSet<User>();
+
+ while ( results.hasMoreElements() )
+ {
+ SearchResult result = results.nextElement();
+
+ users.add( mapper.getUser( result.getAttributes() ) );
+ }
+
+ return users;
+ }
+ catch ( NamingException e )
+ {
+ String message = "Failed to retrieve ldap information for users.";
+
+ throw new LdapControllerException( message, e );
+ }
+ finally
+ {
+ if ( results != null )
+ try
+ {
+ results.close();
+ }
+ catch ( NamingException e )
+ {
+ log.warn( "failed to close search results", e );
+ }
+ }
+ }
+
+ /**
+ * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#getUsersByQuery(org.apache.archiva.redback.users.ldap.LdapUserQuery, javax.naming.directory.DirContext)
+ */
+ public List<User> getUsersByQuery( LdapUserQuery query, DirContext context )
+ throws LdapControllerException, MappingException
+ {
+ NamingEnumeration<SearchResult> results = null;
+ try
+ {
+ results = searchUsers( context, null, query );
+ List<User> users = new LinkedList<User>();
+
+ while ( results.hasMoreElements() )
+ {
+ SearchResult result = results.nextElement();
+
+ users.add( mapper.getUser( result.getAttributes() ) );
+ }
+
+ return users;
+ }
+ catch ( NamingException e )
+ {
+ String message = "Failed to retrieve ldap information for users.";
+
+ throw new LdapControllerException( message, e );
+ }
+ finally
+ {
+ if ( results != null )
+ try
+ {
+ results.close();
+ }
+ catch ( NamingException e )
+ {
+ log.warn( "failed to close search results", e );
+ }
+ }
+ }
+
+ /**
+ * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#createUser(org.apache.archiva.redback.users.User, javax.naming.directory.DirContext, boolean)
+ */
+ public void createUser( User user, DirContext context, boolean encodePasswordIfChanged )
+ throws LdapControllerException, MappingException
+ {
+ if ( user == null )
+ {
+ return;
+ }
+ if ( user.getUsername().equals( UserManager.GUEST_USERNAME ) )
+ {
+ //We don't store guest
+ return;
+ }
+
+ }
+
+ /**
+ * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#getUser(java.lang.Object, javax.naming.directory.DirContext)
+ */
+ public LdapUser getUser( Object key, DirContext context )
+ throws LdapControllerException, MappingException
+ {
+ String username = key.toString();
+
+ log.info( "Searching for user: {}", username );
+ LdapUserQuery query = new LdapUserQuery();
+ query.setUsername( username );
+
+ NamingEnumeration<SearchResult> result = null;
+ try
+ {
+ result = searchUsers( context, null, query );
+
+ if ( result.hasMoreElements() )
+ {
+ SearchResult next = result.nextElement();
+
+ return mapper.getUser( next.getAttributes() );
+ }
+ else
+ {
+ return null;
+ }
+ }
+ catch ( NamingException e )
+ {
+ String message = "Failed to retrieve information for user: " + username;
+
+ throw new LdapControllerException( message, e );
+ }
+ finally
+ {
+ if ( result != null )
+ try
+ {
+ result.close();
+ }
+ catch ( NamingException e )
+ {
+ log.warn( "failed to close search results", e );
+ }
+ }
+ }
+
+}
--- /dev/null
+package org.apache.archiva.redback.users.ldap.ctl;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.redback.users.User;
+import org.codehaus.plexus.redback.common.ldap.LdapUser;
+import org.codehaus.plexus.redback.common.ldap.MappingException;
+import org.apache.archiva.redback.users.ldap.LdapUserQuery;
+
+import javax.naming.directory.DirContext;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * @version $Id$
+ */
+public interface LdapController
+{
+
+ void removeUser( Object principal, DirContext context )
+ throws LdapControllerException;
+
+ void updateUser( User user, DirContext context )
+ throws LdapControllerException, MappingException;
+
+ boolean userExists( Object key, DirContext context )
+ throws LdapControllerException;
+
+ Collection<User> getUsers( DirContext context )
+ throws LdapControllerException, MappingException;
+
+ void createUser( User user, DirContext context, boolean encodePasswordIfChanged )
+ throws LdapControllerException, MappingException;
+
+ LdapUser getUser( Object key, DirContext context )
+ throws LdapControllerException, MappingException;
+
+ List<User> getUsersByQuery( LdapUserQuery query, DirContext context )
+ throws LdapControllerException, MappingException;
+}
--- /dev/null
+package org.apache.archiva.redback.users.ldap.ctl;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public class LdapControllerException
+ extends Exception
+{
+
+ public LdapControllerException( String message, Throwable cause )
+ {
+ super( message, cause );
+ }
+
+ public LdapControllerException( String message )
+ {
+ super( message );
+ }
+
+}
--- /dev/null
+package org.apache.archiva.redback.users.ldap.service;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
+import org.codehaus.plexus.cache.builder.CacheBuilder;
+import org.codehaus.plexus.redback.common.ldap.LdapUser;
+import org.springframework.stereotype.Service;
+
+import javax.inject.Inject;
+
+/**
+ * DefaultLdapCacheService
+ *
+ * @author: Maria Odea Ching <oching@apache.org>
+ * @version
+ */
+@Service
+public class DefaultLdapCacheService
+ implements LdapCacheService
+{
+ @Inject
+ private CacheBuilder cacheBuilder;
+
+ // LDAP Users
+
+ /**
+ * @see LdapCacheService#getUser(String)
+ */
+ public LdapUser getUser( String username )
+ {
+ return (LdapUser) cacheBuilder.getCache( "ldapUser" ).get( username );
+ }
+
+ /**
+ * @see LdapCacheService#removeUser(String)
+ */
+ public boolean removeUser( String username )
+ {
+ return ( cacheBuilder.getCache( "ldapUser" ).remove( username ) == null ? false : true );
+ }
+
+ /**
+ * @see LdapCacheService#removeAllUsers()
+ */
+ public void removeAllUsers()
+ {
+ cacheBuilder.getCache( "ldapUser" ).clear();
+ }
+
+ /**
+ * @see LdapCacheService#addUser(org.codehaus.plexus.redback.common.ldap.LdapUser)
+ */
+ public void addUser( LdapUser user )
+ {
+ LdapUser existingUser = (LdapUser) cacheBuilder.getCache( "ldapUser" ).get( user.getUsername() );
+ if( existingUser != null )
+ {
+ removeUser( user.getUsername() );
+ }
+
+ cacheBuilder.getCache( "ldapUser" ).put( user.getUsername(), user );
+ }
+
+ // LDAP UserDn
+
+ /**
+ * @see LdapCacheService#getLdapUserDn(String)
+ */
+ public String getLdapUserDn( String username )
+ {
+ return (String) cacheBuilder.getCache( "ldapUserDn" ).get( username );
+ }
+
+ /**
+ * @see LdapCacheService#removeLdapUserDn(String)
+ */
+ public boolean removeLdapUserDn( String username )
+ {
+ return ( cacheBuilder.getCache( "ldapUserDn" ).remove( username ) == null ? false : true );
+ }
+
+ /**
+ * @see org.apache.archiva.redback.users.ldap.service.LdapCacheService#removeAllLdapUserDn()
+ */
+ public void removeAllLdapUserDn()
+ {
+ cacheBuilder.getCache( "ldapUserDn" ).clear();
+ }
+
+ /**
+ * @see LdapCacheService#addLdapUserDn(String, String)
+ */
+ public void addLdapUserDn( String username, String userDn )
+ {
+ String existingUserDn = (String) cacheBuilder.getCache( "ldapUserDn" ).get( username );
+ if( existingUserDn != null )
+ {
+ removeUser( username );
+ }
+
+ cacheBuilder.getCache( "ldapUserDn" ).put( username, userDn );
+ }
+
+}
--- /dev/null
+package org.apache.archiva.redback.users.ldap.service;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.codehaus.plexus.redback.common.ldap.LdapUser;
+
+/**
+ * LdapCacheService
+ *
+ * Service that manages the LDAP caches: LDAP connections and LDAP users
+ *
+ * @author: Maria Odea Ching <oching@apache.org>
+ * @version
+ */
+public interface LdapCacheService
+{
+ /**
+ * Retrieve LDAP user with the given username from the cache.
+ * Returns null if user is not found.
+ *
+ * @param username
+ * @return
+ */
+ LdapUser getUser( String username );
+
+ /**
+ * Remove LDAP user with the given username from the cache.
+ * Returns the removed object if it was in the cache. Otherwise, returns null.
+ *
+ * @param username
+ * @return
+ */
+ boolean removeUser( String username );
+
+ /**
+ * Remove all LDAP users in the cache. In short, it flushes the cache.
+ *
+ */
+ void removeAllUsers();
+
+ /**
+ * Adds the user to the LDAP users cache.
+ *
+ * @param user
+ */
+ void addUser( LdapUser user );
+
+ /**
+ * Retrieve the cached LDAP userDn for the given user.
+ *
+ * @param username
+ * @return
+ */
+ String getLdapUserDn( String username );
+
+ /**
+ * Remove the cached LDAP userDn for the given user.
+ *
+ * @param username
+ * @return
+ */
+ boolean removeLdapUserDn( String username );
+
+ /**
+ * Remove all cached LDAP userDn
+ */
+ void removeAllLdapUserDn();
+
+ /**
+ * All the LDAP userDn for the given user to the cache
+ *
+ * @param username
+ * @param userDn
+ */
+ void addLdapUserDn( String username, String userDn );
+}
+++ /dev/null
-package org.codehaus.plexus.redback.users.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-
-import org.apache.archiva.redback.users.AbstractUserManager;
-import org.apache.archiva.redback.users.User;
-import org.apache.archiva.redback.users.UserNotFoundException;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-import org.codehaus.plexus.redback.common.ldap.MappingException;
-import org.codehaus.plexus.redback.common.ldap.UserMapper;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnection;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionFactory;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapException;
-import org.apache.archiva.redback.users.UserQuery;
-import org.codehaus.plexus.redback.users.ldap.ctl.LdapController;
-import org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerException;
-import org.codehaus.plexus.redback.users.ldap.service.LdapCacheService;
-import org.springframework.stereotype.Service;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.naming.directory.DirContext;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * @author <a href="jesse@codehaus.org"> jesse
- * @version "$Id$"
- */
-@Service( "userManager#ldap" )
-public class LdapUserManager
- extends AbstractUserManager
-{
- @Inject
- @Named( value = "ldapConnectionFactory#configurable" )
- private LdapConnectionFactory connectionFactory;
-
- @Inject
- private LdapController controller;
-
- @Inject
- @Named( value = "userMapper#ldap" )
- private UserMapper mapper;
-
- @Inject
- private LdapCacheService ldapCacheService;
-
- private User guestUser;
-
- public boolean isReadOnly()
- {
- return true;
- }
-
- public User addUser( User user )
- {
- return addUser( user, true );
- }
-
- public void addUserUnchecked( User user )
- {
- addUser( user, false );
- }
-
- private User addUser( User user, boolean checked )
- {
- if ( user == null )
- {
- return null;
- }
-
- if ( GUEST_USERNAME.equals( user.getUsername() ) )
- {
- guestUser = user;
- return guestUser;
- }
-
- LdapConnection ldapConnection = getLdapConnection();
- try
- {
- DirContext context = ldapConnection.getDirContext();
- controller.createUser( user, context, checked );
- }
- catch ( LdapControllerException e )
- {
- log.error( "Error mapping user: " + user.getPrincipal() + " to LDAP attributes.", e );
- }
- catch ( MappingException e )
- {
- log.error( "Error mapping user: " + user.getPrincipal() + " to LDAP attributes.", e );
- }
- finally
- {
- closeLdapConnection( ldapConnection );
- }
- return user;
- }
-
- public User createUser( String username, String fullName, String emailAddress )
- {
- return mapper.newUserInstance( username, fullName, emailAddress );
- }
-
- public UserQuery createUserQuery()
- {
- return new LdapUserQuery();
- }
-
- public void deleteUser( Object principal )
- throws UserNotFoundException
- {
- if ( principal != null )
- {
- clearFromCache( principal.toString() );
- }
-
- LdapConnection ldapConnection = getLdapConnection();
- try
- {
- DirContext context = ldapConnection.getDirContext();
- controller.removeUser( principal, context );
- }
- catch ( LdapControllerException e )
- {
- log.error( "Failed to delete user: {}", principal, e );
- }
- finally
- {
- closeLdapConnection( ldapConnection );
- }
- }
-
- public void deleteUser( String username )
- throws UserNotFoundException
- {
- if ( username != null )
- {
- clearFromCache( username );
- }
-
- LdapConnection ldapConnection = getLdapConnection();
- try
- {
- DirContext context = ldapConnection.getDirContext();
- controller.removeUser( username, context );
- }
- catch ( LdapControllerException e )
- {
- log.error( "Failed to delete user: " + username, e );
- }
- finally
- {
- closeLdapConnection( ldapConnection );
- }
- }
-
- public void eraseDatabase()
- {
- // TODO Implement erase!
- }
-
- public User findUser( String username )
- throws UserNotFoundException
- {
- if ( username == null )
- {
- throw new UserNotFoundException( "Unable to find user based on null username." );
- }
-
- if ( GUEST_USERNAME.equals( username ) )
- {
- return getGuestUser();
- }
-
- // REDBACK-289/MRM-1488
- // look for the user in the cache first
- LdapUser ldapUser = ldapCacheService.getUser( username );
- if ( ldapUser != null )
- {
- log.debug( "User {} found in cache.", username );
- return ldapUser;
- }
-
- LdapConnection ldapConnection = getLdapConnection();
- try
- {
- DirContext context = ldapConnection.getDirContext();
- User user = controller.getUser( username, context );
- if ( user == null )
- {
- throw new UserNotFoundException( "user with name " + username + " not found " );
- }
-
- // REDBACK-289/MRM-1488
- log.debug( "Adding user {} to cache..", username );
-
- ldapCacheService.addUser( (LdapUser) user );
-
- return user;
- }
- catch ( LdapControllerException e )
- {
- log.error( "Failed to find user: {}", username, e );
- return null;
- }
- catch ( MappingException e )
- {
- log.error( "Failed to map user: {}", username, e );
- return null;
- }
- finally
- {
- closeLdapConnection( ldapConnection );
- }
- }
-
- public User getGuestUser()
- throws UserNotFoundException
- {
- if ( guestUser == null )
- {
- throw new UserNotFoundException( "Guest user doesn't exist." );
- }
- return guestUser;
- }
-
- public User findUser( Object principal )
- throws UserNotFoundException
- {
- if ( principal == null )
- {
- throw new UserNotFoundException( "Unable to find user based on null principal." );
- }
-
- if ( GUEST_USERNAME.equals( principal.toString() ) )
- {
- return getGuestUser();
- }
-
- // REDBACK-289/MRM-1488
- // look for the user in the cache first
- LdapUser ldapUser = ldapCacheService.getUser( principal.toString() );
- if ( ldapUser != null )
- {
- log.debug( "User {} found in cache.", principal );
- return ldapUser;
- }
-
- LdapConnection ldapConnection = getLdapConnection();
- try
- {
- DirContext context = ldapConnection.getDirContext();
-
- User user = controller.getUser( principal, context );
-
- // REDBACK-289/MRM-1488
- log.debug( "Adding user {} to cache..", principal );
-
- ldapCacheService.addUser( (LdapUser) user );
-
- return user;
- }
- catch ( LdapControllerException e )
- {
- log.error( "Failed to find user: {}", principal, e );
- return null;
- }
- catch ( MappingException e )
- {
- log.error( "Failed to map user: {}", principal, e );
- return null;
- }
- finally
- {
- closeLdapConnection( ldapConnection );
- }
- }
-
- public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
- {
- LdapUserQuery query = new LdapUserQuery();
- query.setEmail( emailKey );
- query.setOrderBy( UserQuery.ORDER_BY_EMAIL );
- query.setAscending( orderAscending );
- return findUsersByQuery( query );
- }
-
- public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
- {
- LdapUserQuery query = new LdapUserQuery();
- query.setFullName( fullNameKey );
- query.setOrderBy( UserQuery.ORDER_BY_FULLNAME );
- query.setAscending( orderAscending );
- return findUsersByQuery( query );
- }
-
- public List<User> findUsersByQuery( UserQuery query )
- {
- if ( query == null )
- {
- return Collections.emptyList();
- }
-
- LdapConnection ldapConnection = getLdapConnection();
- try
- {
- DirContext context = ldapConnection.getDirContext();
- return controller.getUsersByQuery( (LdapUserQuery) query, context );
- }
- catch ( LdapControllerException e )
- {
- log.error( "Failed to find user", e );
- return null;
- }
- catch ( MappingException e )
- {
- log.error( "Failed to map user", e );
- return null;
- }
- finally
- {
- closeLdapConnection( ldapConnection );
- }
- }
-
- /**
- * @see org.apache.archiva.redback.users.UserManager#findUsersByUsernameKey(java.lang.String, boolean)
- */
- public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
- {
- LdapUserQuery query = new LdapUserQuery();
- query.setUsername( usernameKey );
- query.setOrderBy( UserQuery.ORDER_BY_USERNAME );
- query.setAscending( orderAscending );
- return findUsersByQuery( query );
- }
-
- public String getId()
- {
- return "LDAP User-Manager: " + getClass().getName();
- }
-
- /**
- * @see org.apache.archiva.redback.users.UserManager#getUsers()
- */
- public List<User> getUsers()
- {
- LdapConnection ldapConnection = getLdapConnection();
- try
- {
- DirContext context = ldapConnection.getDirContext();
- List<User> users = new ArrayList<User>( controller.getUsers( context ) );
- //We add the guest user because it isn't in LDAP
- try
- {
- User u = getGuestUser();
- if ( u != null )
- {
- users.add( u );
- }
- }
- catch ( UserNotFoundException e )
- {
- //Nothing to do
- }
- return users;
- }
- catch ( Exception e )
- {
- log.error( e.getMessage(), e );
- }
- finally
- {
- closeLdapConnection( ldapConnection );
- }
- return Collections.emptyList();
- }
-
- public List<User> getUsers( boolean orderAscending )
- {
- return getUsers();
- }
-
- public User updateUser( User user )
- throws UserNotFoundException
- {
- return updateUser( user, false );
- }
-
- public User updateUser( User user, boolean passwordChangeRequired )
- throws UserNotFoundException
- {
- if ( user != null )
- {
- clearFromCache( user.getUsername() );
- }
-
- LdapConnection ldapConnection = getLdapConnection();
- try
- {
- DirContext context = ldapConnection.getDirContext();
- controller.updateUser( user, context );
- }
- catch ( LdapControllerException e )
- {
- log.error( "Failed to update user: " + user.getPrincipal(), e );
- }
- catch ( MappingException e )
- {
- log.error( "Failed to update user: " + user.getPrincipal(), e );
- }
- finally
- {
- closeLdapConnection( ldapConnection );
- }
- return user;
- }
-
- public boolean userExists( Object principal )
- {
- if ( principal == null )
- {
- return false;
- }
-
- // REDBACK-289/MRM-1488
- // look for the user in the cache first
- LdapUser ldapUser = ldapCacheService.getUser( principal.toString() );
- if ( ldapUser != null )
- {
- log.debug( "User {} found in cache.", principal );
- return true;
- }
-
- LdapConnection ldapConnection = getLdapConnection();
- try
- {
- DirContext context = ldapConnection.getDirContext();
- return controller.userExists( principal, context );
- }
- catch ( LdapControllerException e )
- {
- log.warn( "Failed to search for user: " + principal, e );
- return false;
- }
- finally
- {
- closeLdapConnection( ldapConnection );
- }
- }
-
- private LdapConnection getLdapConnection()
- {
- try
- {
- return connectionFactory.getConnection();
- }
- catch ( LdapException e )
- {
- log.warn( "failed to get a ldap connection " + e.getMessage(), e );
- throw new RuntimeException( "failed to get a ldap connection " + e.getMessage(), e );
- }
- }
-
- private void closeLdapConnection( LdapConnection ldapConnection )
- {
- if ( ldapConnection != null )
- {
- ldapConnection.close();
- }
- }
-
- // REDBACK-289/MRM-1488
- private void clearFromCache( String username )
- {
- log.debug( "Removing user {} from cache..", username );
- ldapCacheService.removeUser( username );
-
- log.debug( "Removing userDn for user {} from cache..", username );
- ldapCacheService.removeLdapUserDn( username );
- }
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.users.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.codehaus.plexus.redback.common.ldap.UserMapper;
-import org.apache.archiva.redback.users.AbstractUserQuery;
-
-public class LdapUserQuery
- extends AbstractUserQuery
-{
-
- public void setFirstResult( int firstResult )
- {
- super.setFirstResult( firstResult );
- throw new UnsupportedOperationException( "Result limiting is not yet supported for LDAP." );
- }
-
- public void setMaxResults( int maxResults )
- {
- super.setMaxResults( maxResults );
- throw new UnsupportedOperationException( "Result limiting is not yet supported for LDAP." );
- }
-
- public void setOrderBy( String orderBy )
- {
- super.setOrderBy( orderBy );
- throw new UnsupportedOperationException( "Free-form ordering is not yet supported for LDAP." );
- }
-
- public String getLdapFilter( UserMapper mapper )
- {
- String filter = "";
- if (this.getEmail() != null )
- {
- filter += "(" + mapper.getEmailAddressAttribute() + "=" + this.getEmail() + ")";
- }
- if ( this.getFullName() != null )
- {
- filter += "(" + mapper.getUserFullNameAttribute() + "=" + this.getFullName() + ")";
- }
- filter += "(" + mapper.getUserIdAttribute() + "=" + ( this.getUsername() != null ? this.getUsername() : "*" ) + ")";
-
- return filter;
- }
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.users.ldap.ctl;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import java.util.Collection;
-import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.naming.NamingEnumeration;
-import javax.naming.NamingException;
-import javax.naming.directory.DirContext;
-import javax.naming.directory.SearchControls;
-import javax.naming.directory.SearchResult;
-
-import org.apache.archiva.redback.users.User;
-import org.apache.archiva.redback.users.UserManager;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-import org.codehaus.plexus.redback.common.ldap.LdapUserMapper;
-import org.codehaus.plexus.redback.common.ldap.MappingException;
-import org.codehaus.plexus.redback.common.ldap.UserMapper;
-import org.codehaus.plexus.redback.users.ldap.LdapUserQuery;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Service;
-
-/**
- * @author <a href="jesse@codehaus.org"> jesse
- * @version "$Id$"
- */
-@Service
-public class DefaultLdapController
- implements LdapController
-{
-
- private Logger log = LoggerFactory.getLogger( getClass() );
-
- @Inject
- @Named(value = "userMapper#ldap")
- private UserMapper mapper;
-
- /**
- * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#removeUser(java.lang.Object, javax.naming.directory.DirContext)
- */
- public void removeUser( Object principal, DirContext context )
- throws LdapControllerException
- {
-
- }
-
- /**
- * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#updateUser(org.apache.archiva.redback.users.User, javax.naming.directory.DirContext)
- */
- public void updateUser( User user, DirContext context )
- throws LdapControllerException, MappingException
- {
-
- }
-
- /**
- * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#userExists(java.lang.Object, javax.naming.directory.DirContext)
- */
- public boolean userExists( Object key, DirContext context )
- throws LdapControllerException
- {
- NamingEnumeration<SearchResult> results = null;
- try
- {
- results = searchUsers( key, context );
- return results.hasMoreElements();
- }
- catch ( NamingException e )
- {
- throw new LdapControllerException( "Error searching for the existence of user: " + key, e );
- }
- finally
- {
- if ( results != null )
- try
- {
- results.close();
- }
- catch ( NamingException e )
- {
- log.warn( "Error closing search results", e );
- }
- }
- }
-
- protected NamingEnumeration<SearchResult> searchUsers( Object key, DirContext context )
- throws NamingException
- {
- LdapUserQuery query = new LdapUserQuery();
- query.setUsername( "" + key );
- return searchUsers( context, null, query );
- }
-
- protected NamingEnumeration<SearchResult> searchUsers( DirContext context )
- throws NamingException
- {
- return searchUsers( context, null, null );
- }
-
- protected NamingEnumeration<SearchResult> searchUsers( DirContext context, String[] returnAttributes )
- throws NamingException
- {
- return searchUsers( context, returnAttributes, null );
- }
-
- protected NamingEnumeration<SearchResult> searchUsers( DirContext context, String[] returnAttributes, LdapUserQuery query )
- throws NamingException
- {
- if ( query == null )
- {
- query = new LdapUserQuery();
- }
- SearchControls ctls = new SearchControls();
-
- ctls.setDerefLinkFlag( true );
- ctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
- ctls.setReturningAttributes( mapper.getReturningAttributes() );
- ctls.setCountLimit( ( ( LdapUserMapper ) mapper ).getMaxResultCount() );
-
- String finalFilter = "(&(objectClass=" + mapper.getUserObjectClass() + ")" +
- ( mapper.getUserFilter() != null ? mapper.getUserFilter() : "" ) + query.getLdapFilter(mapper) + ")";
-
- log.info( "Searching for users with filter: \'{}\'" + " from base dn: {}",finalFilter, mapper.getUserBaseDn() );
-
- return context.search( mapper.getUserBaseDn(), finalFilter, ctls );
- }
-
- /**
- * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#getUsers(javax.naming.directory.DirContext)
- */
- public Collection<User> getUsers( DirContext context )
- throws LdapControllerException, MappingException
- {
- NamingEnumeration<SearchResult> results = null;
- try
- {
- results = searchUsers( context, null, null );
- Set<User> users = new LinkedHashSet<User>();
-
- while ( results.hasMoreElements() )
- {
- SearchResult result = results.nextElement();
-
- users.add( mapper.getUser( result.getAttributes() ) );
- }
-
- return users;
- }
- catch ( NamingException e )
- {
- String message = "Failed to retrieve ldap information for users.";
-
- throw new LdapControllerException( message, e );
- }
- finally
- {
- if ( results != null )
- try
- {
- results.close();
- }
- catch ( NamingException e )
- {
- log.warn( "failed to close search results", e );
- }
- }
- }
-
- /**
- * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#getUsersByQuery(org.codehaus.plexus.redback.users.ldap.LdapUserQuery, javax.naming.directory.DirContext)
- */
- public List<User> getUsersByQuery( LdapUserQuery query, DirContext context )
- throws LdapControllerException, MappingException
- {
- NamingEnumeration<SearchResult> results = null;
- try
- {
- results = searchUsers( context, null, query );
- List<User> users = new LinkedList<User>();
-
- while ( results.hasMoreElements() )
- {
- SearchResult result = results.nextElement();
-
- users.add( mapper.getUser( result.getAttributes() ) );
- }
-
- return users;
- }
- catch ( NamingException e )
- {
- String message = "Failed to retrieve ldap information for users.";
-
- throw new LdapControllerException( message, e );
- }
- finally
- {
- if ( results != null )
- try
- {
- results.close();
- }
- catch ( NamingException e )
- {
- log.warn( "failed to close search results", e );
- }
- }
- }
-
- /**
- * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#createUser(org.apache.archiva.redback.users.User, javax.naming.directory.DirContext, boolean)
- */
- public void createUser( User user, DirContext context, boolean encodePasswordIfChanged )
- throws LdapControllerException, MappingException
- {
- if ( user == null )
- {
- return;
- }
- if ( user.getUsername().equals( UserManager.GUEST_USERNAME ) )
- {
- //We don't store guest
- return;
- }
-
- }
-
- /**
- * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#getUser(java.lang.Object, javax.naming.directory.DirContext)
- */
- public LdapUser getUser( Object key, DirContext context )
- throws LdapControllerException, MappingException
- {
- String username = key.toString();
-
- log.info( "Searching for user: {}", username );
- LdapUserQuery query = new LdapUserQuery();
- query.setUsername( username );
-
- NamingEnumeration<SearchResult> result = null;
- try
- {
- result = searchUsers( context, null, query );
-
- if ( result.hasMoreElements() )
- {
- SearchResult next = result.nextElement();
-
- return mapper.getUser( next.getAttributes() );
- }
- else
- {
- return null;
- }
- }
- catch ( NamingException e )
- {
- String message = "Failed to retrieve information for user: " + username;
-
- throw new LdapControllerException( message, e );
- }
- finally
- {
- if ( result != null )
- try
- {
- result.close();
- }
- catch ( NamingException e )
- {
- log.warn( "failed to close search results", e );
- }
- }
- }
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.users.ldap.ctl;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.archiva.redback.users.User;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-import org.codehaus.plexus.redback.common.ldap.MappingException;
-import org.codehaus.plexus.redback.users.ldap.LdapUserQuery;
-
-import javax.naming.directory.DirContext;
-import java.util.Collection;
-import java.util.List;
-
-/**
- * @version $Id$
- */
-public interface LdapController
-{
-
- void removeUser( Object principal, DirContext context )
- throws LdapControllerException;
-
- void updateUser( User user, DirContext context )
- throws LdapControllerException, MappingException;
-
- boolean userExists( Object key, DirContext context )
- throws LdapControllerException;
-
- Collection<User> getUsers( DirContext context )
- throws LdapControllerException, MappingException;
-
- void createUser( User user, DirContext context, boolean encodePasswordIfChanged )
- throws LdapControllerException, MappingException;
-
- LdapUser getUser( Object key, DirContext context )
- throws LdapControllerException, MappingException;
-
- List<User> getUsersByQuery( LdapUserQuery query, DirContext context )
- throws LdapControllerException, MappingException;
-}
+++ /dev/null
-package org.codehaus.plexus.redback.users.ldap.ctl;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-public class LdapControllerException
- extends Exception
-{
-
- public LdapControllerException( String message, Throwable cause )
- {
- super( message, cause );
- }
-
- public LdapControllerException( String message )
- {
- super( message );
- }
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.users.ldap.service;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.codehaus.plexus.cache.builder.CacheBuilder;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-import org.springframework.stereotype.Service;
-
-import javax.inject.Inject;
-
-/**
- * DefaultLdapCacheService
- *
- * @author: Maria Odea Ching <oching@apache.org>
- * @version
- */
-@Service
-public class DefaultLdapCacheService
- implements LdapCacheService
-{
- @Inject
- private CacheBuilder cacheBuilder;
-
- // LDAP Users
-
- /**
- * @see LdapCacheService#getUser(String)
- */
- public LdapUser getUser( String username )
- {
- return (LdapUser) cacheBuilder.getCache( "ldapUser" ).get( username );
- }
-
- /**
- * @see LdapCacheService#removeUser(String)
- */
- public boolean removeUser( String username )
- {
- return ( cacheBuilder.getCache( "ldapUser" ).remove( username ) == null ? false : true );
- }
-
- /**
- * @see LdapCacheService#removeAllUsers()
- */
- public void removeAllUsers()
- {
- cacheBuilder.getCache( "ldapUser" ).clear();
- }
-
- /**
- * @see LdapCacheService#addUser(org.codehaus.plexus.redback.common.ldap.LdapUser)
- */
- public void addUser( LdapUser user )
- {
- LdapUser existingUser = (LdapUser) cacheBuilder.getCache( "ldapUser" ).get( user.getUsername() );
- if( existingUser != null )
- {
- removeUser( user.getUsername() );
- }
-
- cacheBuilder.getCache( "ldapUser" ).put( user.getUsername(), user );
- }
-
- // LDAP UserDn
-
- /**
- * @see LdapCacheService#getLdapUserDn(String)
- */
- public String getLdapUserDn( String username )
- {
- return (String) cacheBuilder.getCache( "ldapUserDn" ).get( username );
- }
-
- /**
- * @see LdapCacheService#removeLdapUserDn(String)
- */
- public boolean removeLdapUserDn( String username )
- {
- return ( cacheBuilder.getCache( "ldapUserDn" ).remove( username ) == null ? false : true );
- }
-
- /**
- * @see org.codehaus.plexus.redback.users.ldap.service.LdapCacheService#removeAllLdapUserDn()
- */
- public void removeAllLdapUserDn()
- {
- cacheBuilder.getCache( "ldapUserDn" ).clear();
- }
-
- /**
- * @see LdapCacheService#addLdapUserDn(String, String)
- */
- public void addLdapUserDn( String username, String userDn )
- {
- String existingUserDn = (String) cacheBuilder.getCache( "ldapUserDn" ).get( username );
- if( existingUserDn != null )
- {
- removeUser( username );
- }
-
- cacheBuilder.getCache( "ldapUserDn" ).put( username, userDn );
- }
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.users.ldap.service;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-
-/**
- * LdapCacheService
- *
- * Service that manages the LDAP caches: LDAP connections and LDAP users
- *
- * @author: Maria Odea Ching <oching@apache.org>
- * @version
- */
-public interface LdapCacheService
-{
- /**
- * Retrieve LDAP user with the given username from the cache.
- * Returns null if user is not found.
- *
- * @param username
- * @return
- */
- LdapUser getUser( String username );
-
- /**
- * Remove LDAP user with the given username from the cache.
- * Returns the removed object if it was in the cache. Otherwise, returns null.
- *
- * @param username
- * @return
- */
- boolean removeUser( String username );
-
- /**
- * Remove all LDAP users in the cache. In short, it flushes the cache.
- *
- */
- void removeAllUsers();
-
- /**
- * Adds the user to the LDAP users cache.
- *
- * @param user
- */
- void addUser( LdapUser user );
-
- /**
- * Retrieve the cached LDAP userDn for the given user.
- *
- * @param username
- * @return
- */
- String getLdapUserDn( String username );
-
- /**
- * Remove the cached LDAP userDn for the given user.
- *
- * @param username
- * @return
- */
- boolean removeLdapUserDn( String username );
-
- /**
- * Remove all cached LDAP userDn
- */
- void removeAllLdapUserDn();
-
- /**
- * All the LDAP userDn for the given user to the cache
- *
- * @param username
- * @param userDn
- */
- void addLdapUserDn( String username, String userDn );
-}
<context:annotation-config />
<context:component-scan
- base-package="org.codehaus.plexus.redback.users.ldap"/>
+ base-package="org.apache.archiva.redback.users.ldap"/>
<!-- REDBACK-289/MRM-1488 -->
<!-- Cache for LDAP users. Data is refreshed every 3 mins. -->
--- /dev/null
+package org.apache.archiva.redback.users.ldap;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+import org.apache.archiva.redback.users.User;
+import org.codehaus.plexus.redback.common.ldap.connection.LdapConnection;
+import org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionFactory;
+import org.codehaus.plexus.redback.policy.PasswordEncoder;
+import org.codehaus.plexus.redback.policy.encoders.SHA1PasswordEncoder;
+import org.apache.archiva.redback.users.UserManager;
+import org.apache.archiva.redback.users.UserNotFoundException;
+import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
+import org.codehaus.redback.components.apacheds.ApacheDs;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+import java.util.List;
+
+
+/**
+ * LdapUserManagerTest
+ *
+ * @author <a href="mailto:jesse@codehaus.org">Jesse McConnell</a>
+ * @version $Id$
+ */
+
+@RunWith( SpringJUnit4ClassRunner.class )
+@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
+public class LdapUserManagerTest
+ extends TestCase
+{
+
+ protected Logger log = LoggerFactory.getLogger( getClass() );
+
+ @Inject
+ @Named(value = "userManager#ldap")
+ private UserManager userManager;
+
+ @Inject
+ @Named( value = "apacheDS#test" )
+ private ApacheDs apacheDs;
+
+ private String suffix;
+
+ private PasswordEncoder passwordEncoder;
+
+ @Inject
+ @Named(value = "ldapConnectionFactory#configurable")
+ private LdapConnectionFactory connectionFactory;
+
+ @Inject
+ private LdapCacheService ldapCacheService;
+
+ public void testFoo()
+ throws Exception
+ {
+
+ }
+
+ @Before
+ public void setUp()
+ throws Exception
+ {
+ super.setUp();
+
+ passwordEncoder = new SHA1PasswordEncoder();
+
+ suffix = apacheDs.addSimplePartition( "test", new String[] { "redback", "plexus", "codehaus", "org" } )
+ .getSuffix();
+
+ log.info( "DN Suffix: " + suffix );
+
+ apacheDs.startServer();
+
+ clearManyUsers();
+
+ makeUsers();
+
+ }
+
+ @After
+ public void tearDown()
+ throws Exception
+ {
+ // clear cache
+ ldapCacheService.removeAllUsers();
+
+ InitialDirContext context = apacheDs.getAdminContext();
+
+ context.unbind( createDn( "jesse" ) );
+
+ context.unbind( createDn( "joakim" ) );
+
+ apacheDs.stopServer();
+
+ super.tearDown();
+ }
+
+ private void makeUsers()
+ throws Exception
+ {
+ InitialDirContext context = apacheDs.getAdminContext();
+
+ String cn = "jesse";
+ bindUserObject( context, cn, createDn( cn ) );
+ assertExist( context, createDn( cn ), "cn", cn );
+
+ cn = "joakim";
+ bindUserObject( context, cn, createDn( cn ) );
+ assertExist( context, createDn( cn ), "cn", cn );
+
+ }
+
+ @Test
+ public void testConnection()
+ throws Exception
+ {
+ assertNotNull( connectionFactory );
+
+ LdapConnection connection = null;
+ try
+ {
+ connection = connectionFactory.getConnection();
+
+ assertNotNull( connection );
+
+ DirContext context = connection.getDirContext();
+
+ assertNotNull( context );
+ } finally {
+ connection.close();
+ }
+ }
+
+ @Test
+ public void testDirectUsersExistence()
+ throws Exception
+ {
+ LdapConnection connection = null;
+ try
+ {
+ connection = connectionFactory.getConnection();
+
+ DirContext context = connection.getDirContext();
+
+ assertExist( context, createDn( "jesse" ), "cn", "jesse" );
+ assertExist( context, createDn( "joakim" ), "cn", "joakim" );
+ } finally {
+ connection.close();
+ }
+
+ }
+
+ @Test
+ public void testUserManager()
+ throws Exception
+ {
+ assertNotNull( userManager );
+
+ //assertNull( ldapCacheService.getUser( "jesse" ) );
+
+ assertTrue( userManager.userExists( "jesse" ) );
+
+ //assertNotNull( ldapCacheService.getUser( "jesse" ) );
+
+ List<User> users = userManager.getUsers();
+
+ assertNotNull( users );
+
+ assertEquals( 2, users.size() );
+
+ User jesse = userManager.findUser( "jesse" );
+
+ assertNotNull( jesse );
+
+ assertEquals( "jesse", jesse.getPrincipal().toString() );
+ assertEquals( "jesse@apache.org", jesse.getEmail() );
+ assertEquals( "foo", jesse.getFullName() );
+ System.out.println( "=====>"+jesse.getEncodedPassword());
+ System.out.println( "=====>"+passwordEncoder.encodePassword( "foo" ));
+ assertTrue( passwordEncoder.isPasswordValid( jesse.getEncodedPassword(), "foo" ) );
+
+ }
+
+ @Test
+ public void testUserNotFoundException()
+ throws Exception
+ {
+ try
+ {
+ userManager.findUser( "foo bar" );
+ fail( "not a UserNotFoundException with an unknown user" );
+ }
+ catch ( UserNotFoundException e )
+ {
+ // cool it works !
+ }
+ }
+
+ @Test
+ public void testWithManyUsers()
+ throws Exception
+ {
+ makeManyUsers();
+
+ assertNotNull( userManager );
+
+ assertTrue( userManager.userExists( "user10" ) );
+
+ List<User> users = userManager.getUsers();
+
+ assertNotNull( users );
+
+ assertEquals( 10002, users.size() );
+
+ User user10 = userManager.findUser( "user10" );
+
+ assertNotNull( user10 );
+ }
+
+ private void makeManyUsers()
+ throws Exception
+ {
+ InitialDirContext context = apacheDs.getAdminContext();
+
+ for ( int i = 0 ; i < 10000 ; i++ )
+ {
+ String cn = "user"+i;
+ bindUserObject( context, cn, createDn( cn ) );
+ }
+
+ }
+
+ private void clearManyUsers()
+ throws Exception
+ {
+ InitialDirContext context = apacheDs.getAdminContext();
+
+ for ( int i = 0 ; i < 10000 ; i++ )
+ {
+ String cn = "user"+i;
+ try
+ {
+ context.unbind( createDn( cn ) );
+ }
+ catch ( NamingException e )
+ {
+ // OK lets try with next one
+ }
+ }
+
+ }
+
+ private void bindUserObject( DirContext context, String cn, String dn )
+ throws Exception
+ {
+ Attributes attributes = new BasicAttributes( true );
+ BasicAttribute objectClass = new BasicAttribute( "objectClass" );
+ objectClass.add( "top" );
+ objectClass.add( "inetOrgPerson" );
+ objectClass.add( "person" );
+ objectClass.add( "organizationalperson" );
+ attributes.put( objectClass );
+ attributes.put( "cn", cn );
+ attributes.put( "sn", "foo" );
+ attributes.put( "mail", cn+"@apache.org" );
+ attributes.put( "userPassword", passwordEncoder.encodePassword( "foo" ) );
+ attributes.put( "givenName", "foo" );
+ context.createSubcontext( dn, attributes );
+ }
+
+ private String createDn( String cn )
+ {
+ return "cn=" + cn + "," + suffix;
+ }
+
+ private void assertExist( DirContext context, String dn, String attribute, String value )
+ throws NamingException
+ {
+ SearchControls ctls = new SearchControls();
+
+ ctls.setDerefLinkFlag( true );
+ ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
+ ctls.setReturningAttributes( new String[] { "*" } );
+
+ BasicAttributes matchingAttributes = new BasicAttributes();
+ matchingAttributes.put( attribute, value );
+ BasicAttribute objectClass = new BasicAttribute( "objectClass" );
+ objectClass.add( "inetOrgPerson" );
+ matchingAttributes.put( objectClass );
+
+ NamingEnumeration<SearchResult> results = context.search( suffix, matchingAttributes );
+ // NamingEnumeration<SearchResult> results = context.search( suffix, "(" + attribute + "=" + value + ")", ctls
+ // );
+
+ assertTrue( results.hasMoreElements() );
+ SearchResult result = results.nextElement();
+ Attributes attrs = result.getAttributes();
+ Attribute testAttr = attrs.get( attribute );
+ assertEquals( value, testAttr.get() );
+
+ }
+
+}
--- /dev/null
+package org.apache.archiva.redback.users.ldap.service;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
+import org.codehaus.plexus.redback.common.ldap.LdapUser;
+import org.junit.After;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import javax.inject.Inject;
+
+/**
+ * @author: Maria Odea Ching <oching@apache.org>
+ * @version
+ */
+@RunWith( SpringJUnit4ClassRunner.class )
+@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
+public class LdapCacheServiceTest
+ extends TestCase
+{
+ @Inject
+ private LdapCacheService ldapCacheService;
+
+ private static final String USERNAME = "dummy";
+
+ @After
+ public void tearDown()
+ throws Exception
+ {
+ ldapCacheService.removeAllUsers();
+ ldapCacheService.removeAllLdapUserDn();
+
+ super.tearDown();
+ }
+
+ @Test
+ public void testLdapUserDnCache()
+ throws Exception
+ {
+ ldapCacheService.addLdapUserDn( USERNAME, "userDn" );
+
+ assertNotNull( ldapCacheService.getLdapUserDn( USERNAME ) );
+
+ ldapCacheService.removeLdapUserDn( USERNAME );
+
+ assertNull( ldapCacheService.getLdapUserDn( USERNAME ) );
+ }
+
+ @Test
+ public void testClearLdapUserDnCache()
+ throws Exception
+ {
+ ldapCacheService.addLdapUserDn( USERNAME, "userDn" );
+
+ assertNotNull( ldapCacheService.getLdapUserDn( USERNAME ) );
+
+ ldapCacheService.removeLdapUserDn( USERNAME );
+
+ assertNull( ldapCacheService.getLdapUserDn( USERNAME ) );
+ }
+
+ @Test
+ public void testLdapUsersCache()
+ throws Exception
+ {
+ LdapUser ldapUser = new LdapUser( USERNAME );
+
+ ldapCacheService.addUser( ldapUser );
+
+ assertNotNull( ldapCacheService.getUser( USERNAME ) );
+
+ ldapCacheService.removeUser( USERNAME );
+
+ assertNull( ldapCacheService.getUser( USERNAME ) );
+ }
+
+ @Test
+ public void testClearLdapUsersCache()
+ throws Exception
+ {
+ LdapUser ldapUser = new LdapUser( USERNAME );
+
+ ldapCacheService.addUser( ldapUser );
+
+ assertNotNull( ldapCacheService.getUser( USERNAME ) );
+
+ ldapCacheService.removeAllUsers();
+
+ assertNull( ldapCacheService.getUser( USERNAME ) );
+ }
+}
+++ /dev/null
-package org.codehaus.plexus.redback.users.ldap;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import junit.framework.TestCase;
-import org.apache.archiva.redback.users.User;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnection;
-import org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionFactory;
-import org.codehaus.plexus.redback.policy.PasswordEncoder;
-import org.codehaus.plexus.redback.policy.encoders.SHA1PasswordEncoder;
-import org.apache.archiva.redback.users.UserManager;
-import org.apache.archiva.redback.users.UserNotFoundException;
-import org.codehaus.plexus.redback.users.ldap.service.LdapCacheService;
-import org.codehaus.redback.components.apacheds.ApacheDs;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-import javax.naming.NamingEnumeration;
-import javax.naming.NamingException;
-import javax.naming.directory.Attribute;
-import javax.naming.directory.Attributes;
-import javax.naming.directory.BasicAttribute;
-import javax.naming.directory.BasicAttributes;
-import javax.naming.directory.DirContext;
-import javax.naming.directory.InitialDirContext;
-import javax.naming.directory.SearchControls;
-import javax.naming.directory.SearchResult;
-import java.util.List;
-
-
-/**
- * LdapUserManagerTest
- *
- * @author <a href="mailto:jesse@codehaus.org">Jesse McConnell</a>
- * @version $Id$
- */
-
-@RunWith( SpringJUnit4ClassRunner.class )
-@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
-public class LdapUserManagerTest
- extends TestCase
-{
-
- protected Logger log = LoggerFactory.getLogger( getClass() );
-
- @Inject
- @Named(value = "userManager#ldap")
- private UserManager userManager;
-
- @Inject
- @Named( value = "apacheDS#test" )
- private ApacheDs apacheDs;
-
- private String suffix;
-
- private PasswordEncoder passwordEncoder;
-
- @Inject
- @Named(value = "ldapConnectionFactory#configurable")
- private LdapConnectionFactory connectionFactory;
-
- @Inject
- private LdapCacheService ldapCacheService;
-
- public void testFoo()
- throws Exception
- {
-
- }
-
- @Before
- public void setUp()
- throws Exception
- {
- super.setUp();
-
- passwordEncoder = new SHA1PasswordEncoder();
-
- suffix = apacheDs.addSimplePartition( "test", new String[] { "redback", "plexus", "codehaus", "org" } )
- .getSuffix();
-
- log.info( "DN Suffix: " + suffix );
-
- apacheDs.startServer();
-
- clearManyUsers();
-
- makeUsers();
-
- }
-
- @After
- public void tearDown()
- throws Exception
- {
- // clear cache
- ldapCacheService.removeAllUsers();
-
- InitialDirContext context = apacheDs.getAdminContext();
-
- context.unbind( createDn( "jesse" ) );
-
- context.unbind( createDn( "joakim" ) );
-
- apacheDs.stopServer();
-
- super.tearDown();
- }
-
- private void makeUsers()
- throws Exception
- {
- InitialDirContext context = apacheDs.getAdminContext();
-
- String cn = "jesse";
- bindUserObject( context, cn, createDn( cn ) );
- assertExist( context, createDn( cn ), "cn", cn );
-
- cn = "joakim";
- bindUserObject( context, cn, createDn( cn ) );
- assertExist( context, createDn( cn ), "cn", cn );
-
- }
-
- @Test
- public void testConnection()
- throws Exception
- {
- assertNotNull( connectionFactory );
-
- LdapConnection connection = null;
- try
- {
- connection = connectionFactory.getConnection();
-
- assertNotNull( connection );
-
- DirContext context = connection.getDirContext();
-
- assertNotNull( context );
- } finally {
- connection.close();
- }
- }
-
- @Test
- public void testDirectUsersExistence()
- throws Exception
- {
- LdapConnection connection = null;
- try
- {
- connection = connectionFactory.getConnection();
-
- DirContext context = connection.getDirContext();
-
- assertExist( context, createDn( "jesse" ), "cn", "jesse" );
- assertExist( context, createDn( "joakim" ), "cn", "joakim" );
- } finally {
- connection.close();
- }
-
- }
-
- @Test
- public void testUserManager()
- throws Exception
- {
- assertNotNull( userManager );
-
- //assertNull( ldapCacheService.getUser( "jesse" ) );
-
- assertTrue( userManager.userExists( "jesse" ) );
-
- //assertNotNull( ldapCacheService.getUser( "jesse" ) );
-
- List<User> users = userManager.getUsers();
-
- assertNotNull( users );
-
- assertEquals( 2, users.size() );
-
- User jesse = userManager.findUser( "jesse" );
-
- assertNotNull( jesse );
-
- assertEquals( "jesse", jesse.getPrincipal().toString() );
- assertEquals( "jesse@apache.org", jesse.getEmail() );
- assertEquals( "foo", jesse.getFullName() );
- System.out.println( "=====>"+jesse.getEncodedPassword());
- System.out.println( "=====>"+passwordEncoder.encodePassword( "foo" ));
- assertTrue( passwordEncoder.isPasswordValid( jesse.getEncodedPassword(), "foo" ) );
-
- }
-
- @Test
- public void testUserNotFoundException()
- throws Exception
- {
- try
- {
- userManager.findUser( "foo bar" );
- fail( "not a UserNotFoundException with an unknown user" );
- }
- catch ( UserNotFoundException e )
- {
- // cool it works !
- }
- }
-
- @Test
- public void testWithManyUsers()
- throws Exception
- {
- makeManyUsers();
-
- assertNotNull( userManager );
-
- assertTrue( userManager.userExists( "user10" ) );
-
- List<User> users = userManager.getUsers();
-
- assertNotNull( users );
-
- assertEquals( 10002, users.size() );
-
- User user10 = userManager.findUser( "user10" );
-
- assertNotNull( user10 );
- }
-
- private void makeManyUsers()
- throws Exception
- {
- InitialDirContext context = apacheDs.getAdminContext();
-
- for ( int i = 0 ; i < 10000 ; i++ )
- {
- String cn = "user"+i;
- bindUserObject( context, cn, createDn( cn ) );
- }
-
- }
-
- private void clearManyUsers()
- throws Exception
- {
- InitialDirContext context = apacheDs.getAdminContext();
-
- for ( int i = 0 ; i < 10000 ; i++ )
- {
- String cn = "user"+i;
- try
- {
- context.unbind( createDn( cn ) );
- }
- catch ( NamingException e )
- {
- // OK lets try with next one
- }
- }
-
- }
-
- private void bindUserObject( DirContext context, String cn, String dn )
- throws Exception
- {
- Attributes attributes = new BasicAttributes( true );
- BasicAttribute objectClass = new BasicAttribute( "objectClass" );
- objectClass.add( "top" );
- objectClass.add( "inetOrgPerson" );
- objectClass.add( "person" );
- objectClass.add( "organizationalperson" );
- attributes.put( objectClass );
- attributes.put( "cn", cn );
- attributes.put( "sn", "foo" );
- attributes.put( "mail", cn+"@apache.org" );
- attributes.put( "userPassword", passwordEncoder.encodePassword( "foo" ) );
- attributes.put( "givenName", "foo" );
- context.createSubcontext( dn, attributes );
- }
-
- private String createDn( String cn )
- {
- return "cn=" + cn + "," + suffix;
- }
-
- private void assertExist( DirContext context, String dn, String attribute, String value )
- throws NamingException
- {
- SearchControls ctls = new SearchControls();
-
- ctls.setDerefLinkFlag( true );
- ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
- ctls.setReturningAttributes( new String[] { "*" } );
-
- BasicAttributes matchingAttributes = new BasicAttributes();
- matchingAttributes.put( attribute, value );
- BasicAttribute objectClass = new BasicAttribute( "objectClass" );
- objectClass.add( "inetOrgPerson" );
- matchingAttributes.put( objectClass );
-
- NamingEnumeration<SearchResult> results = context.search( suffix, matchingAttributes );
- // NamingEnumeration<SearchResult> results = context.search( suffix, "(" + attribute + "=" + value + ")", ctls
- // );
-
- assertTrue( results.hasMoreElements() );
- SearchResult result = results.nextElement();
- Attributes attrs = result.getAttributes();
- Attribute testAttr = attrs.get( attribute );
- assertEquals( value, testAttr.get() );
-
- }
-
-}
+++ /dev/null
-package org.codehaus.plexus.redback.users.ldap.service;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import junit.framework.TestCase;
-import org.codehaus.plexus.redback.common.ldap.LdapUser;
-import org.junit.After;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
-import javax.inject.Inject;
-
-/**
- * @author: Maria Odea Ching <oching@apache.org>
- * @version
- */
-@RunWith( SpringJUnit4ClassRunner.class )
-@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
-public class LdapCacheServiceTest
- extends TestCase
-{
- @Inject
- private LdapCacheService ldapCacheService;
-
- private static final String USERNAME = "dummy";
-
- @After
- public void tearDown()
- throws Exception
- {
- ldapCacheService.removeAllUsers();
- ldapCacheService.removeAllLdapUserDn();
-
- super.tearDown();
- }
-
- @Test
- public void testLdapUserDnCache()
- throws Exception
- {
- ldapCacheService.addLdapUserDn( USERNAME, "userDn" );
-
- assertNotNull( ldapCacheService.getLdapUserDn( USERNAME ) );
-
- ldapCacheService.removeLdapUserDn( USERNAME );
-
- assertNull( ldapCacheService.getLdapUserDn( USERNAME ) );
- }
-
- @Test
- public void testClearLdapUserDnCache()
- throws Exception
- {
- ldapCacheService.addLdapUserDn( USERNAME, "userDn" );
-
- assertNotNull( ldapCacheService.getLdapUserDn( USERNAME ) );
-
- ldapCacheService.removeLdapUserDn( USERNAME );
-
- assertNull( ldapCacheService.getLdapUserDn( USERNAME ) );
- }
-
- @Test
- public void testLdapUsersCache()
- throws Exception
- {
- LdapUser ldapUser = new LdapUser( USERNAME );
-
- ldapCacheService.addUser( ldapUser );
-
- assertNotNull( ldapCacheService.getUser( USERNAME ) );
-
- ldapCacheService.removeUser( USERNAME );
-
- assertNull( ldapCacheService.getUser( USERNAME ) );
- }
-
- @Test
- public void testClearLdapUsersCache()
- throws Exception
- {
- LdapUser ldapUser = new LdapUser( USERNAME );
-
- ldapCacheService.addUser( ldapUser );
-
- assertNotNull( ldapCacheService.getUser( USERNAME ) );
-
- ldapCacheService.removeAllUsers();
-
- assertNull( ldapCacheService.getUser( USERNAME ) );
- }
-}