]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8192 update root flag of members in api/user_groups/remove 1304/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 19 Oct 2016 12:44:03 +0000 (14:44 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 19 Oct 2016 12:45:13 +0000 (14:45 +0200)
server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java
server/sonar-server/src/test/java/org/sonar/server/usergroups/ws/DeleteActionTest.java

index d94126147c08ee90d8a911e4b42c051f3214da16..f35473c8b5798860b7ba753fd6ec1890490261c6 100644 (file)
@@ -78,9 +78,10 @@ public class DeleteAction implements UserGroupsWsAction {
 
       checkNotTryingToDeleteDefaultGroup(dbSession, groupId);
       checkNotTryingToDeleteLastAdminGroup(dbSession, groupId);
-      removeGroupMembers(dbSession, groupId);
       removeGroupPermissions(dbSession, groupId);
       removeFromPermissionTemplates(dbSession, groupId);
+      updateRootFlagOfMembers(dbSession, groupId);
+      removeGroupMembers(dbSession, groupId);
       dbClient.groupDao().deleteById(dbSession, groupId.getId());
 
       dbSession.commit();
@@ -110,10 +111,6 @@ public class DeleteAction implements UserGroupsWsAction {
     checkArgument(remaining > 0, "The last system admin group cannot be deleted");
   }
 
-  private void removeGroupMembers(DbSession dbSession, GroupId groupId) {
-    dbClient.userGroupDao().deleteByGroupId(dbSession, groupId.getId());
-  }
-
   private void removeGroupPermissions(DbSession dbSession, GroupId groupId) {
     dbClient.roleDao().deleteGroupRolesByGroupId(dbSession, groupId.getId());
   }
@@ -121,4 +118,12 @@ public class DeleteAction implements UserGroupsWsAction {
   private void removeFromPermissionTemplates(DbSession dbSession, GroupId groupId) {
     dbClient.permissionTemplateDao().deleteByGroup(dbSession, groupId.getId());
   }
+
+  private void updateRootFlagOfMembers(DbSession dbSession, GroupId groupId) {
+    dbClient.groupDao().updateRootFlagOfUsersInGroupFromPermissions(dbSession, groupId.getId(), defaultOrganizationProvider.get().getUuid());
+  }
+
+  private void removeGroupMembers(DbSession dbSession, GroupId groupId) {
+    dbClient.userGroupDao().deleteByGroupId(dbSession, groupId.getId());
+  }
 }
index 35363e98488ffb704862929e027184584c3bfd16..00405a5ad601bba62b148867bc43cfdf82ac609a 100644 (file)
@@ -251,7 +251,7 @@ public class DeleteActionTest {
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("The last system admin group cannot be deleted");
 
-    newRequest().setParam(PARAM_GROUP_ID, adminGroup.getId().toString()).execute();
+    executeDeleteGroupRequest(adminGroup);
   }
 
   @Test
@@ -265,11 +265,96 @@ public class DeleteActionTest {
     db.users().insertMember(adminGroup2, bigBoss);
     loginAsAdmin(org);
 
-    newRequest().setParam(PARAM_GROUP_ID, adminGroup1.getId().toString()).execute();
+    executeDeleteGroupRequest(adminGroup1);
 
     assertThat(db.users().selectGroupPermissions(adminGroup2, null)).hasSize(1);
   }
 
+  @Test
+  public void deleting_a_group_of_default_organization_with_admin_permissions_updates_root_flag_of_its_members() throws Exception {
+    UserDto rootByUserPermission1 = db.users().insertRootByUserPermission("root1");
+    UserDto rootByUserPermission2 = db.users().insertRootByUserPermission("root2");
+    UserDto rootByUserPermission3 = db.users().insertRootByUserPermission("root3");
+    GroupDto adminGroup1 = db.users().insertAdminGroup(db.getDefaultOrganization());
+    GroupDto adminGroup2 = db.users().insertAdminGroup(db.getDefaultOrganization());
+    // member of admin group 1
+    UserDto rootByGroupPermission1 = db.users().insertRootByGroupPermission(adminGroup1);
+    UserDto rootByGroupPermission2 = db.users().insertRootByGroupPermission(adminGroup1);
+    db.users().insertMembers(adminGroup1, rootByUserPermission1);
+    db.users().insertMembers(adminGroup1, rootByUserPermission3);
+    // members of admin group 2
+    UserDto rootByGroupPermission3 = db.users().insertRootByGroupPermission(adminGroup2);
+    db.users().insertMembers(adminGroup2, rootByUserPermission2);
+    db.users().insertMembers(adminGroup2, rootByUserPermission3);
+    db.users().insertMembers(adminGroup2, rootByGroupPermission2);
+    loginAsAdmin(db.getDefaultOrganization());
+
+    executeDeleteGroupRequest(adminGroup1);
+    db.rootFlag().verify(rootByUserPermission1, true);
+    db.rootFlag().verifyUnchanged(rootByUserPermission2);
+    db.rootFlag().verify(rootByUserPermission3, true);
+    db.rootFlag().verify(rootByGroupPermission1, false);
+    db.rootFlag().verify(rootByGroupPermission2, true);
+    db.rootFlag().verifyUnchanged(rootByGroupPermission3);
+
+    executeDeleteGroupRequest(adminGroup2);
+    db.rootFlag().verify(rootByUserPermission1, true);
+    db.rootFlag().verify(rootByUserPermission2, true);
+    db.rootFlag().verify(rootByUserPermission3, true);
+    db.rootFlag().verify(rootByGroupPermission1, false);
+    db.rootFlag().verify(rootByGroupPermission2, false);
+    db.rootFlag().verify(rootByGroupPermission3, false);
+  }
+
+  @Test
+  public void deleting_a_group_of_other_organization_with_admin_permissions_does_not_update_root_flag_of_its_members() throws Exception {
+    OrganizationDto otherOrganization = db.organizations().insert();
+    UserDto rootByUserPermission1 = db.users().insertRootByUserPermission("root1");
+    UserDto rootByUserPermission2 = db.users().insertRootByUserPermission("root2");
+    UserDto rootByUserPermission3 = db.users().insertRootByUserPermission("root3");
+    db.users().insertPermissionOnUser(otherOrganization, rootByUserPermission1, SYSTEM_ADMIN);
+    db.users().insertPermissionOnUser(otherOrganization, rootByUserPermission2, SYSTEM_ADMIN);
+    db.users().insertPermissionOnUser(otherOrganization, rootByUserPermission3, SYSTEM_ADMIN);
+    GroupDto adminGroup1 = db.users().insertAdminGroup(otherOrganization);
+    GroupDto adminGroup2 = db.users().insertAdminGroup(otherOrganization);
+    // member of admin group 1
+    UserDto falselyRootByGroupPermission1 = db.users().makeRoot(db.users().insertUser());
+    UserDto falselyRootByGroupPermission2 = db.users().makeRoot(db.users().insertUser());
+    db.users().insertMembers(adminGroup1, falselyRootByGroupPermission1);
+    db.users().insertMembers(adminGroup1, falselyRootByGroupPermission2);
+    db.users().insertMembers(adminGroup1, rootByUserPermission1);
+    db.users().insertMembers(adminGroup1, rootByUserPermission3);
+    // members of admin group 2
+    UserDto falselyRootByGroupPermission3 = db.users().makeRoot(db.users().insertUser());
+    db.users().insertMembers(adminGroup2, falselyRootByGroupPermission3);
+    db.users().insertMembers(adminGroup2, rootByUserPermission2);
+    db.users().insertMembers(adminGroup2, rootByUserPermission3);
+    db.users().insertMembers(adminGroup2, falselyRootByGroupPermission2);
+    loginAsAdmin(otherOrganization);
+
+    executeDeleteGroupRequest(adminGroup1);
+    db.rootFlag().verify(rootByUserPermission1, true);
+    db.rootFlag().verifyUnchanged(rootByUserPermission2);
+    db.rootFlag().verify(rootByUserPermission3, true);
+    db.rootFlag().verify(falselyRootByGroupPermission1, false);
+    db.rootFlag().verify(falselyRootByGroupPermission2, false);
+    db.rootFlag().verifyUnchanged(falselyRootByGroupPermission3);
+
+    executeDeleteGroupRequest(adminGroup2);
+    db.rootFlag().verify(rootByUserPermission1, true);
+    db.rootFlag().verify(rootByUserPermission2, true);
+    db.rootFlag().verify(rootByUserPermission3, true);
+    db.rootFlag().verify(falselyRootByGroupPermission1, false);
+    db.rootFlag().verify(falselyRootByGroupPermission2, false);
+    db.rootFlag().verify(falselyRootByGroupPermission3, false);
+  }
+
+  private WsTester.Result executeDeleteGroupRequest(GroupDto adminGroup1) throws Exception {
+    return newRequest()
+      .setParam(PARAM_GROUP_ID, adminGroup1.getId().toString())
+      .execute();
+  }
+
   private void addAdminToDefaultOrganization() {
     addAdmin(db.getDefaultOrganization());
   }