]> source.dussan.org Git - sonarqube.git/commitdiff
Clean-up AuthorizationDao
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 27 Sep 2016 07:41:52 +0000 (09:41 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 28 Sep 2016 14:32:36 +0000 (16:32 +0200)
sonar-db/src/main/java/org/sonar/db/user/AuthorizationDao.java
sonar-db/src/main/java/org/sonar/db/user/AuthorizationMapper.java
sonar-db/src/main/java/org/sonar/db/user/RoleDao.java
sonar-db/src/main/resources/org/sonar/db/user/AuthorizationMapper.xml
sonar-db/src/test/java/org/sonar/db/user/AuthorizationDaoTest.java
sonar-db/src/test/resources/org/sonar/db/user/AuthorizationDaoTest/keep_authorized_users_for_role_and_project_for_anonymous.xml [deleted file]
sonar-db/src/test/resources/org/sonar/db/user/AuthorizationDaoTest/keep_authorized_users_for_role_and_project_for_group.xml [deleted file]
sonar-db/src/test/resources/org/sonar/db/user/AuthorizationDaoTest/keep_authorized_users_for_role_and_project_for_user.xml [deleted file]

index 7cd1a964b7d014aead96571991554b7cab7d3df7..a8db69336d6d2fdf8bc6b82cd65d687c4e5fb33f 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.db.user;
 
-import com.google.common.collect.Sets;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -57,33 +56,6 @@ public class AuthorizationDao implements Dao {
       });
   }
 
