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() );
63 public List<String> getObservableRepositoryIds( String principal )
64 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
66 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS;
68 return getAccessibleRepositoryIds( principal, operation );
71 public List<String> getManagableRepositoryIds( String principal )
72 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
74 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD;
76 return getAccessibleRepositoryIds( principal, operation );
79 private List<String> getAccessibleRepositoryIds( String principal, String operation )
80 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
83 List<ManagedRepository> managedRepositories = getAccessibleRepositories( principal, operation );
84 List<String> repoIds = new ArrayList<String>( managedRepositories.size() );
85 for ( ManagedRepository managedRepository : managedRepositories )
87 repoIds.add( managedRepository.getId() );
93 public List<ManagedRepository> getAccessibleRepositories( String principal )
94 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
96 return getAccessibleRepositories( principal, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
99 private List<ManagedRepository> getAccessibleRepositories( String principal, String operation )
100 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
102 SecuritySession securitySession = createSession( principal );
104 List<ManagedRepository> managedRepositories = new ArrayList<ManagedRepository>();
108 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
110 for ( ManagedRepository repo : repos )
114 String repoId = repo.getId();
115 if ( securitySystem.isAuthorized( securitySession, operation, repoId ) )
117 managedRepositories.add( repo );
120 catch ( AuthorizationException e )
124 log.debug( "Not authorizing '{}' for repository '{}': {}", principal, repo.getId(),
130 return managedRepositories;
132 catch ( RepositoryAdminException e )
134 throw new ArchivaSecurityException( e.getMessage(), e );
138 private SecuritySession createSession( String principal )
139 throws ArchivaSecurityException, AccessDeniedException
144 user = securitySystem.getUserManager().findUser( principal );
147 throw new ArchivaSecurityException(
148 "The security system had an internal error - please check your system logs" );
151 catch ( UserNotFoundException e )
153 throw new PrincipalNotFoundException( "Unable to find principal " + principal + "", e );
155 catch ( UserManagerException e )
157 throw new ArchivaSecurityException( e.getMessage(), e );
160 if ( user.isLocked() )
162 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
165 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
166 authn.setUser( user );
167 return new DefaultSecuritySession( authn, user );
170 public void createMissingRepositoryRoles( String repoId )
171 throws ArchivaSecurityException
175 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
177 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
180 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
182 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
185 catch ( RoleManagerException e )
187 throw new ArchivaSecurityException( "Unable to create roles for configured repositories: " + e.getMessage(),
192 public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
193 throws PrincipalNotFoundException, ArchivaSecurityException
197 SecuritySession securitySession = createSession( principal );
199 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
203 catch ( AuthorizationException e )
205 throw new ArchivaSecurityException( e.getMessage() );
209 public boolean isAuthorizedToDeleteArtifacts( String principal, String repoId )
210 throws ArchivaSecurityException
214 SecuritySession securitySession = createSession( principal );
216 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_DELETE,
220 catch ( AuthorizationException e )
222 throw new ArchivaSecurityException( e.getMessage() );
226 public SecuritySystem getSecuritySystem()
228 return securitySystem;
231 public void setSecuritySystem( SecuritySystem securitySystem )
233 this.securitySystem = securitySystem;
236 public RoleManager getRoleManager()
241 public void setRoleManager( RoleManager roleManager )
243 this.roleManager = roleManager;