aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-10-18 18:07:38 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-10-20 17:17:53 +0200
commit9b3ef5c13da5ce90f5fd8697d89aa50dfc0d3807 (patch)
tree4bc70e369506a785961739785586ce9725265757 /sonar-db
parentf06a13c3a2fd9b34680637d1f5ea1535a1c60b03 (diff)
downloadsonarqube-9b3ef5c13da5ce90f5fd8697d89aa50dfc0d3807.tar.gz
sonarqube-9b3ef5c13da5ce90f5fd8697d89aa50dfc0d3807.zip
SONAR-8100 add UserPermissionDao#deleteByOrganization
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java4
-rw-r--r--sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java2
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml7
-rw-r--r--sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java54
4 files changed, 66 insertions, 1 deletions
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<String> selectGlobalPermissionsOfUser(@Param("userId") long userId, @Param("organizationUuid") String organizationUuid);
List<String> 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>
+
+ <delete id="deleteByOrganization" parameterType="String">
+ delete from
+ user_roles
+ where
+ organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
+ </delete>
</mapper>
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<Long> projectIds, CountPerProjectPermission... expected) {
List<CountPerProjectPermission> 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) {