-  /**
-   * Keep only authorized user that have the given permission on a given project.
-   * Please Note that if the permission is 'Anyone' is NOT taking into account by thie method.
-   */
-  public Collection<Long> keepAuthorizedUsersForRoleAndProject(final DbSession session, Collection<Long> userIds, String role, final long projectId) {
-    return executeLargeInputs(
-      userIds,
-      partitionOfIds -> session.getMapper(AuthorizationMapper.class).keepAuthorizedUsersForRoleAndProject(role, projectId, partitionOfIds));
-  }
-
-  public boolean isAuthorizedComponentKey(String componentKey, @Nullable Integer userId, String role) {
-    DbSession session = mybatis.openSession(false);
-    try {
-      return keepAuthorizedComponentKeys(session, componentKey, userId, role).size() == 1;
-    } finally {
-      MyBatis.closeQuietly(session);
-    }
-  }
-
-  private static List<String> keepAuthorizedComponentKeys(final DbSession session, final String componentKey, @Nullable final Integer userId, final String role) {
-    if (userId == null) {
-      return session.getMapper(AuthorizationMapper.class).keepAuthorizedComponentKeysForAnonymous(role, Sets.newHashSet(componentKey));
-    } else {
-      return session.getMapper(AuthorizationMapper.class).keepAuthorizedComponentKeysForUser(userId, role, Sets.newHashSet(componentKey));
-    }
-  }
-
   public Collection<String> selectAuthorizedRootProjectsKeys(@Nullable Integer userId, String role) {
     SqlSession session = mybatis.openSession(false);
     try {
@@ -104,7 +76,7 @@ public class AuthorizationDao implements Dao {
     }
   }
 
-  public Collection<String> selectAuthorizedRootProjectsKeys(@Nullable Integer userId, String role, SqlSession session) {
+  private static Collection<String> selectAuthorizedRootProjectsKeys(@Nullable Integer userId, String role, SqlSession session) {
     String sql;
     Map<String, Object> params = newHashMap();
     sql = "selectAuthorizedRootProjectsKeys";
@@ -114,7 +86,7 @@ public class AuthorizationDao implements Dao {
     return session.selectList(sql, params);
   }
 
-  public Collection<String> selectAuthorizedRootProjectsUuids(@Nullable Integer userId, String role, SqlSession session) {
+  private static Collection<String> selectAuthorizedRootProjectsUuids(@Nullable Integer userId, String role, SqlSession session) {
     String sql;
     Map<String, Object> params = newHashMap();
     sql = "selectAuthorizedRootProjectsUuids";
index 1f395b948d8cccd405f8f7556b232191a4c32a3e..b76ea211a9595651821f0143c7b9f9df7d8aba02 100644 (file)
@@ -29,10 +29,4 @@ public interface AuthorizationMapper {
 
   List<Long> keepAuthorizedProjectIdsForUser(@Param("userId") Integer userId, @Param("role") String role, @Param("componentIds") Collection<Long> componentIds);
 
-  List<String> keepAuthorizedComponentKeysForAnonymous(@Param("role") String role, @Param("componentKeys") Collection<String> componentKeys);
-
-  List<String> keepAuthorizedComponentKeysForUser(@Param("userId") Integer userId, @Param("role") String role, @Param("componentKeys") Collection<String> componentKeys);
-
-  List<Long> keepAuthorizedUsersForRoleAndProject(@Param("role") String role, @Param("componentId") long componentId, @Param("userIds") List<Long> userIds);
-
 }
index 0be63eac29f2aa85cee5f14feb4ad7dda5564fb4..53939c3613c790fe0c7260b78b9f7d421df638ed 100644 (file)
@@ -56,19 +56,19 @@ public class RoleDao implements Dao {
     mapper(session).deleteGroupRole(groupRole);
   }
 
-  public void deleteGroupRolesByResourceId(DbSession session, Long resourceId) {
+  private void deleteGroupRolesByResourceId(DbSession session, Long resourceId) {
     mapper(session).deleteGroupRolesByResourceId(resourceId);
   }
 
-  public void deleteUserRolesByResourceId(DbSession session, Long resourceId) {
+  private void deleteUserRolesByResourceId(DbSession session, Long resourceId) {
     mapper(session).deleteUserRolesByResourceId(resourceId);
   }
 
-  public int countResourceGroupRoles(DbSession session, Long resourceId) {
+  private int countResourceGroupRoles(DbSession session, Long resourceId) {
     return mapper(session).countResourceGroupRoles(resourceId);
   }
 
-  public int countResourceUserRoles(DbSession session, Long resourceId) {
+  private int countResourceUserRoles(DbSession session, Long resourceId) {
     return mapper(session).countResourceUserRoles(resourceId);
   }
 
index f670e71ecc87ddabbc5ce3b00d76ca199fbaafc9..f6aaec259f47f725570199bfa71eee062f92236d 100644 (file)
@@ -3,67 +3,6 @@
 
 <mapper namespace="org.sonar.db.user.AuthorizationMapper">
 
-  <select id="keepAuthorizedComponentKeysForUser" parameterType="map" resultType="string">
-    SELECT p.kee
-    FROM group_roles gr, projects p
-    WHERE
-    gr.role=#{role}
-    and (gr.group_id is null or gr.group_id in (select gu.group_id from groups_users gu where gu.user_id=#{userId}))
-    and gr.resource_id = p.id
-    and
-    <foreach collection="componentKeys" open="(" close=")" item="element" index="index" separator=" or ">
-      p.kee=#{element}
-    </foreach>
-    UNION
-    SELECT p.kee
-    FROM group_roles gr, projects root, projects p
-    WHERE
-    gr.role=#{role}
-    and (gr.group_id is null or gr.group_id in (select gu.group_id from groups_users gu where gu.user_id=#{userId}))
-    and gr.resource_id = root.id
-    and p.root_uuid = root.uuid
-    and
-    <foreach collection="componentKeys" open="(" close=")" item="element" index="index" separator=" or ">
-      p.kee=#{element}
-    </foreach>
-    UNION
-    SELECT p.kee
-    FROM user_roles ur
-    INNER JOIN projects p on p.id = ur.resource_id
-    WHERE
-    ur.role=#{role}
-    and ur.user_id=#{userId}
-    and
-    <foreach collection="componentKeys" open="(" close=")" item="element" index="index" separator=" or ">
-      p.kee=#{element}
-    </foreach>
-  </select>
-
-  <select id="keepAuthorizedComponentKeysForAnonymous" parameterType="map" resultType="string">
-    SELECT p.kee
-    FROM group_roles gr, projects p
-    WHERE
-    gr.role=#{role}
-    and gr.group_id is null
-    and gr.resource_id = p.id
-    and
-    <foreach collection="componentKeys" open="(" close=")" item="element" index="index" separator=" or ">
-      p.kee=#{element}
-    </foreach>
-    UNION
-    SELECT p.kee
-    FROM group_roles gr, projects root, projects p
-    WHERE
-    gr.role=#{role}
-    and gr.group_id is null
-    and gr.resource_id = root.id
-    and p.root_uuid = root.uuid
-    and
-    <foreach collection="componentKeys" open="(" close=")" item="element" index="index" separator=" or ">
-      p.kee=#{element}
-    </foreach>
-  </select>
-
   <select id="keepAuthorizedProjectIdsForUser" parameterType="map" resultType="long">
     SELECT gr.resource_id
     FROM group_roles gr
     </choose>
   </select>
 
-  <select id="keepAuthorizedUsersForRoleAndProject" parameterType="map" resultType="Long">
-    SELECT gu.user_id
-    FROM groups_users gu
-      INNER JOIN group_roles gr ON gr.group_id=gu.group_id
-    WHERE
-      gr.resource_id=#{componentId}
-      AND gr.role=#{role}
-      AND gu.user_id in
-        <foreach collection="userIds" open="(" close=")" item="id" separator=",">
-          #{id}
-        </foreach>
-    UNION
-    SELECT ur.user_id
-    FROM user_roles ur
-    WHERE
-      ur.resource_id=#{componentId}
-      AND ur.role=#{role}
-      AND ur.user_id IN
-      <foreach collection="userIds" open="(" close=")" item="id" separator=",">
-        #{id}
-      </foreach>
-  </select>
-
 </mapper>
index 53c43c9be3279645b4ae59be70dd0b60b6b46e9e..44e07eb3b909c0fb5162d73bddf39e0ba1b82c5a 100644 (file)
@@ -29,19 +29,17 @@ import org.sonar.db.DbTester;
 import static com.google.common.collect.Sets.newHashSet;
 import static org.assertj.core.api.Assertions.assertThat;
 
-
 public class AuthorizationDaoTest {
 
   private static final int USER = 100;
   private static final Long PROJECT_ID = 300L;
   private static final Long PROJECT_ID_WITHOUT_SNAPSHOT = 400L;
   private static final String PROJECT = "pj-w-snapshot";
-  private static final String PROJECT_WIHOUT_SNAPSHOT = "pj-wo-snapshot";
 
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
 
-  AuthorizationDao authorization = dbTester.getDbClient().authorizationDao();
+  private AuthorizationDao authorization = dbTester.getDbClient().authorizationDao();
 
   @Test
   public void user_should_be_authorized() {
@@ -61,7 +59,7 @@ public class AuthorizationDaoTest {
     assertThat(componentIds).isEmpty();
 
     assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(),
-      Collections.<Long>emptySet(),
+      Collections.emptySet(),
       USER, "admin")).isEmpty();
   }
 
@@ -75,7 +73,7 @@ public class AuthorizationDaoTest {
     assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(), newHashSet(PROJECT_ID), USER, "admin")).isEmpty();
 
     // Empty list
-    assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(), Collections.<Long>emptySet(), USER, "admin")).isEmpty();
+    assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(), Collections.emptySet(), USER, "admin")).isEmpty();
   }
 
   @Test
@@ -88,7 +86,7 @@ public class AuthorizationDaoTest {
     assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(), newHashSet(PROJECT_ID), USER, "admin")).isEmpty();
 
     // Empty list
-    assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(), Collections.<Long>emptySet(), USER, "admin")).isEmpty();
+    assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(), Collections.emptySet(), USER, "admin")).isEmpty();
   }
 
   @Test
@@ -101,38 +99,7 @@ public class AuthorizationDaoTest {
     assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(), newHashSet(PROJECT_ID), null, "admin")).isEmpty();
 
     // Empty list
