1 package org.apache.maven.archiva.web.action.admin.repositories;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.io.FileUtils;
23 import org.apache.maven.archiva.configuration.Configuration;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.security.ArchivaRoleConstants;
26 import org.codehaus.plexus.redback.rbac.RBACManager;
27 import org.codehaus.plexus.redback.rbac.RbacManagerException;
28 import org.codehaus.plexus.redback.rbac.UserAssignment;
29 import org.codehaus.plexus.redback.role.RoleManager;
30 import org.codehaus.plexus.redback.role.RoleManagerException;
33 import java.io.IOException;
36 * Abstract ManagedRepositories Action.
38 * Place for all generic methods used in Managed Repository Administration.
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43 public abstract class AbstractManagedRepositoriesAction
44 extends AbstractRepositoriesAdminAction
47 * @plexus.requirement role-hint="default"
49 protected RoleManager roleManager;
52 * @plexus.requirement role-hint="cached"
54 protected RBACManager rbacManager;
56 public RoleManager getRoleManager()
61 public void setRoleManager( RoleManager roleManager )
63 this.roleManager = roleManager;
66 protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
70 File file = new File( repository.getLocation() );
71 repository.setLocation( file.getCanonicalPath() );
76 if ( !file.exists() || !file.isDirectory() )
78 throw new IOException( "unable to add repository - can not create the root directory: " + file );
81 configuration.addManagedRepository( repository );
85 protected void addRepositoryRoles( ManagedRepositoryConfiguration newRepository ) throws RoleManagerException
87 String repoId = newRepository.getId();
89 // TODO: double check these are configured on start up
90 // TODO: belongs in the business logic
92 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
94 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
97 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
99 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
104 UserAssignment ua = rbacManager.getUserAssignment( ArchivaRoleConstants.GUEST_ROLE );
105 ua.addRoleName( ArchivaRoleConstants.REPOSITORY_OBSERVER_ROLE_PREFIX + " - " + repoId );
106 rbacManager.saveUserAssignment( ua );
108 catch ( RbacManagerException e )
110 getLogger().warn( "Unable to add role [" + ArchivaRoleConstants.REPOSITORY_OBSERVER_ROLE_PREFIX + " - "
111 + repoId + "] to Guest user.", e );
115 protected void removeContents( ManagedRepositoryConfiguration existingRepository )
118 FileUtils.deleteDirectory( new File( existingRepository.getLocation() ) );
121 protected void removeRepository( String repoId, Configuration configuration )
123 ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( repoId );
124 if ( toremove != null )
126 configuration.removeManagedRepository( toremove );
130 protected void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
131 throws RoleManagerException
133 String repoId = existingRepository.getId();
135 if ( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
137 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
140 if ( roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
142 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
145 getLogger().debug( "removed user roles associated with repository " + repoId );