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 org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.redback.authentication.AuthenticationResult;
26 import org.apache.archiva.redback.authorization.AuthorizationException;
27 import org.apache.archiva.redback.role.RoleManager;
28 import org.apache.archiva.redback.role.RoleManagerException;
29 import org.apache.archiva.redback.system.DefaultSecuritySession;
30 import org.apache.archiva.redback.system.SecuritySession;
31 import org.apache.archiva.redback.system.SecuritySystem;
32 import org.apache.archiva.redback.users.User;
33 import org.apache.archiva.redback.users.UserNotFoundException;
34 import org.apache.archiva.security.common.ArchivaRoleConstants;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Inject;
40 import java.util.ArrayList;
41 import java.util.List;
44 * DefaultUserRepositories
46 @Service ( "userRepositories" )
47 public class DefaultUserRepositories
48 implements UserRepositories
52 private SecuritySystem securitySystem;
55 private RoleManager roleManager;
58 private ManagedRepositoryAdmin managedRepositoryAdmin;
60 private Logger log = LoggerFactory.getLogger( getClass() );
62 public List<String> getObservableRepositoryIds( String principal )
63 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
65 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS;
67 return getAccessibleRepositoryIds( principal, operation );
70 public List<String> getManagableRepositoryIds( String principal )
71 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
73 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD;
75 return getAccessibleRepositoryIds( principal, operation );
78 private List<String> getAccessibleRepositoryIds( String principal, String operation )
79 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
82 List<ManagedRepository> managedRepositories = getAccessibleRepositories( principal, operation );
83 List<String> repoIds = new ArrayList<String>( managedRepositories.size() );
84 for ( ManagedRepository managedRepository : managedRepositories )
86 repoIds.add( managedRepository.getId() );
92 public List<ManagedRepository> getAccessibleRepositories( String principal )
93 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
95 return getAccessibleRepositories( principal, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
98 private List<ManagedRepository> getAccessibleRepositories( String principal, String operation )
99 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
101 SecuritySession securitySession = createSession( principal );
103 List<ManagedRepository> managedRepositories = new ArrayList<ManagedRepository>();
107 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
109 for ( ManagedRepository repo : repos )
113 String repoId = repo.getId();
114 if ( securitySystem.isAuthorized( securitySession, operation, repoId ) )
116 managedRepositories.add( repo );
119 catch ( AuthorizationException e )
123 log.debug( "Not authorizing '{}' for repository '{}': {}", principal, repo.getId(),
129 return managedRepositories;
131 catch ( RepositoryAdminException e )
133 throw new ArchivaSecurityException( e.getMessage(), e );
137 private SecuritySession createSession( String principal )
138 throws ArchivaSecurityException, AccessDeniedException
143 user = securitySystem.getUserManager().findUser( principal );
146 throw new ArchivaSecurityException(
147 "The security system had an internal error - please check your system logs" );
150 catch ( UserNotFoundException e )
152 throw new PrincipalNotFoundException( "Unable to find principal " + principal + "", e );
155 if ( user.isLocked() )
157 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
160 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
161 return new DefaultSecuritySession( authn, user );
164 public void createMissingRepositoryRoles( String repoId )
165 throws ArchivaSecurityException
169 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
171 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
174 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
176 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
179 catch ( RoleManagerException e )
181 throw new ArchivaSecurityException( "Unable to create roles for configured repositories: " + e.getMessage(),
186 public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
187 throws PrincipalNotFoundException, ArchivaSecurityException
191 SecuritySession securitySession = createSession( principal );
193 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
197 catch ( AuthorizationException e )
199 throw new ArchivaSecurityException( e.getMessage() );
203 public boolean isAuthorizedToDeleteArtifacts( String principal, String repoId )
204 throws ArchivaSecurityException
208 SecuritySession securitySession = createSession( principal );
210 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_DELETE,
214 catch ( AuthorizationException e )
216 throw new ArchivaSecurityException( e.getMessage() );
220 public SecuritySystem getSecuritySystem()
222 return securitySystem;
225 public void setSecuritySystem( SecuritySystem securitySystem )
227 this.securitySystem = securitySystem;
230 public RoleManager getRoleManager()
235 public void setRoleManager( RoleManager roleManager )
237 this.roleManager = roleManager;