-    assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(), Collections.<Long>emptySet(), null, "admin")).isEmpty();
-  }
-
-  @Test
-  public void is_authorized_component_key_for_user() {
-    dbTester.prepareDbUnit(getClass(), "keep_authorized_project_ids_for_user.xml");
-
-    assertThat(authorization.isAuthorizedComponentKey(PROJECT, USER, "user")).isTrue();
-    assertThat(authorization.isAuthorizedComponentKey(PROJECT_WIHOUT_SNAPSHOT, USER, "user")).isFalse();
-
-    // user does not have the role "admin"
-    assertThat(authorization.isAuthorizedComponentKey(PROJECT, USER, "admin")).isFalse();
-  }
-
-  @Test
-  public void is_authorized_component_key_for_group() {
-    dbTester.prepareDbUnit(getClass(), "keep_authorized_project_ids_for_group.xml");
-
-    assertThat(authorization.isAuthorizedComponentKey(PROJECT, USER, "user")).isTrue();
-    assertThat(authorization.isAuthorizedComponentKey(PROJECT_WIHOUT_SNAPSHOT, USER, "user")).isFalse();
-
-    // user does not have the role "admin"
-    assertThat(authorization.isAuthorizedComponentKey(PROJECT, USER, "admin")).isFalse();
-  }
-
-  @Test
-  public void is_authorized_component_key_for_anonymous() {
-    dbTester.prepareDbUnit(getClass(), "keep_authorized_project_ids_for_anonymous.xml");
-
-    assertThat(authorization.isAuthorizedComponentKey(PROJECT, null, "user")).isTrue();
-    assertThat(authorization.isAuthorizedComponentKey(PROJECT_WIHOUT_SNAPSHOT, null, "user")).isFalse();
-    assertThat(authorization.isAuthorizedComponentKey(PROJECT, null, "admin")).isFalse();
+    assertThat(authorization.keepAuthorizedProjectIds(dbTester.getSession(), Collections.emptySet(), null, "admin")).isEmpty();
   }
 
   @Test
