class 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;
    }

    public  BranchAccessAuthorizer();
    @Inject public  BranchAccessAuthorizer(final SpaceConfigStorageRegistry spaceConfigStorageRegistry,
                                  final ConfiguredRepositories configuredRepositories,
                                  final OrganizationalUnitService organizationalUnitService);

}