You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultUserRepositories.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package org.apache.maven.archiva.security;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.apache.maven.archiva.configuration.ArchivaConfiguration;
  23. import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
  24. import org.codehaus.plexus.redback.authentication.AuthenticationResult;
  25. import org.codehaus.plexus.redback.authorization.AuthorizationException;
  26. import org.codehaus.plexus.redback.rbac.RBACManager;
  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. /**
  35. * DefaultUserRepositories
  36. *
  37. * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
  38. * @version $Id$
  39. *
  40. * @plexus.component role="org.apache.maven.archiva.security.UserRepositories"
  41. * role-hint="default"
  42. */
  43. public class DefaultUserRepositories
  44. implements UserRepositories
  45. {
  46. /**
  47. * @plexus.requirement
  48. */
  49. private SecuritySystem securitySystem;
  50. /**
  51. * @plexus.requirement role-hint="cached"
  52. */
  53. private RBACManager rbacManager;
  54. /**
  55. * @plexus.requirement role-hint="default"
  56. */
  57. private RoleManager roleManager;
  58. /**
  59. * @plexus.requirement
  60. */
  61. private ArchivaConfiguration archivaConfiguration;
  62. public List<String> getObservableRepositoryIds( String principal )
  63. throws PrincipalNotFoundException, AccessDeniedException, ArchivaSecurityException
  64. {
  65. try
  66. {
  67. User user = securitySystem.getUserManager().findUser( principal );
  68. if ( user.isLocked() )
  69. {
  70. throw new AccessDeniedException( "User " + principal + "(" + user.getFullName() + ") is locked." );
  71. }
  72. AuthenticationResult authn = new AuthenticationResult( true, principal, null );
  73. SecuritySession securitySession = new DefaultSecuritySession( authn, user );
  74. List<String> repoIds = new ArrayList<String>();
  75. List<ManagedRepositoryConfiguration> repos = archivaConfiguration.getConfiguration().getManagedRepositories();
  76. for ( ManagedRepositoryConfiguration repo : repos )
  77. {
  78. try
  79. {
  80. String repoId = repo.getId();
  81. if ( securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS, repoId ) )
  82. {
  83. repoIds.add( repoId );
  84. }
  85. }
  86. catch ( AuthorizationException e )
  87. {
  88. // swallow.
  89. }
  90. }
  91. return repoIds;
  92. }
  93. catch ( UserNotFoundException e )
  94. {
  95. throw new PrincipalNotFoundException( "Unable to find principal " + principal + "" );
  96. }
  97. }
  98. public void createMissingRepositoryRoles( String repoId )
  99. throws ArchivaSecurityException
  100. {
  101. try
  102. {
  103. if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
  104. {
  105. roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
  106. }
  107. if ( !roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
  108. {
  109. roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
  110. }
  111. }
  112. catch ( RoleManagerException e )
  113. {
  114. throw new ArchivaSecurityException( "Unable to create roles for configured repositories: " + e.getMessage(),
  115. e );
  116. }
  117. }
  118. }