@@ -282,50 +249,4 @@ public class AuthorizationDaoTest {
     assertThat(authorization.selectGlobalPermissions("anyone_user")).containsOnly("user", "profileadmin");
   }
 
-  @Test
-  public void keep_authorized_users_for_role_and_project_for_user() {
-    dbTester.prepareDbUnit(getClass(), "keep_authorized_users_for_role_and_project_for_user.xml");
-
-    assertThat(authorization.keepAuthorizedUsersForRoleAndProject(dbTester.getSession(),
-      // Only 100 and 101 has 'user' role on project
-      newHashSet(100L, 101L, 102L), "user", PROJECT_ID)).containsOnly(100L, 101L);
-
-    assertThat(authorization.keepAuthorizedUsersForRoleAndProject(dbTester.getSession(),
-      // Only 100 and 101 has 'user' role on project
-      newHashSet(100L), "user", PROJECT_ID)).containsOnly(100L);
-
-    // user does not have the role "admin"
-    assertThat(authorization.keepAuthorizedUsersForRoleAndProject(dbTester.getSession(), newHashSet(100L), "admin", PROJECT_ID)).isEmpty();
-
-    // Empty list
-    assertThat(authorization.keepAuthorizedUsersForRoleAndProject(dbTester.getSession(), Collections.<Long>emptySet(), "user", PROJECT_ID)).isEmpty();
-  }
-
-  @Test
-  public void keep_authorized_users_for_role_and_project_for_group() {
-    dbTester.prepareDbUnit(getClass(), "keep_authorized_users_for_role_and_project_for_group.xml");
-
-    assertThat(authorization.keepAuthorizedUsersForRoleAndProject(dbTester.getSession(),
-      // Only 100 and 101 has 'user' role on project
-      newHashSet(100L, 101L, 102L), "user", PROJECT_ID)).containsOnly(100L, 101L);
-
-    assertThat(authorization.keepAuthorizedUsersForRoleAndProject(dbTester.getSession(),
-      newHashSet(100L), "user", PROJECT_ID)).containsOnly(100L);
-
-    // user does not have the role "admin"
-    assertThat(authorization.keepAuthorizedUsersForRoleAndProject(dbTester.getSession(), newHashSet(100L), "admin", PROJECT_ID)).isEmpty();
-
-    // Empty list
-    assertThat(authorization.keepAuthorizedUsersForRoleAndProject(dbTester.getSession(), Collections.<Long>emptySet(), "user", PROJECT_ID)).isEmpty();
-  }
-
-  @Test
-  public void keep_authorized_users_returns_empty_list_for_role_and_project_for_anonymous() {
-    dbTester.prepareDbUnit(getClass(), "keep_authorized_users_for_role_and_project_for_anonymous.xml");
-
-    assertThat(authorization.keepAuthorizedUsersForRoleAndProject(dbTester.getSession(),
-      // Only 100 and 101 has 'user' role on project
-      newHashSet(100L, 101L, 102L), "user", PROJECT_ID)).isEmpty();
-  }
-
 }
