diff options
2 files changed, 290 insertions, 0 deletions
diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/LdapGroupMapping.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/LdapGroupMapping.java new file mode 100644 index 000000000..0da1bf190 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/LdapGroupMapping.java @@ -0,0 +1,115 @@ +package org.apache.archiva.admin.model.beans; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Arrays; +import java.util.Collection; + +/** + * @author Olivier Lamy + * @since 1.4-M4 + */ +public class LdapGroupMapping +{ + private String group; + + private Collection<String> roleNames; + + public LdapGroupMapping() + { + // no op + } + + public LdapGroupMapping( String group ) + { + this.group = group; + } + + public LdapGroupMapping( String group, Collection<String> roleNames ) + { + this.group = group; + this.roleNames = roleNames; + } + + public LdapGroupMapping( String group, String[] roleNames ) + { + this.group = group; + if ( roleNames != null ) + { + this.roleNames = Arrays.asList( roleNames ); + } + } + + public String getGroup() + { + return group; + } + + public void setGroup( String group ) + { + this.group = group; + } + + public Collection<String> getRoleNames() + { + return roleNames; + } + + public void setRoleNames( Collection<String> roleNames ) + { + this.roleNames = roleNames; + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + LdapGroupMapping that = (LdapGroupMapping) o; + + if ( group != null ? !group.equals( that.group ) : that.group != null ) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + return group != null ? group.hashCode() : 0; + } + + @Override + public String toString() + { + return "LdapGroupMapping{" + + "group='" + group + '\'' + + ", roleNames=" + roleNames + + '}'; + } +} diff --git a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/security/ArchivaLdapRoleMapperConfiguration.java b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/security/ArchivaLdapRoleMapperConfiguration.java new file mode 100644 index 000000000..0cf358328 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/security/ArchivaLdapRoleMapperConfiguration.java @@ -0,0 +1,175 @@ +package org.apache.archiva.web.security; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.admin.model.RepositoryAdminException; +import org.apache.archiva.admin.model.beans.LdapGroupMapping; +import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration; +import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin; +import org.apache.archiva.redback.common.ldap.MappingException; +import org.apache.archiva.redback.common.ldap.role.LdapRoleMapperConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import javax.inject.Inject; +import javax.inject.Named; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Olivier Lamy + * @since 2.1 + */ +@Service( "ldapRoleMapperConfiguration#archiva" ) +public class ArchivaLdapRoleMapperConfiguration + implements LdapRoleMapperConfiguration +{ + + private Logger logger = LoggerFactory.getLogger( getClass() ); + + @Inject + @Named( value = "redbackRuntimeConfigurationAdmin#default" ) + private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin; + + public void addLdapMapping( String ldapGroup, List<String> roles ) + throws MappingException + { + logger.debug( "addLdapMapping ldapGroup: {}, roles: {}", ldapGroup, roles ); + // TODO check if already exist first + try + { + RedbackRuntimeConfiguration redbackRuntimeConfiguration = + redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); + boolean added = + redbackRuntimeConfiguration.getLdapGroupMappings().add( new LdapGroupMapping( ldapGroup, roles ) ); + logger.debug( "addLdapMapping ldapGroup: {}, roles: {}, added: {}", ldapGroup, roles, added ); + redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration ); + } + catch ( RepositoryAdminException e ) + { + throw new MappingException( e.getMessage(), e ); + } + + } + + public void updateLdapMapping( String ldapGroup, List<String> roles ) + throws MappingException + { + + try + { + RedbackRuntimeConfiguration redbackRuntimeConfiguration = + redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); + LdapGroupMapping ldapGroupMapping = new LdapGroupMapping( ldapGroup ); + int idx = redbackRuntimeConfiguration.getLdapGroupMappings().indexOf( ldapGroupMapping ); + if ( idx > -1 ) + { + logger.debug( "updateLdapMapping ldapGroup: {}, roles: {}", ldapGroup, roles ); + ldapGroupMapping = redbackRuntimeConfiguration.getLdapGroupMappings().get( idx ); + ldapGroupMapping.setRoleNames( roles ); + } + redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration ); + + } + catch ( RepositoryAdminException e ) + { + throw new MappingException( e.getMessage(), e ); + } + } + + public void removeLdapMapping( String group ) + throws MappingException + { + try + { + RedbackRuntimeConfiguration redbackRuntimeConfiguration = + redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); + boolean removed = + redbackRuntimeConfiguration.getLdapGroupMappings().remove( new LdapGroupMapping( group ) ); + redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration ); + logger.debug( "removeLdapMapping ldapGroup: {}, removed: {}", group, removed ); + } + catch ( RepositoryAdminException e ) + { + throw new MappingException( e.getMessage(), e ); + } + + } + + public Map<String, Collection<String>> getLdapGroupMappings() + throws MappingException + { + try + { + RedbackRuntimeConfiguration redbackRuntimeConfiguration = + redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); + + List<LdapGroupMapping> ldapGroupMappings = redbackRuntimeConfiguration.getLdapGroupMappings(); + + if ( ldapGroupMappings == null ) + { + return Collections.emptyMap(); + } + + Map<String, Collection<String>> res = new HashMap<String, Collection<String>>( ldapGroupMappings.size() ); + + for ( LdapGroupMapping ldapGroupMapping : ldapGroupMappings ) + { + res.put( ldapGroupMapping.getGroup(), ldapGroupMapping.getRoleNames() ); + } + + return res; + } + catch ( RepositoryAdminException e ) + { + throw new MappingException( e.getMessage(), e ); + } + } + + public void setLdapGroupMappings( Map<String, List<String>> mappings ) + throws MappingException + { + try + { + RedbackRuntimeConfiguration redbackRuntimeConfiguration = + redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration(); + + List<LdapGroupMapping> ldapGroupMappings = new ArrayList<LdapGroupMapping>( mappings.size() ); + + for ( Map.Entry<String, List<String>> entry : mappings.entrySet() ) + { + ldapGroupMappings.add( new LdapGroupMapping( entry.getKey(), entry.getValue() ) ); + } + + redbackRuntimeConfiguration.setLdapGroupMappings( ldapGroupMappings ); + + redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration ); + } + catch ( RepositoryAdminException e ) + { + throw new MappingException( e.getMessage(), e ); + } + + } +} |