001package org.apache.archiva.redback.common.ldap.role;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import org.apache.archiva.redback.common.ldap.MappingException;
022import org.apache.archiva.redback.configuration.UserConfiguration;
023import org.apache.archiva.redback.configuration.UserConfigurationKeys;
024import org.apache.commons.collections4.MultiMap;
025import org.apache.commons.collections4.MultiValuedMap;
026import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
027import org.apache.commons.lang3.StringUtils;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.springframework.stereotype.Service;
031
032import javax.inject.Inject;
033import javax.inject.Named;
034import java.util.Arrays;
035import java.util.Collection;
036import java.util.HashMap;
037import java.util.List;
038import java.util.Map;
039
040/**
041 * <b>This implementation is only in memory you must use a different one if you need to save datas.</b>
042 *
043 * @author Olivier Lamy
044 * @since 2.1
045 */
046@Service("ldapRoleMapperConfiguration#default")
047public class DefaultLdapRoleMapperConfiguration
048    implements LdapRoleMapperConfiguration
049{
050
051    private Logger log = LoggerFactory.getLogger( getClass() );
052
053    private Map<String, List<String>> ldapMappings = new HashMap<String, List<String>>();
054
055    @Inject
056    @Named(value = "userConfiguration#default")
057    private UserConfiguration userConf;
058
059    public void addLdapMapping( String ldapGroup, List<String> roles )
060        throws MappingException
061    {
062        ldapMappings.put( ldapGroup, roles );
063        log.warn( "addLdapMapping implemented but only in memory save: group '{}' roles '{}'", ldapGroup, roles );
064    }
065
066    public void removeLdapMapping( String group )
067    {
068        ldapMappings.remove( group );
069    }
070
071    public void updateLdapMapping( String ldapGroup, List<String> roles )
072        throws MappingException
073    {
074        ldapMappings.put( ldapGroup, roles );
075        log.warn( "updateLdapMapping implemented but only in memory save: group '{}' roles '{}'", ldapGroup, roles );
076    }
077
078    public void setLdapGroupMappings( Map<String, List<String>> mappings )
079        throws MappingException
080    {
081        log.warn( "setLdapGroupMappings implemented but only in memory save" );
082        this.ldapMappings = mappings;
083    }
084
085    public Map<String, Collection<String>> getLdapGroupMappings()
086    {
087        MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>( );
088
089        Collection<String> keys = userConf.getKeys();
090
091        for ( String key : keys )
092        {
093            if ( key.startsWith( UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY ) )
094            {
095                String val = userConf.getString( key );
096                String[] roles = StringUtils.split( val, ',' );
097                for ( String role : roles )
098                {
099                    map.put( StringUtils.substringAfter( key, UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY ),
100                             role );
101                }
102            }
103        }
104
105        for ( Map.Entry<String, List<String>> entry : this.ldapMappings.entrySet() )
106        {
107            map.putAll( entry.getKey(), entry.getValue() );
108        }
109
110        Map<String, Collection<String>> mappings = map.asMap();
111        return mappings;
112    }
113
114    @Override
115    public Collection<String> getLdapGroupMapping( String groupName ) throws MappingException
116    {
117        if (this.ldapMappings.containsKey( groupName )) {
118            return this.ldapMappings.get( groupName );
119        } else {
120            String value = userConf.getString( UserConfigurationKeys.LDAP_GROUPS_ROLE_START_KEY + groupName );
121            if ( value != null) {
122                return Arrays.asList( StringUtils.split( "," ) );
123            }
124        }
125        throw new MappingException( "Mapping for group " + groupName + " not found" );
126    }
127}