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.Iterator;
25 import java.util.List;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.security.ArchivaRoleConstants;
30 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
31 import org.codehaus.plexus.redback.authorization.AuthorizationException;
32 import org.codehaus.plexus.redback.rbac.RBACManager;
33 import org.codehaus.plexus.redback.rbac.RbacObjectNotFoundException;
34 import org.codehaus.plexus.redback.rbac.RbacManagerException;
35 import org.codehaus.plexus.redback.rbac.Role;
36 import org.codehaus.plexus.redback.role.RoleManager;
37 import org.codehaus.plexus.redback.role.RoleManagerException;
38 import org.codehaus.plexus.redback.system.DefaultSecuritySession;
39 import org.codehaus.plexus.redback.system.SecuritySession;
40 import org.codehaus.plexus.redback.system.SecuritySystem;
41 import org.codehaus.plexus.redback.users.User;
42 import org.codehaus.plexus.redback.users.UserNotFoundException;
45 * DefaultUserRepositories
48 * @plexus.component role="org.apache.maven.archiva.security.UserRepositories" role-hint="default"
50 public class DefaultUserRepositories
51 implements UserRepositories
56 private SecuritySystem securitySystem;
59 * @plexus.requirement role-hint="cached"
61 private RBACManager rbacManager;
64 * @plexus.requirement role-hint="default"
66 private RoleManager roleManager;
71 private ArchivaConfiguration archivaConfiguration;
73 public List<String> getObservableRepositoryIds( String principal )
74 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
79 User user = securitySystem.getUserManager().findUser( principal );
82 throw new ArchivaSecurityException( "The security system had an internal error - please check your system logs" );
85 if ( user.isLocked() )
87 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
90 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
91 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
93 List<String> repoIds = new ArrayList<String>();
95 List<ManagedRepositoryConfiguration> repos =
96 archivaConfiguration.getConfiguration().getManagedRepositories();
98 for ( ManagedRepositoryConfiguration repo : repos )
102 String repoId = repo.getId();
103 if ( securitySystem.isAuthorized( securitySession,
104 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS, repoId ) )
106 repoIds.add( repoId );
109 catch ( AuthorizationException e )
117 catch ( UserNotFoundException e )
119 throw new PrincipalNotFoundException( "Unable to find principal " + principal + "" );
123 public void createMissingRepositoryRoles( String repoId )
124 throws ArchivaSecurityException
128 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
130 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
133 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
135 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
138 catch ( RoleManagerException e )
140 throw new ArchivaSecurityException(
141 "Unable to create roles for configured repositories: " + e.getMessage(),
146 public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
147 throws PrincipalNotFoundException, ArchivaSecurityException
151 User user = securitySystem.getUserManager().findUser( principal );
154 throw new ArchivaSecurityException( "The security system had an internal error - please check your system logs" );
157 if ( user.isLocked() )
159 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
162 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
163 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
165 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
169 catch ( UserNotFoundException e )
171 throw new PrincipalNotFoundException( "Unable to find principal " + principal + "" );
173 catch ( AuthorizationException e )
175 throw new ArchivaSecurityException( e.getMessage() );
179 public boolean isAuthorizedToDeleteArtifacts( String principal, String repoId )
180 throws RbacManagerException, RbacObjectNotFoundException
182 boolean isAuthorized = false;
183 String delimiter = " - ";
187 Collection roleList = rbacManager.getEffectivelyAssignedRoles( principal );
189 Iterator it = roleList.iterator();
191 while ( it.hasNext() )
193 Role role = (Role) it.next();
195 String roleName = role.getName();
197 if ( roleName.startsWith( ArchivaRoleConstants.REPOSITORY_MANAGER_ROLE_PREFIX ) )
199 int delimiterIndex = roleName.indexOf( delimiter );
200 String resourceName = roleName.substring( delimiterIndex + delimiter.length() );
202 if ( resourceName.equals( repoId ) )
210 catch ( RbacObjectNotFoundException e )
212 throw new RbacObjectNotFoundException( "Unable to find user " + principal + "" );
214 catch ( RbacManagerException e )
216 throw new RbacManagerException( "Unable to get roles for user " + principal + "" );