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.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.redback.users.User;
27 import org.apache.archiva.redback.users.UserNotFoundException;
28 import org.apache.archiva.security.common.ArchivaRoleConstants;
29 import org.apache.archiva.redback.authentication.AuthenticationResult;
30 import org.apache.archiva.redback.authorization.AuthorizationException;
31 import org.apache.archiva.redback.role.RoleManager;
32 import org.apache.archiva.redback.role.RoleManagerException;
33 import org.apache.archiva.redback.system.DefaultSecuritySession;
34 import org.apache.archiva.redback.system.SecuritySession;
35 import org.apache.archiva.redback.system.SecuritySystem;
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
49 @Service( "userRepositories" )
50 public class DefaultUserRepositories
51 implements UserRepositories
55 private SecuritySystem securitySystem;
58 private RoleManager roleManager;
61 private ManagedRepositoryAdmin managedRepositoryAdmin;
63 private Logger log = LoggerFactory.getLogger( getClass() );
65 public List<String> getObservableRepositoryIds( String principal )
66 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
68 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS;
70 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<String>( managedRepositories.size() );
87 for ( ManagedRepository managedRepository : managedRepositories )
89 repoIds.add( managedRepository.getId() );
95 public List<ManagedRepository> getAccessibleRepositories( String principal )
96 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
98 return getAccessibleRepositories( principal, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
101 private List<ManagedRepository> getAccessibleRepositories( String principal, String operation )
102 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
104 SecuritySession securitySession = createSession( principal );
106 List<ManagedRepository> managedRepositories = new ArrayList<ManagedRepository>();
110 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
112 for ( ManagedRepository repo : repos )
116 String repoId = repo.getId();
117 if ( securitySystem.isAuthorized( securitySession, operation, repoId ) )
119 managedRepositories.add( repo );
122 catch ( AuthorizationException e )
125 if ( log.isDebugEnabled() )
127 log.debug( "Not authorizing '{}' for repository '{}': {}",
128 Lists.<Object>newArrayList( principal, repo.getId(), e.getMessage() ) );
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 + "" );
159 if ( user.isLocked() )
161 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
164 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
165 return new DefaultSecuritySession( authn, user );
168 public void createMissingRepositoryRoles( String repoId )
169 throws ArchivaSecurityException
173 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
175 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
178 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
180 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
183 catch ( RoleManagerException e )
185 throw new ArchivaSecurityException( "Unable to create roles for configured repositories: " + e.getMessage(),
190 public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
191 throws PrincipalNotFoundException, ArchivaSecurityException
195 SecuritySession securitySession = createSession( principal );
197 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
201 catch ( AuthorizationException e )
203 throw new ArchivaSecurityException( e.getMessage() );
207 public boolean isAuthorizedToDeleteArtifacts( String principal, String repoId )
208 throws ArchivaSecurityException
212 SecuritySession securitySession = createSession( principal );
214 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_DELETE,
218 catch ( AuthorizationException e )
220 throw new ArchivaSecurityException( e.getMessage() );
224 public SecuritySystem getSecuritySystem()
226 return securitySystem;
229 public void setSecuritySystem( SecuritySystem securitySystem )
231 this.securitySystem = securitySystem;
234 public RoleManager getRoleManager()
239 public void setRoleManager( RoleManager roleManager )
241 this.roleManager = roleManager;