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.UserManagerException;
34 import org.apache.archiva.redback.users.UserNotFoundException;
35 import org.apache.archiva.security.common.ArchivaRoleConstants;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Service;
40 import javax.inject.Inject;
41 import java.util.ArrayList;
42 import java.util.List;
45 * DefaultUserRepositories
47 @Service( "userRepositories" )
48 public class DefaultUserRepositories
49 implements UserRepositories
53 private SecuritySystem securitySystem;
56 private RoleManager roleManager;
59 private ManagedRepositoryAdmin managedRepositoryAdmin;
61 private Logger log = LoggerFactory.getLogger( getClass() );
64 public List<String> getObservableRepositoryIds( String principal )
65 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
67 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS;
69 return getAccessibleRepositoryIds( principal, operation );
73 public List<String> getManagableRepositoryIds( String principal )
74 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
76 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD;
78 return getAccessibleRepositoryIds( principal, operation );
81 private List<String> getAccessibleRepositoryIds( String principal, String operation )
82 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
85 List<ManagedRepository> managedRepositories = getAccessibleRepositories( principal, operation );
86 List<String> repoIds = new ArrayList<>( managedRepositories.size() );
87 for ( ManagedRepository managedRepository : managedRepositories )
89 repoIds.add( managedRepository.getId() );
96 public List<ManagedRepository> getAccessibleRepositories( String principal )
97 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
99 return getAccessibleRepositories( principal, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
102 private List<ManagedRepository> getAccessibleRepositories( String principal, String operation )
103 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
105 SecuritySession securitySession = createSession( principal );
107 List<ManagedRepository> managedRepositories = new ArrayList<>();
111 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
113 for ( ManagedRepository repo : repos )
117 String repoId = repo.getId();
118 if ( securitySystem.isAuthorized( securitySession, operation, repoId ) )
120 managedRepositories.add( repo );
123 catch ( AuthorizationException e )
127 log.debug( "Not authorizing '{}' for repository '{}': {}", principal, repo.getId(),
133 return managedRepositories;
135 catch ( RepositoryAdminException e )
137 throw new ArchivaSecurityException( e.getMessage(), e );
141 private SecuritySession createSession( String principal )
142 throws ArchivaSecurityException, AccessDeniedException
147 user = securitySystem.getUserManager().findUser( principal );
150 throw new ArchivaSecurityException(
151 "The security system had an internal error - please check your system logs" );
154 catch ( UserNotFoundException e )
156 throw new PrincipalNotFoundException( "Unable to find principal " + principal + "", e );
158 catch ( UserManagerException e )
160 throw new ArchivaSecurityException( e.getMessage(), e );
163 if ( user.isLocked() )
165 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
168 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
169 authn.setUser( user );
170 return new DefaultSecuritySession( authn, user );
174 public void createMissingRepositoryRoles( String repoId )
175 throws ArchivaSecurityException
179 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
181 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
184 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
186 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
189 catch ( RoleManagerException e )
191 throw new ArchivaSecurityException( "Unable to create roles for configured repositories: " + e.getMessage(),
197 public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
198 throws PrincipalNotFoundException, ArchivaSecurityException
202 SecuritySession securitySession = createSession( principal );
204 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
208 catch ( AuthorizationException e )
210 throw new ArchivaSecurityException( e.getMessage(), e);
215 public boolean isAuthorizedToDeleteArtifacts( String principal, String repoId )
216 throws ArchivaSecurityException
220 SecuritySession securitySession = createSession( principal );
222 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_DELETE,
226 catch ( AuthorizationException e )
228 throw new ArchivaSecurityException( e.getMessage(), e);
232 public SecuritySystem getSecuritySystem()
234 return securitySystem;
237 public void setSecuritySystem( SecuritySystem securitySystem )
239 this.securitySystem = securitySystem;
242 public RoleManager getRoleManager()
247 public void setRoleManager( RoleManager roleManager )
249 this.roleManager = roleManager;