1 package org.apache.maven.archiva.security;
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 java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
29 import org.codehaus.plexus.redback.authorization.AuthorizationException;
30 import org.codehaus.plexus.redback.rbac.RBACManager;
31 import org.codehaus.plexus.redback.rbac.RbacManagerException;
32 import org.codehaus.plexus.redback.rbac.RbacObjectNotFoundException;
33 import org.codehaus.plexus.redback.rbac.Role;
34 import org.codehaus.plexus.redback.role.RoleManager;
35 import org.codehaus.plexus.redback.role.RoleManagerException;
36 import org.codehaus.plexus.redback.system.DefaultSecuritySession;
37 import org.codehaus.plexus.redback.system.SecuritySession;
38 import org.codehaus.plexus.redback.system.SecuritySystem;
39 import org.codehaus.plexus.redback.users.User;
40 import org.codehaus.plexus.redback.users.UserNotFoundException;
43 * DefaultUserRepositories
46 * @plexus.component role="org.apache.maven.archiva.security.UserRepositories" role-hint="default"
48 public class DefaultUserRepositories
49 implements UserRepositories
54 private SecuritySystem securitySystem;
57 * @plexus.requirement role-hint="cached"
59 private RBACManager rbacManager;
62 * @plexus.requirement role-hint="default"
64 private RoleManager roleManager;
69 private ArchivaConfiguration archivaConfiguration;
71 public List<String> getObservableRepositoryIds( String principal )
72 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
74 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS;
76 return getAccessibleRepositoryIds( principal, operation );
79 public List<String> getManagableRepositoryIds( String principal )
80 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
82 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD;
84 return getAccessibleRepositoryIds( principal, operation );
87 private List<String> getAccessibleRepositoryIds( String principal, String operation )
88 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
92 User user = securitySystem.getUserManager().findUser( principal );
95 throw new ArchivaSecurityException( "The security system had an internal error - please check your system logs" );
98 if ( user.isLocked() )
100 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
103 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
104 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
106 List<String> repoIds = new ArrayList<String>();
108 List<ManagedRepositoryConfiguration> repos =
109 archivaConfiguration.getConfiguration().getManagedRepositories();
111 for ( ManagedRepositoryConfiguration repo : repos )
115 String repoId = repo.getId();
116 if ( securitySystem.isAuthorized( securitySession, operation, repoId ) )
118 repoIds.add( repoId );
121 catch ( AuthorizationException e )
129 catch ( UserNotFoundException e )
131 throw new PrincipalNotFoundException( "Unable to find principal " + principal + "" );
135 public void createMissingRepositoryRoles( String repoId )
136 throws ArchivaSecurityException
140 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
142 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
145 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
147 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
150 catch ( RoleManagerException e )
152 throw new ArchivaSecurityException(
153 "Unable to create roles for configured repositories: " + e.getMessage(),
158 public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
159 throws PrincipalNotFoundException, ArchivaSecurityException
163 User user = securitySystem.getUserManager().findUser( principal );
166 throw new ArchivaSecurityException( "The security system had an internal error - please check your system logs" );
169 if ( user.isLocked() )
171 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
174 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
175 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
177 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
181 catch ( UserNotFoundException e )
183 throw new PrincipalNotFoundException( "Unable to find principal " + principal + "" );
185 catch ( AuthorizationException e )
187 throw new ArchivaSecurityException( e.getMessage() );
191 public boolean isAuthorizedToDeleteArtifacts( String principal, String repoId )
192 throws RbacManagerException, RbacObjectNotFoundException
194 boolean isAuthorized = false;
195 String delimiter = " - ";
199 Collection<Role> roleList = rbacManager.getEffectivelyAssignedRoles( principal );
201 for ( Role role : roleList )
203 String roleName = role.getName();
205 if ( roleName.startsWith( ArchivaRoleConstants.REPOSITORY_MANAGER_ROLE_PREFIX ) )
207 int delimiterIndex = roleName.indexOf( delimiter );
208 String resourceName = roleName.substring( delimiterIndex + delimiter.length() );
210 if ( resourceName.equals( repoId ) )
218 catch ( RbacObjectNotFoundException e )
220 throw new RbacObjectNotFoundException( "Unable to find user " + principal + "" );
222 catch ( RbacManagerException e )
224 throw new RbacManagerException( "Unable to get roles for user " + principal + "" );