add flag for anonymous mode availability to scm protocols and filter supported protocols accordingly

This commit is contained in:
Konstantin Schaper
2020-10-30 10:58:36 +01:00
parent 681cd52811
commit 7a712f8d9e
3 changed files with 11 additions and 6 deletions

View File

@@ -34,6 +34,7 @@ import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.spi.RepositoryServiceProvider;
import sonia.scm.repository.work.WorkdirProvider;
import sonia.scm.security.Authentications;
import sonia.scm.user.EMail;
import javax.annotation.Nullable;
@@ -453,7 +454,8 @@ public final class RepositoryService implements Closeable {
public Stream<ScmProtocol> getSupportedProtocols() {
return protocolProviders.stream()
.filter(protocolProvider -> protocolProvider.getType().equals(getRepository().getType()))
.map(this::createProviderInstanceForRepository);
.map(this::createProviderInstanceForRepository)
.filter(protocol -> !Authentications.isAuthenticatedSubjectAnonymous() || protocol.isAnonymousEnabled());
}
@SuppressWarnings({"rawtypes", "java:S3740"})

View File

@@ -40,4 +40,11 @@ public interface ScmProtocol {
* The URL to access the repository providing this protocol.
*/
String getUrl();
/**
* Whether the protocol can be used as an anonymous user.
*/
default boolean isAnonymousEnabled() {
return true;
}
}

View File

@@ -93,11 +93,7 @@ public abstract class RepositoryToRepositoryDtoMapper extends BaseMapper<Reposit
}
try (RepositoryService repositoryService = serviceFactory.create(repository)) {
if (RepositoryPermissions.pull(repository).isPermitted()) {
Stream<ScmProtocol> supportedProtocols = repositoryService.getSupportedProtocols();
if (Authentications.isAuthenticatedSubjectAnonymous()) {
supportedProtocols = supportedProtocols.filter(p -> !p.getType().equals("ssh"));
}
List<Link> protocolLinks = supportedProtocols
List<Link> protocolLinks = repositoryService.getSupportedProtocols()
.map(this::createProtocolLink)
.collect(toList());
linksBuilder.array(protocolLinks);