From 5efcc863cf8f76e37218713945f791e6963754ad Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Thu, 4 Jan 2024 14:19:16 +0100 Subject: [PATCH] SONAR-21244 Allow deletion of local groups when instance is managed. --- .../management/ManagedInstanceChecker.java | 4 ++++ .../management/ManagedInstanceCheckerTest.java | 17 +++++++++++++++++ .../controller/DefaultGroupController.java | 7 ++++++- .../controller/DefaultGroupControllerTest.java | 4 ++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/management/ManagedInstanceChecker.java b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/management/ManagedInstanceChecker.java index fa2f70c6539..dad6f4ac6d2 100644 --- a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/management/ManagedInstanceChecker.java +++ b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/management/ManagedInstanceChecker.java @@ -49,6 +49,10 @@ public class ManagedInstanceChecker { BadRequestException.checkRequest(!managedInstanceService.isUserManaged(dbSession, userUuid), INSTANCE_EXCEPTION_MESSAGE); } + public void throwIfGroupIsManaged(DbSession dbSession, String groupUuid) { + BadRequestException.checkRequest(!managedInstanceService.isGroupManaged(dbSession, groupUuid), INSTANCE_EXCEPTION_MESSAGE); + } + public void throwIfUserAndProjectAreManaged(DbSession dbSession, String userUuid, String projectUuid) { boolean isUserManaged = managedInstanceService.isUserManaged(dbSession, userUuid); boolean isProjectManaged = managedProjectService.isProjectManaged(dbSession, projectUuid); diff --git a/server/sonar-webserver-common/src/test/java/com/sonar/server/common/management/ManagedInstanceCheckerTest.java b/server/sonar-webserver-common/src/test/java/com/sonar/server/common/management/ManagedInstanceCheckerTest.java index f96a624f860..83b3c732707 100644 --- a/server/sonar-webserver-common/src/test/java/com/sonar/server/common/management/ManagedInstanceCheckerTest.java +++ b/server/sonar-webserver-common/src/test/java/com/sonar/server/common/management/ManagedInstanceCheckerTest.java @@ -103,6 +103,23 @@ public class ManagedInstanceCheckerTest { assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserIsManaged(dbSession, userDto.getUuid())); } + @Test + public void throwIfGroupIsManaged_whenGroupIsManaged_shouldThrow() { + GroupDto groupDto = mockManagedGroup(); + + String groupUuid = groupDto.getUuid(); + assertThatThrownBy(() -> managedInstanceChecker.throwIfGroupIsManaged(dbSession, groupUuid)) + .isInstanceOf(BadRequestException.class) + .hasMessage(INSTANCE_EXCEPTION_MESSAGE); + } + + @Test + public void throwIfGroupIsManaged_whenGroupIsNotManaged_shouldNotThrow() { + GroupDto groupDto = mockNotManagedGroup(); + + assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfGroupIsManaged(dbSession, groupDto.getUuid())); + } + @Test public void throwIfUserAndProjectAreManaged_whenUserAndProjectAreManaged_shouldThrow() { ProjectDto projectDto = mockManagedProject(); diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/group/controller/DefaultGroupController.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/group/controller/DefaultGroupController.java index de91c31c5d4..76dfaac6cf3 100644 --- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/group/controller/DefaultGroupController.java +++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/group/controller/DefaultGroupController.java @@ -80,8 +80,8 @@ public class DefaultGroupController implements GroupController { @Override public void deleteGroup(String id) { - throwIfNotAllowedToModifyGroups(); try (DbSession session = dbClient.openSession(false)) { + throwIfNotAllowedToDeleteGroup(id, session); GroupInformation group = findGroupInformationOrThrow(id, session); groupService.delete(session, group.groupDto()); session.commit(); @@ -108,6 +108,11 @@ public class DefaultGroupController implements GroupController { .orElseThrow(() -> new NotFoundException(String.format(GROUP_NOT_FOUND_MESSAGE, id))); } + private void throwIfNotAllowedToDeleteGroup(String id, DbSession session) { + userSession.checkIsSystemAdministrator(); + managedInstanceChecker.throwIfGroupIsManaged(session, id); + } + @Override public GroupRestResponse create(GroupCreateRestRequest request) { throwIfNotAllowedToModifyGroups(); diff --git a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/group/controller/DefaultGroupControllerTest.java b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/group/controller/DefaultGroupControllerTest.java index 9196e962a56..28013317f3e 100644 --- a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/group/controller/DefaultGroupControllerTest.java +++ b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/group/controller/DefaultGroupControllerTest.java @@ -174,9 +174,9 @@ public class DefaultGroupControllerTest { } @Test - public void deleteGroup_whenInstanceIsManaged_shouldReturnException() throws Exception { + public void deleteGroup_whenInstanceAndGroupAreManaged_shouldReturnException() throws Exception { userSession.logIn().setSystemAdministrator(); - doThrow(BadRequestException.create("the instance is managed")).when(managedInstanceChecker).throwIfInstanceIsManaged(); + doThrow(BadRequestException.create("the instance is managed")).when(managedInstanceChecker).throwIfGroupIsManaged(any(), eq(GROUP_UUID)); mockMvc.perform( delete(GROUPS_ENDPOINT + "/" + GROUP_UUID)) .andExpectAll( -- 2.39.5