]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21244 Allow deletion of local groups when instance is managed.
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>
Thu, 4 Jan 2024 13:19:16 +0000 (14:19 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 4 Jan 2024 20:02:48 +0000 (20:02 +0000)
server/sonar-webserver-common/src/main/java/org/sonar/server/common/management/ManagedInstanceChecker.java
server/sonar-webserver-common/src/test/java/com/sonar/server/common/management/ManagedInstanceCheckerTest.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/group/controller/DefaultGroupController.java
server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/group/controller/DefaultGroupControllerTest.java

index fa2f70c653968a1cc5f91bb4808e986eb041d08d..dad6f4ac6d23acdf06438ac69bd43a5b5455a953 100644 (file)
@@ -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);
index f96a624f8608263eab19a270ad7d6e8b879a04f8..83b3c73270798cb879be8acc96d3d84326337404 100644 (file)
@@ -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();
index de91c31c5d4dabbbbb51ceea6362b1e02166fb77..76dfaac6cf33d8fee4ef20d9ce7c682de6ba7fcf 100644 (file)
@@ -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();
index 9196e962a5682a5bf0c92c471d2b20048615f543..28013317f3eb04bdec23a105fc7c90e29c84fa6e 100644 (file)
@@ -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(