1package org.apache.archiva.redback.common.ldap.role;
2/*3 * Licensed to the Apache Software Foundation (ASF) under one4 * or more contributor license agreements. See the NOTICE file5 * distributed with this work for additional information6 * regarding copyright ownership. The ASF licenses this file7 * to you under the Apache License, Version 2.0 (the8 * "License"); you may not use this file except in compliance9 * with the License. You may obtain a copy of the License at10 *11 * http://www.apache.org/licenses/LICENSE-2.012 *13 * Unless required by applicable law or agreed to in writing,14 * software distributed under the License is distributed on an15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY16 * KIND, either express or implied. See the License for the17 * specific language governing permissions and limitations18 * under the License.19 */2021import com.google.common.collect.ArrayListMultimap;
22import com.google.common.collect.Multimap;
23import org.apache.archiva.redback.common.ldap.MappingException;
24import org.apache.archiva.redback.configuration.UserConfiguration;
25import org.apache.archiva.redback.configuration.UserConfigurationKeys;
26import org.apache.commons.lang.StringUtils;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29import org.springframework.stereotype.Service;
3031import javax.inject.Inject;
32import javax.inject.Named;
33import java.util.Collection;
34import java.util.HashMap;
35import java.util.List;
36import java.util.Map;
3738/**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 Lamy42 * @since 2.143 */44 @Service("ldapRoleMapperConfiguration#default")
45publicclassDefaultLdapRoleMapperConfiguration46implementsLdapRoleMapperConfiguration47 {
4849private Logger log = LoggerFactory.getLogger( getClass() );
5051private Map<String, List<String>> ldapMappings = new HashMap<String, List<String>>();
5253 @Inject
54 @Named(value = "userConfiguration#default")
55privateUserConfiguration userConf;
5657publicvoid addLdapMapping( String ldapGroup, List<String> roles )
58throwsMappingException59 {
60 ldapMappings.put( ldapGroup, roles );
61 log.warn( "addLdapMapping implemented but only in memory save: group '{}' roles '{}'", ldapGroup, roles );
62 }
6364publicvoid removeLdapMapping( String group )
65 {
66 ldapMappings.remove( group );
67 }
6869publicvoid updateLdapMapping( String ldapGroup, List<String> roles )
70throwsMappingException71 {
72 ldapMappings.put( ldapGroup, roles );
73 log.warn( "updateLdapMapping implemented but only in memory save: group '{}' roles '{}'", ldapGroup, roles );
74 }
7576publicvoid setLdapGroupMappings( Map<String, List<String>> mappings )
77throwsMappingException78 {
79 log.warn( "setLdapGroupMappings implemented but only in memory save" );
80this.ldapMappings = mappings;
81 }
8283public Map<String, Collection<String>> getLdapGroupMappings()
84 {
85 Multimap<String, String> map = ArrayListMultimap.create();
8687 Collection<String> keys = userConf.getKeys();
8889for ( String key : keys )
90 {
91if ( key.startsWith( UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY ) )
92 {
93 String val = userConf.getString( key );
94 String[] roles = StringUtils.split( val, ',' );
95for ( String role : roles )
96 {
97 map.put( StringUtils.substringAfter( key, UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY ),
98 role );
99 }
100 }
101 }
102103for ( Map.Entry<String, List<String>> entry : this.ldapMappings.entrySet() )
104 {
105 map.putAll( entry.getKey(), entry.getValue() );
106 }
107108 Map<String, Collection<String>> mappings = map.asMap();
109return mappings;
110 }
111 }