]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8100 add UserPermissionDao#deleteByOrganization
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 18 Oct 2016 16:07:38 +0000 (18:07 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 20 Oct 2016 15:17:53 +0000 (17:17 +0200)
sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java
sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java
sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml
sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java

index 91db6ad91c0ab0689993d05fbe779a5067c8e0d0..ac72fe33491f9e47ea446906a862d2eeb0647401 100644 (file)
@@ -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);
   }
index 916a56a6e6c6f43d09e0b5c94a7ffefe9c3cf1a3..16bf5dd747e1de15c7e500839560d3b9f0ef3f33 100644 (file)
@@ -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);
 }
index fee655a91e5006cd1b29dc96a8ffd8aa78839dff..86fb41937e2a260135d3322fba4409d74c737f4a 100644 (file)
     where
     resource_id = #{projectId,jdbcType=BIGINT}
   </delete>
+
+  <delete id="deleteByOrganization" parameterType="String">
+    delete from
+      user_roles
+    where
+      organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
+  </delete>
 </mapper>
index 2568a5cff87db73712e7a6b536903cd2bafc9be2..eac366b339c709c620d9df9b1111cba5a4e3b7f0 100644 (file)
@@ -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) {