]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9105 add selectUserIdsWithPermissionOnProjectBut
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 21 Apr 2017 16:01:08 +0000 (18:01 +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/UserPermissionDaoTest.java

index 31f276e15956bf7ab5b774e6140a9d942b148a46..8ffeb772d2908d009d5064de822d508655f206fd 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.db.permission;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Set;
 import javax.annotation.Nullable;
 import org.apache.ibatis.session.RowBounds;
 import org.sonar.core.util.stream.MoreCollectors;
@@ -99,6 +100,10 @@ public class UserPermissionDao implements Dao {
     return mapper(dbSession).selectProjectPermissionsOfUser(userId, projectId);
   }
 
+  public Set<Integer> selectUserIdsWithPermissionOnProjectBut(DbSession session, long projectId, String permission) {
+    return mapper(session).selectUserIdsWithPermissionOnProjectBut(projectId, permission);
+  }
+
   public void insert(DbSession dbSession, UserPermissionDto dto) {
     ensureComponentPermissionConsistency(dbSession, dto);
     mapper(dbSession).insert(dto);
index 4007e221b0cfe3293fd0541608c1737c51e858f7..5c78b79d38a389941fb4b800af28d9c7a83ebe1a 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.db.permission;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Set;
 import javax.annotation.Nullable;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.session.RowBounds;
@@ -45,6 +46,11 @@ public interface UserPermissionMapper {
    */
   List<CountPerProjectPermission> countUsersByProjectPermission(@Param("projectIds") List<Long> projectIds);
 
+  /**
+   * select id of users with at least one permission on the specified project but which do not have the specified permission.
+   */
+  Set<Integer> selectUserIdsWithPermissionOnProjectBut(@Param("projectId") long projectId, @Param("permission") String permission);
+
   void insert(UserPermissionDto dto);
 
   void deleteGlobalPermission(@Param("userId") int userId, @Param("permission") String permission,
index 7e9e3d4b68bf7b3dce0032eab9c4a002c8c0d832..bfe02d6267db1dd77f988e8ef047eadf42db3783 100644 (file)
     group by ur.resource_id, ur.role
   </select>
 
+  <select id="selectUserIdsWithPermissionOnProjectBut" resultType="Integer">
+    select
+      distinct ur1.user_id
+    from
+      user_roles ur1
+    where
+      ur1.resource_id = #{projectId,jdbcType=BIGINT}
+      and role &lt;&gt; #{permission,jdbcType=VARCHAR}
+      and not exists (
+        select
+          1
+        from
+          user_roles ur2
+        where
+          ur2.resource_id = ur1.resource_id
+          and ur2.user_id = ur1.user_id
+          and role = #{permission,jdbcType=VARCHAR}
+      )
+  </select>
+
   <insert id="insert" parameterType="org.sonar.db.permission.UserPermissionDto" useGeneratedKeys="false">
     insert into user_roles (
     organization_uuid,
index 64762e5eed4c32a7ef2e693f6f609591e4825797..fdffb764833c20ffe26b29d225f18e545b513658 100644 (file)
@@ -367,6 +367,50 @@ public class UserPermissionDaoTest {
     assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project3.getId())).isEmpty();
   }
 
+  @Test
+  public void selectGroupIdsWithPermissionOnProjectBut_returns_empty_if_project_does_not_exist() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto project = randomPublicOrPrivateProject(organization);
+    UserDto user = dbTester.users().insertUser();
+    dbTester.users().insertProjectPermissionOnUser(user, "foo", project);
+
+    assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, 1234, UserRole.USER))
+      .isEmpty();
+  }
+
+  @Test
+  public void selectGroupIdsWithPermissionOnProjectBut_returns_only_users_of_projects_which_do_not_have_permission() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto project = randomPublicOrPrivateProject(organization);
+    UserDto user1 = dbTester.users().insertUser();
+    UserDto user2 = dbTester.users().insertUser();
+    dbTester.users().insertProjectPermissionOnUser(user1, "p1", project);
+    dbTester.users().insertProjectPermissionOnUser(user2, "p2", project);
+
+    assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.getId(), "p2"))
+      .containsOnly(user1.getId());
+    assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.getId(), "p1"))
+      .containsOnly(user2.getId());
+    assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.getId(), "p3"))
+      .containsOnly(user1.getId(), user2.getId());
+  }
+
+  @Test
+  public void selectGroupIdsWithPermissionOnProjectBut_does_not_return_groups_which_have_no_permission_at_all_on_specified_project() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto project = randomPublicOrPrivateProject(organization);
+    UserDto user1 = dbTester.users().insertUser();
+    UserDto user2 = dbTester.users().insertUser();
+    UserDto user3 = dbTester.users().insertUser();
+    dbTester.users().insertProjectPermissionOnUser(user1, "p1", project);
+    dbTester.users().insertProjectPermissionOnUser(user2, "p2", project);
+
+    assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.getId(), "p2"))
+      .containsOnly(user1.getId());
+    assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.getId(), "p1"))
+      .containsOnly(user2.getId());
+  }
+
   @Test
   public void deleteByOrganization_does_not_fail_if_table_is_empty() {
     underTest.deleteByOrganization(dbSession, "some uuid");