1 package org.apache.archiva.redback.users.ldap.ctl;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.Collection;
23 import java.util.LinkedHashSet;
24 import java.util.LinkedList;
25 import java.util.List;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.NamingEnumeration;
31 import javax.naming.NamingException;
32 import javax.naming.directory.DirContext;
33 import javax.naming.directory.SearchControls;
34 import javax.naming.directory.SearchResult;
36 import org.apache.archiva.redback.common.ldap.LdapUser;
37 import org.apache.archiva.redback.common.ldap.LdapUserMapper;
38 import org.apache.archiva.redback.common.ldap.UserMapper;
39 import org.apache.archiva.redback.users.User;
40 import org.apache.archiva.redback.users.UserManager;
41 import org.apache.archiva.redback.common.ldap.MappingException;
42 import org.apache.archiva.redback.users.ldap.LdapUserQuery;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.stereotype.Service;
48 * @author <a href="jesse@codehaus.org"> jesse
52 public class DefaultLdapController
53 implements LdapController
56 private Logger log = LoggerFactory.getLogger( getClass() );
59 @Named(value = "userMapper#ldap")
60 private UserMapper mapper;
63 * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#removeUser(java.lang.Object, javax.naming.directory.DirContext)
65 public void removeUser( Object principal, DirContext context )
66 throws LdapControllerException
72 * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#updateUser(org.apache.archiva.redback.users.User, javax.naming.directory.DirContext)
74 public void updateUser( User user, DirContext context )
75 throws LdapControllerException, MappingException
81 * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#userExists(java.lang.Object, javax.naming.directory.DirContext)
83 public boolean userExists( Object key, DirContext context )
84 throws LdapControllerException
86 NamingEnumeration<SearchResult> results = null;
89 results = searchUsers( key, context );
90 return results.hasMoreElements();
92 catch ( NamingException e )
94 throw new LdapControllerException( "Error searching for the existence of user: " + key, e );
98 if ( results != null )
103 catch ( NamingException e )
105 log.warn( "Error closing search results", e );
110 protected NamingEnumeration<SearchResult> searchUsers( Object key, DirContext context )
111 throws NamingException
113 LdapUserQuery query = new LdapUserQuery();
114 query.setUsername( "" + key );
115 return searchUsers( context, null, query );
118 protected NamingEnumeration<SearchResult> searchUsers( DirContext context )
119 throws NamingException
121 return searchUsers( context, null, null );
124 protected NamingEnumeration<SearchResult> searchUsers( DirContext context, String[] returnAttributes )
125 throws NamingException
127 return searchUsers( context, returnAttributes, null );
130 protected NamingEnumeration<SearchResult> searchUsers( DirContext context, String[] returnAttributes, LdapUserQuery query )
131 throws NamingException
135 query = new LdapUserQuery();
137 SearchControls ctls = new SearchControls();
139 ctls.setDerefLinkFlag( true );
140 ctls.setSearchScope( SearchControls.SUBTREE_SCOPE );
141 ctls.setReturningAttributes( mapper.getReturningAttributes() );
142 ctls.setCountLimit( ( (LdapUserMapper) mapper ).getMaxResultCount() );
144 String finalFilter = "(&(objectClass=" + mapper.getUserObjectClass() + ")" +
145 ( mapper.getUserFilter() != null ? mapper.getUserFilter() : "" ) + query.getLdapFilter(mapper) + ")";
147 log.info( "Searching for users with filter: \'{}\'" + " from base dn: {}",finalFilter, mapper.getUserBaseDn() );
149 return context.search( mapper.getUserBaseDn(), finalFilter, ctls );
153 * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#getUsers(javax.naming.directory.DirContext)
155 public Collection<User> getUsers( DirContext context )
156 throws LdapControllerException, MappingException
158 NamingEnumeration<SearchResult> results = null;
161 results = searchUsers( context, null, null );
162 Set<User> users = new LinkedHashSet<User>();
164 while ( results.hasMoreElements() )
166 SearchResult result = results.nextElement();
168 users.add( mapper.getUser( result.getAttributes() ) );
173 catch ( NamingException e )
175 String message = "Failed to retrieve ldap information for users.";
177 throw new LdapControllerException( message, e );
181 if ( results != null )
186 catch ( NamingException e )
188 log.warn( "failed to close search results", e );
194 * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#getUsersByQuery(org.apache.archiva.redback.users.ldap.LdapUserQuery, javax.naming.directory.DirContext)
196 public List<User> getUsersByQuery( LdapUserQuery query, DirContext context )
197 throws LdapControllerException, MappingException
199 NamingEnumeration<SearchResult> results = null;
202 results = searchUsers( context, null, query );
203 List<User> users = new LinkedList<User>();
205 while ( results.hasMoreElements() )
207 SearchResult result = results.nextElement();
209 users.add( mapper.getUser( result.getAttributes() ) );
214 catch ( NamingException e )
216 String message = "Failed to retrieve ldap information for users.";
218 throw new LdapControllerException( message, e );
222 if ( results != null )
227 catch ( NamingException e )
229 log.warn( "failed to close search results", e );
235 * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#createUser(org.apache.archiva.redback.users.User, javax.naming.directory.DirContext, boolean)
237 public void createUser( User user, DirContext context, boolean encodePasswordIfChanged )
238 throws LdapControllerException, MappingException
244 if ( user.getUsername().equals( UserManager.GUEST_USERNAME ) )
246 //We don't store guest
253 * @see org.codehaus.plexus.redback.users.ldap.ctl.LdapControllerI#getUser(java.lang.Object, javax.naming.directory.DirContext)
255 public LdapUser getUser( Object key, DirContext context )
256 throws LdapControllerException, MappingException
258 String username = key.toString();
260 log.info( "Searching for user: {}", username );
261 LdapUserQuery query = new LdapUserQuery();
262 query.setUsername( username );
264 NamingEnumeration<SearchResult> result = null;
267 result = searchUsers( context, null, query );
269 if ( result.hasMoreElements() )
271 SearchResult next = result.nextElement();
273 return mapper.getUser( next.getAttributes() );
280 catch ( NamingException e )
282 String message = "Failed to retrieve information for user: " + username;
284 throw new LdapControllerException( message, e );
288 if ( result != null )
293 catch ( NamingException e )
295 log.warn( "failed to close search results", e );