1 package org.apache.archiva.redback.rbac.ldap;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.redback.common.ldap.MappingException;
22 import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
23 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
24 import org.apache.archiva.redback.common.ldap.connection.LdapException;
25 import org.apache.archiva.redback.configuration.UserConfiguration;
26 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
27 import org.apache.commons.lang.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.stereotype.Service;
32 import javax.annotation.PostConstruct;
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import javax.naming.NamingEnumeration;
36 import javax.naming.NamingException;
37 import javax.naming.directory.Attribute;
38 import javax.naming.directory.DirContext;
39 import javax.naming.directory.SearchControls;
40 import javax.naming.directory.SearchResult;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
47 * @author Olivier Lamy
50 @Service( "ldapRoleMapper#default" )
51 public class DefaultLdapRoleMapper
52 implements LdapRoleMapper
55 private Logger log = LoggerFactory.getLogger( getClass() );
58 private LdapConnectionFactory ldapConnectionFactory;
61 @Named( value = "userConfiguration#default" )
62 private UserConfiguration userConf;
64 //---------------------------
66 //---------------------------
68 private String ldapGroupClass = "groupOfUniqueNames";
70 private String groupsDn;
72 private String baseDn;
75 public void initialize()
77 this.ldapGroupClass = userConf.getString( UserConfigurationKeys.LDAP_GROUPS_CLASS, this.ldapGroupClass );
79 this.groupsDn = userConf.getString( UserConfigurationKeys.LDAP_GROUPS_BASEDN, this.groupsDn );
81 this.baseDn = userConf.getString( UserConfigurationKeys.LDAP_BASEDN, this.baseDn );
84 public String getLdapGroup( String role )
86 return userConf.getString( UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY + role );
89 public List<String> getAllGroups()
90 throws MappingException
92 LdapConnection ldapConnection = null;
94 NamingEnumeration<SearchResult> namingEnumeration = null;
97 ldapConnection = ldapConnectionFactory.getConnection();
99 DirContext context = ldapConnection.getDirContext();
101 SearchControls searchControls = new SearchControls();
103 searchControls.setDerefLinkFlag( true );
104 searchControls.setSearchScope( SearchControls.SUBTREE_SCOPE );
106 String filter = "objectClass=" + getLdapGroupClass();
108 namingEnumeration = context.search( getGroupsDn(), filter, searchControls );
110 List<String> allGroups = new ArrayList<String>();
112 while ( namingEnumeration.hasMore() )
114 SearchResult searchResult = namingEnumeration.next();
116 String groupName = searchResult.getName();
117 // cn=blabla we only want bla bla
118 groupName = StringUtils.substringAfter( groupName, "=" );
120 log.debug( "found groupName: '{}", groupName );
122 allGroups.add( groupName );
128 catch ( LdapException e )
130 throw new MappingException( e.getMessage(), e );
132 catch ( NamingException e )
134 throw new MappingException( e.getMessage(), e );
139 if ( ldapConnection != null )
141 ldapConnection.close();
143 if ( namingEnumeration != null )
147 namingEnumeration.close();
149 catch ( NamingException e )
151 log.warn( "failed to close search results", e );
157 public List<String> getGroupsMember( String group )
158 throws MappingException
160 LdapConnection ldapConnection = null;
162 NamingEnumeration<SearchResult> namingEnumeration = null;
165 ldapConnection = ldapConnectionFactory.getConnection();
167 DirContext context = ldapConnection.getDirContext();
169 SearchControls searchControls = new SearchControls();
171 searchControls.setDerefLinkFlag( true );
172 searchControls.setSearchScope( SearchControls.SUBTREE_SCOPE );
174 String filter = "objectClass=" + getLdapGroupClass();
176 namingEnumeration = context.search( "cn=" + group + "," + getGroupsDn(), filter, searchControls );
178 List<String> allMembers = new ArrayList<String>();
180 while ( namingEnumeration.hasMore() )
182 SearchResult searchResult = namingEnumeration.next();
184 Attribute uniqueMemberAttr = searchResult.getAttributes().get( "uniquemember" );
186 if ( uniqueMemberAttr != null )
188 NamingEnumeration<String> allMembersEnum = (NamingEnumeration<String>) uniqueMemberAttr.getAll();
189 while ( allMembersEnum.hasMore() )
191 String userName = allMembersEnum.next();
192 // uid=blabla we only want bla bla
193 userName = StringUtils.substringAfter( userName, "=" );
194 userName = StringUtils.substringBefore( userName, "," );
195 log.debug( "found userName for group {}: '{}", group, userName );
197 allMembers.add( userName );
199 close( allMembersEnum );
207 catch ( LdapException e )
209 throw new MappingException( e.getMessage(), e );
211 catch ( NamingException e )
213 throw new MappingException( e.getMessage(), e );
218 if ( ldapConnection != null )
220 ldapConnection.close();
222 close( namingEnumeration );
226 public List<String> getGroups( String username )
227 throws MappingException
230 List<String> userGroups = new ArrayList<String>();
232 LdapConnection ldapConnection = null;
234 NamingEnumeration<SearchResult> namingEnumeration = null;
237 ldapConnection = ldapConnectionFactory.getConnection();
239 DirContext context = ldapConnection.getDirContext();
241 SearchControls searchControls = new SearchControls();
243 searchControls.setDerefLinkFlag( true );
244 searchControls.setSearchScope( SearchControls.SUBTREE_SCOPE );
247 new StringBuilder().append( "(&" ).append( "(objectClass=" + getLdapGroupClass() + ")" ).append(
248 "(uniquemember=" ).append( "uid=" + username + "," + this.getBaseDn() ).append( ")" ).append(
251 log.debug( "filter: {}", filter );
253 namingEnumeration = context.search( getGroupsDn(), filter, searchControls );
255 while ( namingEnumeration.hasMore() )
257 SearchResult searchResult = namingEnumeration.next();
259 List<String> allMembers = new ArrayList<String>();
261 Attribute uniqueMemberAttr = searchResult.getAttributes().get( "uniquemember" );
263 if ( uniqueMemberAttr != null )
265 NamingEnumeration<String> allMembersEnum = (NamingEnumeration<String>) uniqueMemberAttr.getAll();
266 while ( allMembersEnum.hasMore() )
268 String userName = allMembersEnum.next();
269 // uid=blabla we only want bla bla
270 userName = StringUtils.substringAfter( userName, "=" );
271 userName = StringUtils.substringBefore( userName, "," );
272 allMembers.add( userName );
274 close( allMembersEnum );
277 if ( allMembers.contains( username ) )
279 String groupName = searchResult.getName();
280 // cn=blabla we only want bla bla
281 groupName = StringUtils.substringAfter( groupName, "=" );
282 userGroups.add( groupName );
291 catch ( LdapException e )
293 throw new MappingException( e.getMessage(), e );
295 catch ( NamingException e )
297 throw new MappingException( e.getMessage(), e );
302 if ( ldapConnection != null )
304 ldapConnection.close();
306 close( namingEnumeration );
311 private void close( NamingEnumeration namingEnumeration )
313 if ( namingEnumeration != null )
317 namingEnumeration.close();
319 catch ( NamingException e )
321 log.warn( "fail to close namingEnumeration: {}", e.getMessage() );
326 public String getGroupsDn()
328 return this.groupsDn;
331 public String getLdapGroupClass()
333 return this.ldapGroupClass;
336 public void addLdapMapping( String role, String ldapGroup )
338 log.warn( "addLdapMapping not implemented" );
341 public void removeLdapMapping( String role )
343 log.warn( "removeLdapMapping not implemented" );
346 public Map<String, String> getLdapGroupMappings()
348 log.warn( "getLdapGroupMappings not implemented" );
349 return Collections.emptyMap();
352 //---------------------------------
353 // setters for unit tests
354 //---------------------------------
357 public void setGroupsDn( String groupsDn )
359 this.groupsDn = groupsDn;
362 public void setLdapGroupClass( String ldapGroupClass )
364 this.ldapGroupClass = ldapGroupClass;
367 public void setUserConf( UserConfiguration userConf )
369 this.userConf = userConf;
372 public void setLdapConnectionFactory( LdapConnectionFactory ldapConnectionFactory )
374 this.ldapConnectionFactory = ldapConnectionFactory;
377 public String getBaseDn()
382 public void setBaseDn( String baseDn )
384 this.baseDn = baseDn;