* under the License.
*/
-import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.Preparable;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.InvalidConfigurationException;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.security.ArchivaRoleConstants;
-import org.codehaus.plexus.redback.authorization.AuthorizationException;
-import org.codehaus.plexus.redback.authorization.AuthorizationResult;
import org.codehaus.plexus.redback.rbac.Resource;
import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.redback.role.RoleManagerException;
-import org.codehaus.plexus.redback.system.SecuritySession;
-import org.codehaus.plexus.redback.system.SecuritySystem;
import org.codehaus.plexus.redback.xwork.interceptor.SecureAction;
import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
*/
private RoleManager roleManager;
- /**
- * @plexus.requirement
- */
- private SecuritySystem securitySystem;
-
/**
* @plexus.requirement
*/
public String add()
{
- getLogger().info( ".add()" );
this.mode = "add";
this.repository.setReleases( true );
public String confirm()
{
- getLogger().info( ".confirm()" );
-
- if ( operationAllowed( ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, repoid ) )
- {
- addActionError( "You do not have the appropriate permissions to delete the " + repoid + " repository." );
- return ERROR;
- }
-
return INPUT;
}
public String delete()
{
- getLogger().info( ".delete()" );
-
- if ( operationAllowed( ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, repoid ) )
- {
- addActionError( "You do not have the appropriate permissions to delete the " + repoid + " repository." );
- return ERROR;
- }
-
String result = SUCCESS;
if ( StringUtils.equals( mode, "delete-entry" ) || StringUtils.equals( mode, "delete-contents" ) )
{
try
{
- removeRepository( repoid );
+ removeRepository( repoid, archivaConfiguration.getConfiguration() );
result = saveConfiguration( archivaConfiguration.getConfiguration() );
if ( result.equals( SUCCESS ) )
public String edit()
{
- getLogger().info( ".edit()" );
this.mode = "edit";
- if ( operationAllowed( ArchivaRoleConstants.OPERATION_EDIT_REPOSITORY, repoid ) )
- {
- addActionError( "You do not have the appropriate permissions to edit the " + repoid + " repository." );
- return ERROR;
- }
-
return INPUT;
}
{
String repoId = repository.getId();
- getLogger().info( ".save(" + mode + ":" + repoId + ")" );
-
Configuration configuration = archivaConfiguration.getConfiguration();
boolean containsError = validateFields( configuration );
if ( StringUtils.equalsIgnoreCase( "edit", this.mode ) )
{
- removeRepository( repoId );
+ removeRepository( repoId, configuration );
}
String result;
private void addRepository( AdminRepositoryConfiguration repository, Configuration configuration )
throws IOException, RoleManagerException
{
- getLogger().info( ".addRepository(" + repository + ")" );
-
// Fix the URL entry (could possibly be a filesystem path)
/* TODO! reinstate
String rawUrlEntry = repository.getUrl();
roleManager.createTemplatedRole( "archiva-repository-observer", repository.getId() );
}
- private boolean operationAllowed( String permission, String repoid )
- {
- ActionContext context = ActionContext.getContext();
- SecuritySession securitySession = (SecuritySession) context.get( SecuritySession.ROLE );
-
- try
- {
- AuthorizationResult authzResult = securitySystem.authorize( securitySession, permission, repoid );
-
- return authzResult.isAuthorized();
- }
- catch ( AuthorizationException e )
- {
- getLogger().info( "Unable to authorize permission: " + permission + " against repo: " + repoid +
- " due to: " + e.getMessage() );
- return false;
- }
- }
-
private void removeContents( AdminRepositoryConfiguration existingRepository )
throws IOException
{
- getLogger().info( "Removing " + existingRepository.getLocation() );
FileUtils.deleteDirectory( new File( existingRepository.getLocation() ) );
}
- private void removeRepository( String repoId )
+ private void removeRepository( String repoId, Configuration configuration )
{
- getLogger().info( ".removeRepository()" );
-
// TODO! what about others?
- ManagedRepositoryConfiguration toremove =
- archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
+ ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( repoId );
if ( toremove != null )
{
- archivaConfiguration.getConfiguration().removeManagedRepository( toremove );
+ configuration.removeManagedRepository( toremove );
}
}
roleManager.removeTemplatedRole( "archiva-repository-manager", existingRepository.getId() );
roleManager.removeTemplatedRole( "archiva-repository-observer", existingRepository.getId() );
- getLogger().info( "removed user roles associated with repository " + existingRepository.getId() );
+ getLogger().debug( "removed user roles associated with repository " + existingRepository.getId() );
}
private String saveConfiguration( Configuration configuration )
throws IOException, InvalidConfigurationException, RegistryException
{
- getLogger().info( ".saveConfiguration()" );
-
try
{
archivaConfiguration.save( configuration );
{
this.archivaConfiguration = archivaConfiguration;
}
+
}
import com.opensymphony.xwork.Action;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
+import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
private ArchivaConfiguration archivaConfiguration;
+ private static final String REPO_ID = "repo-ident";
+
protected void setUp()
throws Exception
{
public void testAddRepository()
throws Exception
{
- String repoId = "repo-ident";
// TODO: should be in the business model
- roleManager.createTemplatedRole( "archiva-repository-manager", repoId );
- roleManager.createTemplatedRole( "archiva-repository-observer", repoId );
+ roleManager.createTemplatedRole( "archiva-repository-manager", REPO_ID );
+ roleManager.createTemplatedRole( "archiva-repository-observer", REPO_ID );
roleManagerControl.replay();
action.prepare();
action.setMode( "add" );
AdminRepositoryConfiguration repository = action.getRepository();
- repository.setId( repoId );
+ populateRepository( repository );
+
+ String status = action.save();
+ assertEquals( Action.SUCCESS, status );
+
+ assertEquals( configuration.getManagedRepositories(), Collections.singletonList( repository ) );
+
+ roleManagerControl.verify();
+ archivaConfigurationControl.verify();
+ }
+
+ public void testEditRepositoryInitialPage()
+ throws Exception
+ {
+ Configuration configuration = createConfigurationForEditing();
+
+ archivaConfiguration.getConfiguration();
+ archivaConfigurationControl.setReturnValue( configuration );
+ archivaConfigurationControl.replay();
+
+ action.setRepoid( REPO_ID );
+
+ action.prepare();
+ assertEquals( REPO_ID, action.getRepoid() );
+ assertNull( action.getMode() );
+ AdminRepositoryConfiguration repository = action.getRepository();
+ assertNotNull( repository );
+ assertRepositoryEquals( repository, createRepository() );
+
+ String status = action.edit();
+ assertEquals( Action.INPUT, status );
+ repository = action.getRepository();
+ assertRepositoryEquals( repository, createRepository() );
+ }
+
+ private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
+ ManagedRepositoryConfiguration actualRepository )
+ {
+ assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
+ assertEquals( expectedRepository.getId(), actualRepository.getId() );
+ assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
+ assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
+ assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
+ assertEquals( expectedRepository.getName(), actualRepository.getName() );
+ assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
+ assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
+ assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
+ assertEquals( expectedRepository.isIndexed(), actualRepository.isIndexed() );
+ assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
+ assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
+ }
+
+ private static Configuration createConfigurationForEditing()
+ {
+ Configuration configuration = new Configuration();
+ ManagedRepositoryConfiguration r = createRepository();
+ configuration.addManagedRepository( r );
+ return configuration;
+ }
+
+ private static ManagedRepositoryConfiguration createRepository()
+ {
+ ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
+ r.setId( REPO_ID );
+ populateRepository( r );
+ return r;
+ }
+
+ public void testEditRepository()
+ throws Exception
+ {
+ // TODO: should be in the business model
+ roleManager.createTemplatedRole( "archiva-repository-manager", REPO_ID );
+ roleManager.createTemplatedRole( "archiva-repository-observer", REPO_ID );
+
+ roleManagerControl.replay();
+
+ Configuration configuration = createConfigurationForEditing();
+ archivaConfiguration.getConfiguration();
+ archivaConfigurationControl.setReturnValue( configuration );
+ archivaConfiguration.getConfiguration();
+ archivaConfigurationControl.setReturnValue( configuration );
+
+ archivaConfiguration.save( configuration );
+
+ archivaConfigurationControl.replay();
+
+ action.prepare();
+ action.setMode( "edit" );
+ AdminRepositoryConfiguration repository = action.getRepository();
+ populateRepository( repository );
+ repository.setName( "new repo name" );
+
+ String status = action.save();
+ assertEquals( Action.SUCCESS, status );
+
+ ManagedRepositoryConfiguration newRepository = createRepository();
+ newRepository.setName( "new repo name" );
+ assertRepositoryEquals( repository, newRepository );
+ assertEquals( configuration.getManagedRepositories(), Collections.singletonList( repository ) );
+
+ roleManagerControl.verify();
+ archivaConfigurationControl.verify();
+ }
+
+ private static void populateRepository( ManagedRepositoryConfiguration repository )
+ {
+ repository.setId( REPO_ID );
repository.setName( "repo name" );
repository.setLocation( "location" );
repository.setLayout( "default" );
repository.setSnapshots( true );
repository.setIndexed( true );
repository.setDeleteReleasedSnapshots( true );
-
- String status = action.save();
- assertEquals( Action.SUCCESS, status );
-
- assertEquals( configuration.getManagedRepositories(), Collections.singletonList( repository ) );
-
- roleManagerControl.verify();
- archivaConfigurationControl.verify();
}
// TODO: test errors during add, other actions