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

1   package org.apache.archiva.redback.common.ldap.role;
2   /*
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
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
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
18   * under the License.
19   */
20  
21  import com.google.common.collect.ArrayListMultimap;
22  import com.google.common.collect.Multimap;
23  import org.apache.archiva.redback.common.ldap.MappingException;
24  import org.apache.archiva.redback.configuration.UserConfiguration;
25  import org.apache.archiva.redback.configuration.UserConfigurationKeys;
26  import org.apache.commons.lang.StringUtils;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.stereotype.Service;
30  
31  import javax.inject.Inject;
32  import javax.inject.Named;
33  import java.util.Collection;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * <b>This implementation is only in memory you must use a different one if you need to save datas.</b>
40   *
41   * @author Olivier Lamy
42   * @since 2.1
43   */
44  @Service("ldapRoleMapperConfiguration#default")
45  public class DefaultLdapRoleMapperConfiguration
46      implements LdapRoleMapperConfiguration
47  {
48  
49      private Logger log = LoggerFactory.getLogger( getClass() );
50  
51      private Map<String, List<String>> ldapMappings = new HashMap<String, List<String>>();
52  
53      @Inject
54      @Named(value = "userConfiguration#default")
55      private UserConfiguration userConf;
56  
57      public void addLdapMapping( String ldapGroup, List<String> roles )
58          throws MappingException
59      {
60          ldapMappings.put( ldapGroup, roles );
61          log.warn( "addLdapMapping implemented but only in memory save: group '{}' roles '{}'", ldapGroup, roles );
62      }
63  
64      public void removeLdapMapping( String group )
65      {
66          ldapMappings.remove( group );
67      }
68  
69      public void updateLdapMapping( String ldapGroup, List<String> roles )
70          throws MappingException
71      {
72          ldapMappings.put( ldapGroup, roles );
73          log.warn( "updateLdapMapping implemented but only in memory save: group '{}' roles '{}'", ldapGroup, roles );
74      }
75  
76      public void setLdapGroupMappings( Map<String, List<String>> mappings )
77          throws MappingException
78      {
79          log.warn( "setLdapGroupMappings implemented but only in memory save" );
80          this.ldapMappings = mappings;
81      }
82  
83      public Map<String, Collection<String>> getLdapGroupMappings()
84      {
85          Multimap<String, String> map = ArrayListMultimap.create();
86  
87          Collection<String> keys = userConf.getKeys();
88  
89          for ( String key : keys )
90          {
91              if ( key.startsWith( UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY ) )
92              {
93                  String val = userConf.getString( key );
94                  String[] roles = StringUtils.split( val, ',' );
95                  for ( String role : roles )
96                  {
97                      map.put( StringUtils.substringAfter( key, UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY ),
98                               role );
99                  }
100             }
101         }
102 
103         for ( Map.Entry<String, List<String>> entry : this.ldapMappings.entrySet() )
104         {
105             map.putAll( entry.getKey(), entry.getValue() );
106         }
107 
108         Map<String, Collection<String>> mappings = map.asMap();
109         return mappings;
110     }
111 }