mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-27 00:40:51 +01:00
Add namespace owner on namespace creation
Namespace owner may see the namespace configuration page. Committed-by: René Pfeuffer<rene.pfeuffer@cloudogu.com>
This commit is contained in:
@@ -27,13 +27,15 @@ package sonia.scm.repository;
|
||||
import com.github.legman.Subscribe;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import sonia.scm.HandlerEventType;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.web.security.AdministrationContext;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||
import static sonia.scm.NotFoundException.notFound;
|
||||
|
||||
@@ -43,12 +45,14 @@ public class DefaultNamespaceManager implements NamespaceManager {
|
||||
private final RepositoryManager repositoryManager;
|
||||
private final NamespaceDao dao;
|
||||
private final ScmEventBus eventBus;
|
||||
private final AdministrationContext administrationContext;
|
||||
|
||||
@Inject
|
||||
public DefaultNamespaceManager(RepositoryManager repositoryManager, NamespaceDao dao, ScmEventBus eventBus) {
|
||||
public DefaultNamespaceManager(RepositoryManager repositoryManager, NamespaceDao dao, ScmEventBus eventBus, AdministrationContext administrationContext) {
|
||||
this.repositoryManager = repositoryManager;
|
||||
this.dao = dao;
|
||||
this.eventBus = eventBus;
|
||||
this.administrationContext = administrationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,12 +71,12 @@ public class DefaultNamespaceManager implements NamespaceManager {
|
||||
.getAllNamespaces()
|
||||
.stream()
|
||||
.map(this::createNamespaceForName)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modify(Namespace namespace) {
|
||||
NamespacePermissions.permissionWrite().check();
|
||||
NamespacePermissions.permissionWrite().check(namespace);
|
||||
Namespace oldNamespace = get(namespace.getNamespace())
|
||||
.orElseThrow(() -> notFound(entity(Namespace.class, namespace.getNamespace())));
|
||||
fireEvent(HandlerEventType.BEFORE_MODIFY, namespace, oldNamespace);
|
||||
@@ -80,18 +84,41 @@ public class DefaultNamespaceManager implements NamespaceManager {
|
||||
fireEvent(HandlerEventType.MODIFY, namespace, oldNamespace);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void cleanupDeletedNamespaces(RepositoryEvent repositoryEvent) {
|
||||
if (namespaceRelevantChange(repositoryEvent)) {
|
||||
Collection<String> allNamespaces = repositoryManager.getAllNamespaces();
|
||||
String oldNamespace = getOldNamespace(repositoryEvent);
|
||||
if (!allNamespaces.contains(oldNamespace)) {
|
||||
dao.delete(oldNamespace);
|
||||
}
|
||||
@Subscribe(async = false)
|
||||
public void handleRepositoryEvent(RepositoryEvent repositoryEvent) {
|
||||
if (repositoryRemovedFromNamespace(repositoryEvent)) {
|
||||
cleanUpNamespaceIfEmpty(repositoryEvent);
|
||||
}
|
||||
if (repositoryCreatedInNamespace(repositoryEvent)) {
|
||||
initializeIfNeeded(repositoryEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean namespaceRelevantChange(RepositoryEvent repositoryEvent) {
|
||||
private static boolean repositoryCreatedInNamespace(RepositoryEvent repositoryEvent) {
|
||||
return repositoryEvent.getEventType() == HandlerEventType.CREATE;
|
||||
}
|
||||
|
||||
private void cleanUpNamespaceIfEmpty(RepositoryEvent repositoryEvent) {
|
||||
Collection<String> allNamespaces = repositoryManager.getAllNamespaces();
|
||||
String oldNamespace = getOldNamespace(repositoryEvent);
|
||||
if (!allNamespaces.contains(oldNamespace)) {
|
||||
dao.delete(oldNamespace);
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeIfNeeded(RepositoryEvent repositoryEvent) {
|
||||
Namespace namespace = createNamespaceForName(repositoryEvent.getItem().getNamespace());
|
||||
if (repositoryManager.getAll(r -> r.getNamespace().equals(namespace.getNamespace())).size() == 1) {
|
||||
String creatingUser = SecurityUtils.getSubject().getPrincipal().toString();
|
||||
administrationContext.runAsAdmin(() -> {
|
||||
namespace.setPermissions(singletonList(new RepositoryPermission(creatingUser, "OWNER", false)));
|
||||
modify(namespace);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean repositoryRemovedFromNamespace(RepositoryEvent repositoryEvent) {
|
||||
HandlerEventType eventType = repositoryEvent.getEventType();
|
||||
return eventType == HandlerEventType.DELETE
|
||||
|| eventType == HandlerEventType.MODIFY && !repositoryEvent.getItem().getNamespace().equals(repositoryEvent.getOldItem().getNamespace());
|
||||
@@ -106,7 +133,7 @@ public class DefaultNamespaceManager implements NamespaceManager {
|
||||
}
|
||||
|
||||
private Namespace createNamespaceForName(String namespace) {
|
||||
if (NamespacePermissions.permissionRead().isPermitted()) {
|
||||
if (NamespacePermissions.permissionRead().isPermitted(namespace)) {
|
||||
return dao.get(namespace)
|
||||
.map(Namespace::clone)
|
||||
.orElse(new Namespace(namespace));
|
||||
|
||||
Reference in New Issue
Block a user