BranchAccessAuthorizer { public boolean authorize(final String user, final String spaceName, final String repositoryIdentifier, final String repositoryAlias, final String branchName, final AccessType accessType) { final BranchPermissions branchPermissions = spaceConfigStorageRegistry.get(spaceName).loadBranchPermissions(branchName, repositoryIdentifier); final OrganizationalUnit organizationalUnit = organizationalUnitService.getOrganizationalUnit(spaceName); final Repository repository = configuredRepositories.getRepositoryByRepositoryAlias(organizationalUnit.getSpace(), repositoryAlias); final Optional<Contributor> userContributor = repository.getContributors().stream().filter(c -> c.getUsername().equals(user)).findFirst(); if (userContributor.isPresent()) { final String userRole = userContributor.get().getType().name(); final RolePermissions rolePermissions = branchPermissions.getPermissionsByRole().get(userRole); if (AccessType.READ.equals(accessType)) { return rolePermissions.canRead(); } else if (AccessType.WRITE.equals(accessType)) { return rolePermissions.canWrite(); } else if (AccessType.DELETE.equals(accessType)) { return rolePermissions.canDelete(); } } return true; } BranchAccessAuthorizer(); @Inject  BranchAccessAuthorizer(final SpaceConfigStorageRegistry spaceConfigStorageRegistry,
                                  final ConfiguredRepositories configuredRepositories,
                                  final OrganizationalUnitService organizationalUnitService); boolean authorize(final String user,
                             final String spaceName,
                             final String repositoryIdentifier,
                             final String repositoryAlias,
                             final String branchName,
                             final AccessType accessType);  }
@Test public void authorizeOwnerTest() { assertTrue(branchAccessAuthorizer.authorize("owner", "space", "repositoryIdentifier", "repositoryAlias", "branch", BranchAccessAuthorizer.AccessType.READ)); assertTrue(branchAccessAuthorizer.authorize("owner", "space", "repositoryIdentifier", "repositoryAlias", "branch", BranchAccessAuthorizer.AccessType.WRITE)); assertTrue(branchAccessAuthorizer.authorize("owner", "space", "repositoryIdentifier", "repositoryAlias", "branch", BranchAccessAuthorizer.AccessType.DELETE)); }
@Test public void authorizeAdminTest() { assertTrue(branchAccessAuthorizer.authorize("admin", "space", "repositoryIdentifier", "repositoryAlias", "branch", BranchAccessAuthorizer.AccessType.READ)); assertTrue(branchAccessAuthorizer.authorize("admin", "space", "repositoryIdentifier", "repositoryAlias", "branch", BranchAccessAuthorizer.AccessType.WRITE)); assertFalse(branchAccessAuthorizer.authorize("admin", "space", "repositoryIdentifier", "repositoryAlias", "branch", BranchAccessAuthorizer.AccessType.DELETE)); }
@Test public void authorizeContributorTest() { assertTrue(branchAccessAuthorizer.authorize("contributor", "space", "repositoryIdentifier", "repositoryAlias", "branch", BranchAccessAuthorizer.AccessType.READ)); assertTrue(branchAccessAuthorizer.authorize("contributor", "space", "repositoryIdentifier", "repositoryAlias", "branch", BranchAccessAuthorizer.AccessType.WRITE)); assertFalse(branchAccessAuthorizer.authorize("contributor", "space", "repositoryIdentifier", "repositoryAlias", "branch", BranchAccessAuthorizer.AccessType.DELETE)); }