From cf6b9cb7a653e875f693a8364642411b46d38d95 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 27 Sep 2017 18:29:22 +0200 Subject: [PATCH] SONAR-1330 Purge edit permissions when removing group --- .../qualityprofile/QProfileEditGroupsDao.java | 4 ++++ .../QProfileEditGroupsMapper.java | 2 ++ .../QProfileEditGroupsMapper.xml | 5 ++++ .../QProfileEditGroupsDaoTest.java | 23 +++++++++++++++++++ .../server/usergroups/ws/DeleteAction.java | 1 + .../usergroups/ws/DeleteActionTest.java | 17 ++++++++++++++ 6 files changed, 52 insertions(+) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsDao.java index c390e7afee0..ecb6b5355ab 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsDao.java @@ -76,6 +76,10 @@ public class QProfileEditGroupsDao implements Dao { executeLargeUpdates(qProfiles.stream().map(QProfileDto::getKee).collect(toList()), p -> mapper(dbSession).deleteByQProfiles(p)); } + public void deleteByGroup(DbSession dbSession, GroupDto group) { + mapper(dbSession).deleteByGroup(group.getId()); + } + private static QProfileEditGroupsMapper mapper(DbSession dbSession) { return dbSession.getMapper(QProfileEditGroupsMapper.class); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.java index 561b0f85085..668a0a4999c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.java @@ -40,4 +40,6 @@ public interface QProfileEditGroupsMapper { void deleteByQProfiles(@Param("qProfileUuids") Collection qProfileUuids); + void deleteByGroup(@Param("groupId") int groupId); + } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.xml index 0ce45bd4ee7..9a023f45047 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.xml @@ -109,5 +109,10 @@ where qprofile_uuid in #{qProfileUuid, jdbcType=VARCHAR} + + delete from qprofile_edit_groups + where group_id = #{groupId, jdbcType=INTEGER} + + diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditGroupsDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditGroupsDaoTest.java index 82c2f91f882..003cf80e236 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditGroupsDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditGroupsDaoTest.java @@ -277,4 +277,27 @@ public class QProfileEditGroupsDaoTest { assertThat(underTest.exists(db.getSession(), anotherProfile, group1)).isTrue(); } + @Test + public void deleteByGroup() { + OrganizationDto organization = db.organizations().insert(); + OrganizationDto anotherOrganization = db.organizations().insert(); + QProfileDto profile1 = db.qualityProfiles().insert(organization); + QProfileDto profile2 = db.qualityProfiles().insert(organization); + QProfileDto profile3 = db.qualityProfiles().insert(organization); + QProfileDto anotherProfile = db.qualityProfiles().insert(anotherOrganization); + GroupDto group1 = db.users().insertGroup(organization); + GroupDto group2 = db.users().insertGroup(organization); + db.qualityProfiles().addGroupPermission(profile1, group1); + db.qualityProfiles().addGroupPermission(profile2, group2); + db.qualityProfiles().addGroupPermission(profile3, group1); + db.qualityProfiles().addGroupPermission(anotherProfile, group1); + + underTest.deleteByGroup(db.getSession(), group1); + + assertThat(underTest.exists(db.getSession(), profile1, group1)).isFalse(); + assertThat(underTest.exists(db.getSession(), profile2, group2)).isTrue(); + assertThat(underTest.exists(db.getSession(), profile3, group1)).isFalse(); + assertThat(underTest.exists(db.getSession(), anotherProfile, group1)).isFalse(); + } + } 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 87661878417..da36bbf1086 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 @@ -72,6 +72,7 @@ public class DeleteAction implements UserGroupsWsAction { removeGroupPermissions(dbSession, group); removeFromPermissionTemplates(dbSession, group); removeGroupMembers(dbSession, group); + dbClient.qProfileEditGroupsDao().deleteByGroup(dbSession, group); dbClient.groupDao().deleteById(dbSession, group.getId()); dbSession.commit(); 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 2585a18b947..a56e800bd68 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 @@ -31,6 +31,7 @@ import org.sonar.db.component.ComponentTesting; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.permission.template.PermissionTemplateDto; import org.sonar.db.permission.template.PermissionTemplateTesting; +import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.server.exceptions.NotFoundException; @@ -184,6 +185,22 @@ public class DeleteActionTest { assertThat(db.countRowsOfTable("perm_templates_groups")).isEqualTo(0); } + @Test + public void delete_qprofile_permissions() throws Exception { + addAdminToDefaultOrganization(); + insertDefaultGroupOnDefaultOrganization(); + GroupDto group = db.users().insertGroup(); + QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); + db.qualityProfiles().addGroupPermission(profile, group); + loginAsAdminOnDefaultOrganization(); + + newRequest() + .setParam("id", group.getId().toString()) + .execute(); + + assertThat(db.countRowsOfTable("qprofile_edit_groups")).isZero(); + } + @Test public void fail_if_id_does_not_exist() throws Exception { addAdminToDefaultOrganization(); -- 2.39.5