001package org.apache.archiva.web.security; 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.admin.model.RepositoryAdminException; 022import org.apache.archiva.admin.model.beans.LdapGroupMapping; 023import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration; 024import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin; 025import org.apache.archiva.redback.common.ldap.MappingException; 026import org.apache.archiva.redback.common.ldap.role.LdapRoleMapperConfiguration; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029import org.springframework.stereotype.Service; 030 031import javax.inject.Inject; 032import javax.inject.Named; 033import java.util.ArrayList; 034import java.util.Collection; 035import java.util.Collections; 036import java.util.HashMap; 037import java.util.List; 038import java.util.Map; 039 040/** 041 * @author Olivier Lamy 042 * @since 2.1 043 */ 044@Service( "ldapRoleMapperConfiguration#archiva" ) 045public class ArchivaLdapRoleMapperConfiguration 046 implements LdapRoleMapperConfiguration 047{ 048 049 private Logger logger = LoggerFactory.getLogger( getClass() ); 050 051 @Inject 052 @Named( value = "redbackRuntimeConfigurationAdmin#default" ) 053 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin; 054 055 @Override 056 public void addLdapMapping( String ldapGroup, List<String> roles ) 057 throws MappingException 058 { 059 logger.debug( "addLdapMapping ldapGroup: {}, roles: {}", ldapGroup, roles ); 060 // TODO check if already exist first 061 try 062 { 063 RedbackRuntimeConfiguration redbackRuntimeConfiguration = 064 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); 065 boolean added = 066 redbackRuntimeConfiguration.getLdapGroupMappings().add( new LdapGroupMapping( ldapGroup, roles ) ); 067 logger.debug( "addLdapMapping ldapGroup: {}, roles: {}, added: {}", ldapGroup, roles, added ); 068 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration ); 069 } 070 catch ( RepositoryAdminException e ) 071 { 072 throw new MappingException( e.getMessage(), e ); 073 } 074 075 } 076 077 @Override 078 public void updateLdapMapping( String ldapGroup, List<String> roles ) 079 throws MappingException 080 { 081 082 try 083 { 084 RedbackRuntimeConfiguration redbackRuntimeConfiguration = 085 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); 086 LdapGroupMapping ldapGroupMapping = new LdapGroupMapping( ldapGroup ); 087 int idx = redbackRuntimeConfiguration.getLdapGroupMappings().indexOf( ldapGroupMapping ); 088 if ( idx > -1 ) 089 { 090 logger.debug( "updateLdapMapping ldapGroup: {}, roles: {}", ldapGroup, roles ); 091 ldapGroupMapping = redbackRuntimeConfiguration.getLdapGroupMappings().get( idx ); 092 ldapGroupMapping.setRoleNames( roles ); 093 } 094 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration ); 095 096 } 097 catch ( RepositoryAdminException e ) 098 { 099 throw new MappingException( e.getMessage(), e ); 100 } 101 } 102 103 @Override 104 public void removeLdapMapping( String group ) 105 throws MappingException 106 { 107 try 108 { 109 RedbackRuntimeConfiguration redbackRuntimeConfiguration = 110 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); 111 boolean removed = 112 redbackRuntimeConfiguration.getLdapGroupMappings().remove( new LdapGroupMapping( group ) ); 113 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration ); 114 logger.debug( "removeLdapMapping ldapGroup: {}, removed: {}", group, removed ); 115 } 116 catch ( RepositoryAdminException e ) 117 { 118 throw new MappingException( e.getMessage(), e ); 119 } 120 121 } 122 123 @Override 124 public Map<String, Collection<String>> getLdapGroupMappings() 125 throws MappingException 126 { 127 try 128 { 129 RedbackRuntimeConfiguration redbackRuntimeConfiguration = 130 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); 131 132 List<LdapGroupMapping> ldapGroupMappings = redbackRuntimeConfiguration.getLdapGroupMappings(); 133 134 if ( ldapGroupMappings == null ) 135 { 136 return Collections.emptyMap(); 137 } 138 139 Map<String, Collection<String>> res = new HashMap<>( ldapGroupMappings.size() ); 140 141 for ( LdapGroupMapping ldapGroupMapping : ldapGroupMappings ) 142 { 143 res.put( ldapGroupMapping.getGroup(), ldapGroupMapping.getRoleNames() ); 144 } 145 146 return res; 147 } 148 catch ( RepositoryAdminException e ) 149 { 150 throw new MappingException( e.getMessage(), e ); 151 } 152 } 153 154 @Override 155 public void setLdapGroupMappings( Map<String, List<String>> mappings ) 156 throws MappingException 157 { 158 try 159 { 160 RedbackRuntimeConfiguration redbackRuntimeConfiguration = 161 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); 162 163 List<LdapGroupMapping> ldapGroupMappings = new ArrayList<>( mappings.size() ); 164 165 for ( Map.Entry<String, List<String>> entry : mappings.entrySet() ) 166 { 167 ldapGroupMappings.add( new LdapGroupMapping( entry.getKey(), entry.getValue() ) ); 168 } 169 170 redbackRuntimeConfiguration.setLdapGroupMappings( ldapGroupMappings ); 171 172 redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration ); 173 } 174 catch ( RepositoryAdminException e ) 175 { 176 throw new MappingException( e.getMessage(), e ); 177 } 178 179 } 180}