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
46 * plexus.component role="org.apache.archiva.security.UserRepositories" role-hint="default"
48 @Service( "userRepositories" )
49 public class DefaultUserRepositories
50 implements UserRepositories
56 private SecuritySystem securitySystem;
59 * plexus.requirement role-hint="default"
62 private RoleManager roleManager;
68 private ArchivaConfiguration archivaConfiguration;
70 private Logger log = LoggerFactory.getLogger( DefaultUserRepositories.class );
72 public List<String> getObservableRepositoryIds( String principal )
73 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
75 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS;
77 return getAccessibleRepositoryIds( principal, operation );
80 public List<String> getManagableRepositoryIds( String principal )
81 throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
83 String operation = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD;
85 return getAccessibleRepositoryIds( principal, operation );
88 private List<String> getAccessibleRepositoryIds( String principal, String operation )
89 throws ArchivaSecurityException, AccessDeniedException, PrincipalNotFoundException
91 SecuritySession securitySession = createSession( principal );
93 List<String> repoIds = new ArrayList<String>();
95 List<ManagedRepositoryConfiguration> repos = archivaConfiguration.getConfiguration().getManagedRepositories();
97 for ( ManagedRepositoryConfiguration repo : repos )
101 String repoId = repo.getId();
102 if ( securitySystem.isAuthorized( securitySession, operation, repoId ) )
104 repoIds.add( repoId );
107 catch ( AuthorizationException e )
110 if ( log.isDebugEnabled() )
112 log.debug( "Not authorizing '{}' for repository '{}': {}",
113 Lists.<Object>newArrayList( principal, repo.getId(), e.getMessage() ) );
121 private SecuritySession createSession( String principal )
122 throws ArchivaSecurityException, AccessDeniedException
127 user = securitySystem.getUserManager().findUser( principal );
130 throw new ArchivaSecurityException(
131 "The security system had an internal error - please check your system logs" );
134 catch ( UserNotFoundException e )
136 throw new PrincipalNotFoundException( "Unable to find principal " + principal + "" );
139 if ( user.isLocked() )
141 throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
144 AuthenticationResult authn = new AuthenticationResult( true, principal, null );
145 return new DefaultSecuritySession( authn, user );
148 public void createMissingRepositoryRoles( String repoId )
149 throws ArchivaSecurityException
153 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
155 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
158 if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
160 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
163 catch ( RoleManagerException e )
165 throw new ArchivaSecurityException( "Unable to create roles for configured repositories: " + e.getMessage(),
170 public boolean isAuthorizedToUploadArtifacts( String principal, String repoId )
171 throws PrincipalNotFoundException, ArchivaSecurityException
175 SecuritySession securitySession = createSession( principal );
177 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
181 catch ( AuthorizationException e )
183 throw new ArchivaSecurityException( e.getMessage() );
187 public boolean isAuthorizedToDeleteArtifacts( String principal, String repoId )
188 throws AccessDeniedException, ArchivaSecurityException
192 SecuritySession securitySession = createSession( principal );
194 return securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_DELETE,
198 catch ( AuthorizationException e )
200 throw new ArchivaSecurityException( e.getMessage() );
204 public SecuritySystem getSecuritySystem()
206 return securitySystem;
209 public void setSecuritySystem( SecuritySystem securitySystem )
211 this.securitySystem = securitySystem;
214 public RoleManager getRoleManager()
219 public void setRoleManager( RoleManager roleManager )
221 this.roleManager = roleManager;
224 public ArchivaConfiguration getArchivaConfiguration()
226 return archivaConfiguration;
229 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
231 this.archivaConfiguration = archivaConfiguration;