From 9b3ef5c13da5ce90f5fd8697d89aa50dfc0d3807 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 18 Oct 2016 18:07:38 +0200 Subject: [PATCH] SONAR-8100 add UserPermissionDao#deleteByOrganization --- .../db/permission/UserPermissionDao.java | 4 ++ .../db/permission/UserPermissionMapper.java | 2 + .../db/permission/UserPermissionMapper.xml | 7 +++ .../db/permission/UserPermissionDaoTest.java | 54 ++++++++++++++++++- 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java b/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java index 91db6ad91c0..ac72fe33491 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java @@ -131,6 +131,10 @@ public class UserPermissionDao implements Dao { mapper(dbSession).deleteProjectPermissions(projectId); } + public void deleteByOrganization(DbSession dbSession, String organizationUuid) { + mapper(dbSession).deleteByOrganization(organizationUuid); + } + private static UserPermissionMapper mapper(DbSession dbSession) { return dbSession.getMapper(UserPermissionMapper.class); } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java index 916a56a6e6c..16bf5dd747e 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java @@ -56,4 +56,6 @@ public interface UserPermissionMapper { List selectGlobalPermissionsOfUser(@Param("userId") long userId, @Param("organizationUuid") String organizationUuid); List selectProjectPermissionsOfUser(@Param("userId") long userId, @Param("projectId") long projectId); + + void deleteByOrganization(@Param("organizationUuid") String organizationUuid); } diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml index fee655a91e5..86fb41937e2 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml @@ -118,4 +118,11 @@ where resource_id = #{projectId,jdbcType=BIGINT} + + + delete from + user_roles + where + organization_uuid = #{organizationUuid,jdbcType=VARCHAR} + diff --git a/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java index 2568a5cff87..eac366b339c 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java @@ -348,6 +348,57 @@ public class UserPermissionDaoTest { assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project3.getId())).isEmpty(); } + @Test + public void deleteByOrganization_does_not_fail_if_table_is_empty() { + underTest.deleteByOrganization(dbSession, "some uuid"); + dbSession.commit(); + } + + @Test + public void deleteByOrganization_does_not_fail_if_organization_has_no_user_permission() { + OrganizationDto organization = dbTester.organizations().insert(); + + underTest.deleteByOrganization(dbSession, organization.getUuid()); + dbSession.commit(); + } + + @Test + public void deleteByOrganization_deletes_all_user_permission_of_specified_organization() { + OrganizationDto organization1 = dbTester.organizations().insert(); + OrganizationDto organization2 = dbTester.organizations().insert(); + OrganizationDto organization3 = dbTester.organizations().insert(); + UserDto user1 = dbTester.users().insertUser(); + UserDto user2 = dbTester.users().insertUser(); + UserDto user3 = dbTester.users().insertUser(); + dbTester.users().insertPermissionOnUser(organization1, user1, "foo"); + dbTester.users().insertPermissionOnUser(organization1, user2, "foo"); + dbTester.users().insertPermissionOnUser(organization1, user2, "bar"); + dbTester.users().insertPermissionOnUser(organization2, user2, "foo"); + dbTester.users().insertPermissionOnUser(organization2, user3, "foo"); + dbTester.users().insertPermissionOnUser(organization2, user3, "bar"); + dbTester.users().insertPermissionOnUser(organization3, user3, "foo"); + dbTester.users().insertPermissionOnUser(organization3, user1, "foo"); + dbTester.users().insertPermissionOnUser(organization3, user1, "bar"); + + underTest.deleteByOrganization(dbSession, organization3.getUuid()); + dbSession.commit(); + verifyOrganizationUuidsInTable(organization1.getUuid(), organization2.getUuid()); + + underTest.deleteByOrganization(dbSession, organization2.getUuid()); + dbSession.commit(); + verifyOrganizationUuidsInTable(organization1.getUuid()); + + underTest.deleteByOrganization(dbSession, organization1.getUuid()); + dbSession.commit(); + verifyOrganizationUuidsInTable(); + } + + private void verifyOrganizationUuidsInTable(String... organizationUuids) { + assertThat(dbTester.select("select organization_uuid as \"organizationUuid\" from user_roles")) + .extracting((row) -> (String) row.get("organizationUuid")) + .containsOnly(organizationUuids); + } + private void expectCount(List projectIds, CountPerProjectPermission... expected) { List got = underTest.countUsersByProjectPermission(dbSession, projectIds); assertThat(got).hasSize(expected.length); @@ -406,7 +457,8 @@ public class UserPermissionDaoTest { } private void assertThatProjectPermissionDoesNotExist(UserDto user, String permission, ComponentDto project) { - assertThat(dbTester.countSql(dbSession, "select count(id) from user_roles where role='" + permission + "' and user_id=" + user.getId() + " and resource_id=" + project.getId())).isEqualTo(0); + assertThat(dbTester.countSql(dbSession, "select count(id) from user_roles where role='" + permission + "' and user_id=" + user.getId() + " and resource_id=" + project.getId())) + .isEqualTo(0); } private void assertThatProjectHasNoPermissions(ComponentDto project) { -- 2.39.5