From d8745146247c2ab1066edc887de2f2a68ea3f836 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 19 Oct 2016 14:44:03 +0200 Subject: [PATCH] SONAR-8192 update root flag of members in api/user_groups/remove --- .../server/usergroups/ws/DeleteAction.java | 15 ++-- .../usergroups/ws/DeleteActionTest.java | 89 ++++++++++++++++++- 2 files changed, 97 insertions(+), 7 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java index d94126147c0..f35473c8b57 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java @@ -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()); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/usergroups/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/usergroups/ws/DeleteActionTest.java index 35363e98488..00405a5ad60 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/usergroups/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/usergroups/ws/DeleteActionTest.java @@ -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()); } -- 2.39.5