1 package org.apache.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 com.google.common.collect.Lists;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
26 import org.codehaus.plexus.redback.authorization.AuthorizationException;
27 import org.codehaus.plexus.redback.role.RoleManager;
28 import org.codehaus.plexus.redback.role.RoleManagerException;
29 import org.codehaus.plexus.redback.system.DefaultSecuritySession;
30 import org.codehaus.plexus.redback.system.SecuritySession;
31 import org.codehaus.plexus.redback.system.SecuritySystem;
32 import org.codehaus.plexus.redback.users.User;
33 import org.codehaus.plexus.redback.users.UserNotFoundException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import java.util.ArrayList;
40 import java.util.List;
43 * DefaultUserRepositories
47 @Service( "userRepositories" )
48 public class DefaultUserRepositories
49 implements UserRepositories
55 private SecuritySystem securitySystem;
58 * plexus.requirement role-hint="default"
61 private RoleManager roleManager;
67 private ArchivaConfiguration archivaConfiguration;
69 private Logger log = LoggerFactory.getLogger( DefaultUserRepositories.class );
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
90 SecuritySession securitySession = createSession( principal );
92 List<String> repoIds = new ArrayList<String>();
94 List<ManagedRepositoryConfiguration> repos = archivaConfiguration.getConfiguration().getManagedRepositories();
96 for ( ManagedRepositoryConfiguration repo : repos )
100 String repoId = repo.getId();
101 if ( securitySystem.isAuthorized( securitySession, operation, repoId ) )
103 repoIds.add( repoId );
106 catch ( AuthorizationException e )
109 if ( log.isDebugEnabled() )
111 log.debug( "Not authorizing '{}' for repository '{}': {}",
112 Lists.<Object>newArrayList( principal, repo.getId(), e.getMessage() ) );
120 private SecuritySession createSession( String principal )
121 throws ArchivaSecurityException, AccessDeniedException
126 user = securitySystem.getUserManager().findUser( principal );
129 throw new ArchivaSecurityException(
130 "The security system had an internal error - please check your system logs" );
133 catch ( UserNotFoundException e )
135 throw new PrincipalNotFoundException( "Unable to find principal " + principal + "" );
138 if ( user.isLocked() )
140 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
143 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
144 return new DefaultSecuritySession( authn, user );
147 public void createMissingRepositoryRoles( String repoId )
148 throws ArchivaSecurityException
152 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
154 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
157 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
159 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
162 catch ( RoleManagerException e )
164 throw new ArchivaSecurityException( "Unable to create roles for configured repositories: " + e.getMessage(),
169 public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
170 throws PrincipalNotFoundException, ArchivaSecurityException
174 SecuritySession securitySession = createSession( principal );
176 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
180 catch ( AuthorizationException e )
182 throw new ArchivaSecurityException( e.getMessage() );
186 public boolean isAuthorizedToDeleteArtifacts( String principal, String repoId )
187 throws AccessDeniedException, ArchivaSecurityException
191 SecuritySession securitySession = createSession( principal );
193 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_DELETE,
197 catch ( AuthorizationException e )
199 throw new ArchivaSecurityException( e.getMessage() );
203 public SecuritySystem getSecuritySystem()
205 return securitySystem;
208 public void setSecuritySystem( SecuritySystem securitySystem )
210 this.securitySystem = securitySystem;
213 public RoleManager getRoleManager()
218 public void setRoleManager( RoleManager roleManager )
220 this.roleManager = roleManager;
223 public ArchivaConfiguration getArchivaConfiguration()
225 return archivaConfiguration;
228 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
230 this.archivaConfiguration = archivaConfiguration;