This project has retired. For details please refer to its Attic page.
ArchivaLdapRoleMapperConfiguration xref
View Javadoc
1   package org.apache.archiva.web.security;
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 org.apache.archiva.admin.model.RepositoryAdminException;
22  import org.apache.archiva.admin.model.beans.LdapGroupMapping;
23  import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration;
24  import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
25  import org.apache.archiva.redback.common.ldap.MappingException;
26  import org.apache.archiva.redback.common.ldap.role.LdapRoleMapperConfiguration;
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.ArrayList;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  /**
41   * @author Olivier Lamy
42   * @since 2.1
43   */
44  @Service( "ldapRoleMapperConfiguration#archiva" )
45  public class ArchivaLdapRoleMapperConfiguration
46      implements LdapRoleMapperConfiguration
47  {
48  
49      private Logger logger = LoggerFactory.getLogger( getClass() );
50  
51      @Inject
52      @Named( value = "redbackRuntimeConfigurationAdmin#default" )
53      private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
54  
55      @Override
56      public void addLdapMapping( String ldapGroup, List<String> roles )
57          throws MappingException
58      {
59          logger.debug( "addLdapMapping ldapGroup: {}, roles: {}", ldapGroup, roles );
60          // TODO check if already exist first
61          try
62          {
63              RedbackRuntimeConfiguration redbackRuntimeConfiguration =
64                  redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
65              boolean added =
66                  redbackRuntimeConfiguration.getLdapGroupMappings().add( new LdapGroupMapping( ldapGroup, roles ) );
67              logger.debug( "addLdapMapping ldapGroup: {}, roles: {}, added: {}", ldapGroup, roles, added );
68              redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
69          }
70          catch ( RepositoryAdminException e )
71          {
72              throw new MappingException( e.getMessage(), e );
73          }
74  
75      }
76  
77      @Override
78      public void updateLdapMapping( String ldapGroup, List<String> roles )
79          throws MappingException
80      {
81  
82          try
83          {
84              RedbackRuntimeConfiguration redbackRuntimeConfiguration =
85                  redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
86              LdapGroupMapping ldapGroupMapping = new LdapGroupMapping( ldapGroup );
87              int idx = redbackRuntimeConfiguration.getLdapGroupMappings().indexOf( ldapGroupMapping );
88              if ( idx > -1 )
89              {
90                  logger.debug( "updateLdapMapping ldapGroup: {}, roles: {}", ldapGroup, roles );
91                  ldapGroupMapping = redbackRuntimeConfiguration.getLdapGroupMappings().get( idx );
92                  ldapGroupMapping.setRoleNames( roles );
93              }
94              redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
95  
96          }
97          catch ( RepositoryAdminException e )
98          {
99              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 }