mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
Cleanup permissions for namespace when it is removed
This commit is contained in:
@@ -25,8 +25,8 @@
|
|||||||
package sonia.scm.repository;
|
package sonia.scm.repository;
|
||||||
|
|
||||||
import com.github.legman.EventBus;
|
import com.github.legman.EventBus;
|
||||||
|
import com.github.legman.Subscribe;
|
||||||
import sonia.scm.HandlerEventType;
|
import sonia.scm.HandlerEventType;
|
||||||
import sonia.scm.event.ScmEventBus;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -80,6 +80,26 @@ public class DefaultNamespaceManager implements NamespaceManager {
|
|||||||
fireEvent(HandlerEventType.MODIFY, namespace, oldNamespace);
|
fireEvent(HandlerEventType.MODIFY, namespace, oldNamespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void cleanupDeletedNamespaces(RepositoryEvent repositoryEvent) {
|
||||||
|
HandlerEventType eventType = repositoryEvent.getEventType();
|
||||||
|
if (eventType == HandlerEventType.DELETE || eventType == HandlerEventType.MODIFY && !repositoryEvent.getItem().getNamespace().equals(repositoryEvent.getOldItem().getNamespace())) {
|
||||||
|
Collection<String> allNamespaces = repositoryManager.getAllNamespaces();
|
||||||
|
String oldNamespace = getOldNamespace(repositoryEvent);
|
||||||
|
if (!allNamespaces.contains(oldNamespace)) {
|
||||||
|
dao.delete(oldNamespace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOldNamespace(RepositoryEvent repositoryEvent) {
|
||||||
|
if (repositoryEvent.getEventType() == HandlerEventType.DELETE) {
|
||||||
|
return repositoryEvent.getItem().getNamespace();
|
||||||
|
} else {
|
||||||
|
return repositoryEvent.getOldItem().getNamespace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Namespace createNamespaceForName(String namespace) {
|
private Namespace createNamespaceForName(String namespace) {
|
||||||
return dao.get(namespace)
|
return dao.get(namespace)
|
||||||
.map(Namespace::clone)
|
.map(Namespace::clone)
|
||||||
|
|||||||
@@ -46,4 +46,8 @@ public class NamespaceDao {
|
|||||||
public void add(Namespace namespace) {
|
public void add(Namespace namespace) {
|
||||||
store.put(namespace.getNamespace(), namespace);
|
store.put(namespace.getNamespace(), namespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void delete(String namespace) {
|
||||||
|
store.remove(namespace);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,9 +40,10 @@ import java.util.Optional;
|
|||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.mockito.ArgumentMatchers.argThat;
|
import static org.mockito.ArgumentMatchers.argThat;
|
||||||
import static org.mockito.Mockito.times;
|
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
import static sonia.scm.HandlerEventType.DELETE;
|
||||||
|
import static sonia.scm.HandlerEventType.MODIFY;
|
||||||
|
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
@@ -125,4 +126,26 @@ class DefaultNamespaceManagerTest {
|
|||||||
verify(eventBus).post(argThat(event -> ((NamespaceModificationEvent)event).getEventType() == HandlerEventType.BEFORE_MODIFY));
|
verify(eventBus).post(argThat(event -> ((NamespaceModificationEvent)event).getEventType() == HandlerEventType.BEFORE_MODIFY));
|
||||||
verify(eventBus).post(argThat(event -> ((NamespaceModificationEvent)event).getEventType() == HandlerEventType.MODIFY));
|
verify(eventBus).post(argThat(event -> ((NamespaceModificationEvent)event).getEventType() == HandlerEventType.MODIFY));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldCleanUpPermissionWhenLastRepositoryOfNamespaceWasDeleted() {
|
||||||
|
when(repositoryManager.getAllNamespaces()).thenReturn(asList("universe", "rest"));
|
||||||
|
|
||||||
|
manager.cleanupDeletedNamespaces(new RepositoryEvent(DELETE, new Repository("1", "git", "life", "earth")));
|
||||||
|
|
||||||
|
assertThat(dao.get("life")).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldCleanUpPermissionWhenLastRepositoryOfNamespaceWasRenamed() {
|
||||||
|
when(repositoryManager.getAllNamespaces()).thenReturn(asList("universe", "rest", "highway"));
|
||||||
|
|
||||||
|
manager.cleanupDeletedNamespaces(
|
||||||
|
new RepositoryModificationEvent(
|
||||||
|
MODIFY,
|
||||||
|
new Repository("1", "git", "highway", "earth"),
|
||||||
|
new Repository("1", "git", "life", "earth")));
|
||||||
|
|
||||||
|
assertThat(dao.get("life")).isEmpty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user