]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9105 add GroupPermissionDao#deleteByRootComponentIdAndGroupId
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 14 Apr 2017 07:57:09 +0000 (09:57 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 27 Apr 2017 12:25:54 +0000 (14:25 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/permission/GroupPermissionDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java

index 4781b47378739c9843eafafd58bce71bd51e4bcb..97e779624999e2e82504c839d2c5510e33605728 100644 (file)
@@ -138,6 +138,14 @@ public class GroupPermissionDao implements Dao {
     mapper(dbSession).deleteByRootComponentId(rootComponentId);
   }
 
+  /**
+   * Delete all permissions of the specified group (group "AnyOne" if {@code groupId} is {@code null}) for the specified
+   * component.
+   */
+  public int deleteByRootComponentIdAndGroupId(DbSession dbSession, long rootComponentId, @Nullable Integer groupId) {
+    return mapper(dbSession).deleteByRootComponentIdAndGroupId(rootComponentId, groupId);
+  }
+
   /**
    * Delete a single permission. It can be:
    * <ul>
index ec5288e56c867907fabf7eb83b9e5e9e5680163a..00e58e1ab2045cb0d3be43d4319fe4b553f48c17 100644 (file)
@@ -39,8 +39,6 @@ public interface GroupPermissionMapper {
 
   void insert(GroupPermissionDto dto);
 
-  void deleteByRootComponentId(@Param("rootComponentId") long componentId);
-
   void delete(@Param("permission") String permission, @Param("organizationUuid") String organizationUuid,
     @Nullable @Param("groupId") Integer groupId, @Nullable @Param("rootComponentId") Long rootComponentId);
 
@@ -55,4 +53,7 @@ public interface GroupPermissionMapper {
 
   void deleteByOrganization(@Param("organizationUuid") String organizationUuid);
 
+  void deleteByRootComponentId(@Param("rootComponentId") long componentId);
+
+  int deleteByRootComponentIdAndGroupId(@Param("rootComponentId") long rootComponentId, @Nullable @Param("groupId") Integer groupId);
 }
index b2ab5ef4a9ebd04d109b5932b43459615a184882..ccd42f9360c906190416ee85b89c0eb5e8870c05 100644 (file)
     where resource_id=#{rootComponentId,jdbcType=BIGINT}
   </delete>
 
+  <delete id="deleteByRootComponentIdAndGroupId">
+    delete from
+      group_roles
+    where
+      resource_id=#{rootComponentId,jdbcType=BIGINT}
+      <choose>
+        <when test="groupId != null">
+          and group_id = #{groupId,jdbcType=INTEGER}
+        </when>
+        <otherwise>
+          and group_id is null
+        </otherwise>
+      </choose>
+  </delete>
+
   <delete id="delete" parameterType="map">
     delete from group_roles
     where
index dea92218fe0405462419ae0cb7957c5e63b6e074..c2d43e87cc404d710c097d731443ded460fa0c93 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.db.permission;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Random;
 import java.util.stream.IntStream;
 import org.junit.Before;
 import org.junit.Rule;
@@ -660,6 +661,172 @@ public class GroupPermissionDaoTest {
     verifyOrganizationUuidsInTable();
   }
 
+  @Test
+  public void deleteByRootComponentIdAndGroupId_deletes_all_permissions_of_group_AnyOne_of_specified_component_if_groupId_is_null() {
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto project = db.components().insertPublicProject(organization);
+    GroupDto group = db.users().insertGroup(organization);
+    db.users().insertProjectPermissionOnAnyone("p1", project);
+    db.users().insertProjectPermissionOnGroup(group, "p2", project);
+    db.users().insertPermissionOnAnyone(organization, "p3");
+    db.users().insertPermissionOnGroup(group, "p4");
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), null, project.getId()))
+      .containsOnly("p1");
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group.getId(), project.getId()))
+      .containsOnly("p2");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), null))
+      .containsOnly("p3");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group.getId()))
+      .containsOnly("p4");
+
+    int deletedCount = underTest.deleteByRootComponentIdAndGroupId(dbSession, project.getId(), null);
+
+    assertThat(deletedCount).isEqualTo(1);
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), null, project.getId()))
+      .isEmpty();
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group.getId(), project.getId()))
+      .containsOnly("p2");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), null))
+      .containsOnly("p3");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group.getId()))
+      .containsOnly("p4");
+  }
+
+  @Test
+  public void deleteByRootComponentIdAndGroupId_deletes_all_permissions_of_specified_group_of_specified_component_if_groupId_is_non_null() {
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto project = db.components().insertPublicProject(organization);
+    GroupDto group1 = db.users().insertGroup(organization);
+    GroupDto group2 = db.users().insertGroup(organization);
+    db.users().insertProjectPermissionOnAnyone("p1", project);
+    db.users().insertProjectPermissionOnGroup(group1, "p2", project);
+    db.users().insertProjectPermissionOnGroup(group2, "p3", project);
+    db.users().insertProjectPermissionOnGroup(group2, "p4", project);
+    db.users().insertPermissionOnAnyone(organization, "p5");
+    db.users().insertPermissionOnGroup(group1, "p6");
+    db.users().insertPermissionOnGroup(group2, "p7");
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), null, project.getId()))
+      .containsOnly("p1");
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId(), project.getId()))
+      .containsOnly("p2");
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group2.getId(), project.getId()))
+      .containsOnly("p3", "p4");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), null))
+      .containsOnly("p5");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId()))
+      .containsOnly("p6");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group2.getId()))
+      .containsOnly("p7");
+
+    int deletedCount = underTest.deleteByRootComponentIdAndGroupId(dbSession, project.getId(), group1.getId());
+
+    assertThat(deletedCount).isEqualTo(1);
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), null, project.getId()))
+      .containsOnly("p1");
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId(), project.getId()))
+      .isEmpty();
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group2.getId(), project.getId()))
+      .containsOnly("p3", "p4");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId()))
+      .containsOnly("p6");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group2.getId()))
+      .containsOnly("p7");
+
+    deletedCount = underTest.deleteByRootComponentIdAndGroupId(dbSession, project.getId(), group2.getId());
+
+    assertThat(deletedCount).isEqualTo(2);
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), null, project.getId()))
+      .containsOnly("p1");
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId(), project.getId()))
+      .isEmpty();
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group2.getId(), project.getId()))
+      .isEmpty();
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId()))
+      .containsOnly("p6");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group2.getId()))
+      .containsOnly("p7");
+  }
+
+  @Test
+  public void deleteByRootComponentIdAndGroupId_has_no_effect_if_component_does_not_exist() {
+    OrganizationDto organization = db.organizations().insert();
+    GroupDto group = db.users().insertGroup(organization);
+
+    assertThat(underTest.deleteByRootComponentIdAndGroupId(dbSession, 1234L, null)).isEqualTo(0);
+    assertThat(underTest.deleteByRootComponentIdAndGroupId(dbSession, 1234L, group.getId())).isEqualTo(0);
+  }
+
+  @Test
+  public void deleteByRootComponentIdAndGroupId_has_no_effect_if_component_has_no_group_permission_at_all() {
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto project = randomPublicOrPrivateProject(organization);
+    GroupDto group = db.users().insertGroup(organization);
+
+    assertThat(underTest.deleteByRootComponentIdAndGroupId(dbSession, project.getId(), null)).isEqualTo(0);
+    assertThat(underTest.deleteByRootComponentIdAndGroupId(dbSession, project.getId(), group.getId())).isEqualTo(0);
+  }
+
+  @Test
+  public void deleteByRootComponentIdAndGroupId_has_no_effect_if_group_does_not_exist() {
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto project = randomPublicOrPrivateProject(organization);
+
+    assertThat(underTest.deleteByRootComponentIdAndGroupId(dbSession, project.getId(), 5678)).isEqualTo(0);
+  }
+
+  @Test
+  public void deleteByRootComponentIdAndGroupId_has_no_effect_if_component_has_no_group_permission_for_group_AnyOne() {
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto project = db.components().insertPrivateProject(organization);
+    GroupDto group1 = db.users().insertGroup(organization);
+    db.users().insertProjectPermissionOnGroup(group1, "p1", project);
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), null, project.getId()))
+      .isEmpty();
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId(), project.getId()))
+      .containsOnly("p1");
+    db.users().insertPermissionOnAnyone(organization, "p2");
+    db.users().insertPermissionOnGroup(group1, "p3");
+
+    int deletedCount = underTest.deleteByRootComponentIdAndGroupId(dbSession, project.getId(), null);
+
+    assertThat(deletedCount).isEqualTo(0);
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), null, project.getId()))
+      .isEmpty();
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId(), project.getId()))
+      .containsOnly("p1");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), null))
+      .containsOnly("p2");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId()))
+      .containsOnly("p3");
+  }
+
+  @Test
+  public void deleteByRootComponentIdAndGroupId_has_no_effect_if_component_has_no_group_permission_for_specified_group() {
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto project = db.components().insertPrivateProject(organization);
+    GroupDto group1 = db.users().insertGroup(organization);
+    GroupDto group2 = db.users().insertGroup(organization);
+    db.users().insertProjectPermissionOnGroup(group1, "p1", project);
+    db.users().insertPermissionOnAnyone(organization, "p2");
+    db.users().insertPermissionOnGroup(group1, "p3");
+
+    int deletedCount = underTest.deleteByRootComponentIdAndGroupId(dbSession, project.getId(), group2.getId());
+
+    assertThat(deletedCount).isEqualTo(0);
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId(), project.getId()))
+      .containsOnly("p1");
+    assertThat(underTest.selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group2.getId(), project.getId()))
+      .isEmpty();
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), null))
+      .containsOnly("p2");
+    assertThat(underTest.selectGlobalPermissionsOfGroup(dbSession, organization.getUuid(), group1.getId()))
+      .containsOnly("p3");
+  }
+
+  private ComponentDto randomPublicOrPrivateProject(OrganizationDto organization) {
+    return new Random().nextBoolean() ? db.components().insertPublicProject(organization) : db.components().insertPrivateProject(organization);
+  }
+
   private PermissionQuery.Builder newQuery() {
     return PermissionQuery.builder().setOrganizationUuid(db.getDefaultOrganization().getUuid());
   }