diff options
author | Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> | 2024-01-04 14:19:16 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-01-04 20:02:48 +0000 |
commit | 5efcc863cf8f76e37218713945f791e6963754ad (patch) | |
tree | cca2b36532b0b6082fdac37c82a89fa927fd9ccf /server | |
parent | 2cf35e5bd9752e86cd34457e4bb58548e036d709 (diff) | |
download | sonarqube-5efcc863cf8f76e37218713945f791e6963754ad.tar.gz sonarqube-5efcc863cf8f76e37218713945f791e6963754ad.zip |
SONAR-21244 Allow deletion of local groups when instance is managed.
Diffstat (limited to 'server')
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 @@ -104,6 +104,23 @@ public class ManagedInstanceCheckerTest { } @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(); UserDto userDto = mockManagedUser(); 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( |