]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9105 add UserPermissionDao#deleteProjectPermissionOfAnyUser
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 14 Apr 2017 09:49:24 +0000 (11:49 +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/UserPermissionDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java

index 96a54261b58aefe2ed814fcce72658e1cf7a01de..31f276e15956bf7ab5b774e6140a9d942b148a46 100644 (file)
@@ -136,6 +136,13 @@ public class UserPermissionDao implements Dao {
     mapper(dbSession).deleteProjectPermissions(projectId);
   }
 
+  /**
+   * Deletes the specified permission on the specified project for any user.
+   */
+  public int deleteProjectPermissionOfAnyUser(DbSession dbSession, long projectId, String permission) {
+    return mapper(dbSession).deleteProjectPermissionOfAnyUser(projectId, permission);
+  }
+
   public void deleteByOrganization(DbSession dbSession, String organizationUuid) {
     mapper(dbSession).deleteByOrganization(organizationUuid);
   }
index 31b724c97b1e67c711ab00d8d7b3e26b7ce9e394..4007e221b0cfe3293fd0541608c1737c51e858f7 100644 (file)
@@ -55,6 +55,8 @@ public interface UserPermissionMapper {
 
   void deleteProjectPermissions(@Param("projectId") long projectId);
 
+  int deleteProjectPermissionOfAnyUser(@Param("projectId") long projectId, @Param("permission") String permission);
+
   List<String> selectGlobalPermissionsOfUser(@Param("userId") int userId, @Param("organizationUuid") String organizationUuid);
 
   List<String> selectProjectPermissionsOfUser(@Param("userId") int userId, @Param("projectId") long projectId);
index 57f826b73173c020cab9b78c797799ecb4b9b32a..7e9e3d4b68bf7b3dce0032eab9c4a002c8c0d832 100644 (file)
     resource_id = #{projectId,jdbcType=BIGINT}
   </delete>
 
+  <delete id="deleteProjectPermissionOfAnyUser" parameterType="map">
+    delete from
+      user_roles
+    where
+      resource_id = #{projectId,jdbcType=BIGINT}
+      and role = #{permission,jdbcType=VARCHAR}
+  </delete>
+
   <delete id="deleteByOrganization" parameterType="String">
     delete from
       user_roles
index 29dac03f9898e13785f1630860ce6eb2c0698099..1097e8190c54a2a65a0919a63dabfcb99ec689c3 100644 (file)
@@ -878,14 +878,14 @@ public class GroupPermissionDaoTest {
   @Test
   public void deleteByRootComponentIdAndPermission_has_no_effect_if_component_does_not_exist() {
     OrganizationDto organization = db.organizations().insert();
-    ComponentDto project = randomPublicOrPrivateProject(organization);
+    ComponentDto project = db.components().insertPublicProject(organization);
     GroupDto group = db.users().insertGroup(organization);
     db.users().insertPermissionOnAnyone(organization, "p1");
     db.users().insertPermissionOnGroup(group, "p1");
     db.users().insertProjectPermissionOnGroup(group, "p1", project);
     db.users().insertProjectPermissionOnAnyone("p1", project);
 
-    assertThat(underTest.deleteByRootComponentIdAndPermission(dbSession, project.getId(), "p2")).isEqualTo(0);
+    assertThat(underTest.deleteByRootComponentIdAndPermission(dbSession, 1324, "p1")).isEqualTo(0);
 
     assertThat(getGlobalPermissionsForAnyone(organization)).containsOnly("p1");
     assertThat(getGlobalPermissionsForGroup(group)).containsOnly("p1");
index e02068b90b45e61223a0bd1a971e24ba0c2f821a..64762e5eed4c32a7ef2e693f6f609591e4825797 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Random;
 import javax.annotation.Nullable;
 import org.junit.Before;
 import org.junit.Rule;
@@ -46,6 +47,7 @@ import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
+import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS;
 import static org.sonar.db.permission.OrganizationPermission.SCAN;
 import static org.sonar.db.user.UserTesting.newUserDto;
 
@@ -457,6 +459,81 @@ public class UserPermissionDaoTest {
       .containsOnly(tuple(user2.getId().longValue(), null, SCAN.getKey()), tuple(user2.getId().longValue(), project.getId(), ADMINISTER_QUALITY_GATES.getKey()));
   }
 
+  @Test
+  public void deleteProjectPermissionOfAnyUser_has_no_effect_if_specified_component_does_not_exist() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    dbTester.users().insertPermissionOnUser(organization, user1, SCAN);
+
+    int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, 124L, SCAN.getKey());
+
+    assertThat(deletedCount).isEqualTo(0);
+    assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey());
+  }
+
+  @Test
+  public void deleteProjectPermissionOfAnyUser_has_no_effect_if_specified_component_has_no_permission_at_all() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    dbTester.users().insertPermissionOnUser(organization, user1, SCAN);
+    ComponentDto project = randomPublicOrPrivateProject(organization);
+
+    int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.getId(), SCAN.getKey());
+
+    assertThat(deletedCount).isEqualTo(0);
+    assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey());
+  }
+
+  @Test
+  public void deleteProjectPermissionOfAnyUser_has_no_effect_if_specified_component_does_not_have_specified_permission() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    dbTester.users().insertPermissionOnUser(organization, user1, SCAN);
+    ComponentDto project = randomPublicOrPrivateProject(organization);
+    dbTester.users().insertProjectPermissionOnUser(user1, SCAN.getKey(), project);
+
+    int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.getId(), "p1");
+
+    assertThat(deletedCount).isEqualTo(0);
+    assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey());
+    assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project.getId())).containsOnly(SCAN.getKey());
+  }
+
+  @Test
+  public void deleteProjectPermissionOfAnyUser_deletes_specified_permission_for_any_user_on_the_specified_component() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    dbTester.users().insertPermissionOnUser(organization, user1, SCAN);
+    dbTester.users().insertPermissionOnUser(organization, user2, SCAN);
+    ComponentDto project1 = randomPublicOrPrivateProject(organization);
+    ComponentDto project2 = randomPublicOrPrivateProject(organization);
+    dbTester.users().insertProjectPermissionOnUser(user1, SCAN.getKey(), project1);
+    dbTester.users().insertProjectPermissionOnUser(user2, SCAN.getKey(), project1);
+    dbTester.users().insertProjectPermissionOnUser(user1, SCAN.getKey(), project2);
+    dbTester.users().insertProjectPermissionOnUser(user2, SCAN.getKey(), project2);
+    dbTester.users().insertProjectPermissionOnUser(user2, PROVISION_PROJECTS.getKey(), project2);
+
+    int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project1.getId(), SCAN.getKey());
+
+    assertThat(deletedCount).isEqualTo(2);
+    assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey());
+    assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user2.getId(), organization.getUuid())).containsOnly(SCAN.getKey());
+    assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project1.getId())).isEmpty();
+    assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getId(), project1.getId())).isEmpty();
+    assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project2.getId())).containsOnly(SCAN.getKey());
+    assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getId(), project2.getId())).containsOnly(SCAN.getKey(), PROVISION_PROJECTS.getKey());
+
+    deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project2.getId(), SCAN.getKey());
+
+    assertThat(deletedCount).isEqualTo(2);
+    assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey());
+    assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user2.getId(), organization.getUuid())).containsOnly(SCAN.getKey());
+    assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project1.getId())).isEmpty();
+    assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getId(), project1.getId())).isEmpty();
+    assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project2.getId())).containsOnly();
+    assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getId(), project2.getId())).containsOnly(PROVISION_PROJECTS.getKey());
+  }
+
+  private ComponentDto randomPublicOrPrivateProject(OrganizationDto organization) {
+    return new Random().nextBoolean() ? dbTester.components().insertPrivateProject(organization) : dbTester.components().insertPublicProject(organization);
+  }
+
   private void verifyOrganizationUuidsInTable(String... organizationUuids) {
     assertThat(dbTester.select("select organization_uuid as \"organizationUuid\" from user_roles"))
       .extracting((row) -> (String) row.get("organizationUuid"))