diff --git a/sonar-db/src/test/resources/org/sonar/db/user/AuthorizationDaoTest/keep_authorized_users_for_role_and_project_for_anonymous.xml b/sonar-db/src/test/resources/org/sonar/db/user/AuthorizationDaoTest/keep_authorized_users_for_role_and_project_for_anonymous.xml
deleted file mode 100644 (file)
index 93356a3..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-<dataset>
-
-  <!-- users 100 and 101 have no direct grant access, but are in the group 200 that has the role "user" on the project 300  -->
-  <user_roles id="1"
-              user_id="100"
-              resource_id="999"
-              role="user"/>
-  <user_roles id="2"
-              user_id="101"
-              resource_id="999"
-              role="user"/>
-  <user_roles id="3"
-              user_id="102"
-              resource_id="999"
-              role="user"/>
-
-  <groups_users user_id="100"
-                group_id="200"/>
-  <groups_users user_id="101"
-                group_id="200"/>
-  <groups_users user_id="102"
-                group_id="201"/>
-
-  <group_roles id="1"
-               group_id="[null]"
-               resource_id="300"
-               role="user"/>
-  <group_roles id="2"
-               group_id="201"
-               resource_id="400"
-               role="user"/>
-
-  <projects id="300"
-            kee="pj-w-snapshot"
-            uuid="DEFG"
-            uuid_path="NOT_USED"
-            root_uuid="DEFG"
-            module_uuid="[null]"/>
-  <projects id="400"
-            kee="pj-wo-snapshot"
-            uuid="EFGH"
-            uuid_path="NOT_USED"
-            root_uuid="EFGH"
-            module_uuid="[null]"/>
-
-</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/user/AuthorizationDaoTest/keep_authorized_users_for_role_and_project_for_group.xml b/sonar-db/src/test/resources/org/sonar/db/user/AuthorizationDaoTest/keep_authorized_users_for_role_and_project_for_group.xml
deleted file mode 100644 (file)
index 3b7278e..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-<dataset>
-
-  <!-- users 100 and 101 have no direct grant access, but are in the group 200 that has the role "user" on the project 300  -->
-  <user_roles id="1"
-              user_id="100"
-              resource_id="999"
-              role="user"/>
-  <user_roles id="2"
-              user_id="101"
-              resource_id="999"
-              role="user"/>
-  <user_roles id="3"
-              user_id="102"
-              resource_id="999"
-              role="user"/>
-
-  <groups_users user_id="100"
-                group_id="200"/>
-  <groups_users user_id="101"
-                group_id="200"/>
-  <groups_users user_id="102"
-                group_id="201"/>
-
-  <group_roles id="1"
-               group_id="200"
-               resource_id="300"
-               role="user"/>
-  <group_roles id="2"
-               group_id="201"
-               resource_id="400"
-               role="user"/>
-
-  <projects id="300"
-            kee="pj-w-snapshot"
-            uuid="DEFG"
-            uuid_path="NOT_USED"
-            root_uuid="DEFG"
-            module_uuid="[null]"/>
-  <projects id="400"
-            kee="pj-wo-snapshot"
-            uuid="EFGH"
-            uuid_path="NOT_USED"
-            root_uuid="EFGH"
-            module_uuid="[null]"/>
-
-</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/user/AuthorizationDaoTest/keep_authorized_users_for_role_and_project_for_user.xml b/sonar-db/src/test/resources/org/sonar/db/user/AuthorizationDaoTest/keep_authorized_users_for_role_and_project_for_user.xml
deleted file mode 100644 (file)
index 1736578..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-<dataset>
-
-  <!-- Users 100 and 101 are 'user' on project 300 -->
-  <user_roles id="1"
-              user_id="100"
-              resource_id="300"
-              role="user"/>
-  <user_roles id="2"
-              user_id="101"
-              resource_id="300"
-              role="user"/>
-  <user_roles id="3"
-              user_id="102"
-              resource_id="300"
-              role="admin"/>
-  <user_roles id="4"
-              user_id="100"
-              resource_id="400"
-              role="user"/>
-
-  <groups_users user_id="100"
-                group_id="200"/>
-  <group_roles id="1"
-               group_id="200"
-               resource_id="400"
-               role="user"/>
-
-  <projects id="300"
-            kee="pj-w-snapshot"
-            uuid="DEFG"
-            uuid_path="NOT_USED"
-            root_uuid="DEFG"
-            module_uuid="[null]"/>
-  <projects id="400"
-            kee="pj-wo-snapshot"
-            uuid="EFGH"
-            uuid_path="NOT_USED"
-            root_uuid="EFGH"
-            module_uuid="[null]"/>
-
-</dataset>