]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9140 drop deprecated selectAuthorizedRootProjectsUuids
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 18 Apr 2017 10:03:08 +0000 (12:03 +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/AuthorizationDao.java
server/sonar-db-dao/src/main/resources/org/sonar/db/permission/AuthorizationMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/permission/AuthorizationDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/favorite/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/notification/ws/ListAction.java

index 0afb2d535cedd1b3fe38ae7a679fcf69f0078e6d..792556c951d09c85fc87b7a0fc9e65ba9b333ee5 100644 (file)
@@ -20,8 +20,6 @@
 package org.sonar.db.permission;
 
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.Set;
 import javax.annotation.Nullable;
 import org.sonar.db.Dao;
@@ -133,20 +131,6 @@ public class AuthorizationDao implements Dao {
       });
   }
 
-  /**
-   * @deprecated it loads too many results and there's no functional need.
-   */
-  @Deprecated
-  public Collection<String> selectAuthorizedRootProjectsUuids(DbSession dbSession, @Nullable Integer userId, String role) {
-    String sql;
-    Map<String, Object> params = new HashMap<>(2);
-    sql = "selectAuthorizedRootProjectsUuids";
-    params.put(USER_ID_PARAM, userId);
-    params.put("role", role);
-
-    return dbSession.selectList(sql, params);
-  }
-
   /**
    * 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.
index 5757414332ec6d7dfaa1c732ee875ae6f6331815..e8f88ab6b0283824e82732ddb7443096474af42e 100644 (file)
     </foreach>
   </select>
 
-  <select id="selectAuthorizedRootProjectsUuids" parameterType="map" resultType="string">
-    <choose>
-      <when test="userId != null">
-        SELECT p.uuid as root_project_uuid
-        FROM group_roles gr
-        INNER JOIN projects p on p.id = gr.resource_id AND p.module_uuid IS NULL
-        where
-        gr.role=#{role,jdbcType=VARCHAR}
-        and (gr.group_id is null or exists (select 1 from groups_users gu where gu.user_id = #{userId,jdbcType=INTEGER} and gu.group_id = gr.group_id))
-        UNION
-        SELECT p.uuid as root_project_uuid
-        FROM user_roles ur
-        INNER JOIN projects p on p.id = ur.resource_id AND p.module_uuid IS NULL
-        where
-        ur.role=#{role,jdbcType=VARCHAR}
-        and ur.user_id = #{userId,jdbcType=INTEGER}
-      </when>
-      <otherwise>
-        SELECT p.uuid as root_project_uuid
-        FROM group_roles gr
-        INNER JOIN projects p on p.id = gr.resource_id AND p.module_uuid IS NULL
-        where
-        gr.role=#{role,jdbcType=VARCHAR}
-        and gr.group_id is null
-      </otherwise>
-    </choose>
-  </select>
-
   <select id="keepAuthorizedUsersForRoleAndProject" parameterType="map" resultType="int">
     SELECT gu.user_id
     FROM groups_users gu
index cb1565e177ce3c25cc66e2194807a11c8dd1cb46..59f031b29dcf058c2849c836a11b13cb3ab99e0e 100644 (file)
@@ -298,34 +298,6 @@ public class AuthorizationDaoTest {
     assertThat(componentIds).isEmpty();
   }
 
-  @Test
-  public void should_return_root_project_uuids_for_user() {
-    db.prepareDbUnit(getClass(), "should_return_root_project_keys_for_user.xml");
-
-    Collection<String> rootProjectUuids = underTest.selectAuthorizedRootProjectsUuids(dbSession, USER, "user");
-
-    assertThat(rootProjectUuids).containsOnly("ABCD");
-  }
-
-  @Test
-  public void should_return_root_project_uuids_for_group() {
-    // but user is not in an authorized group
-    db.prepareDbUnit(getClass(), "should_return_root_project_keys_for_group.xml");
-
-    Collection<String> rootProjectUuids = underTest.selectAuthorizedRootProjectsUuids(dbSession, USER, "user");
-
-    assertThat(rootProjectUuids).containsOnly("ABCD");
-  }
-
-  @Test
-  public void should_return_root_project_uuids_for_anonymous() {
-    db.prepareDbUnit(getClass(), "should_return_root_project_keys_for_anonymous.xml");
-
-    Collection<String> rootProjectUuids = underTest.selectAuthorizedRootProjectsUuids(dbSession, null, "user");
-
-    assertThat(rootProjectUuids).containsOnly("ABCD");
-  }
-
   @Test
   public void keep_authorized_users_for_role_and_project_for_user() {
     db.prepareDbUnit(getClass(), "keep_authorized_users_for_role_and_project_for_user.xml");
index b94c6470baa83f4486508ed5967b3f01defbfb33..88bdc89961789bf41d0539ec883cd81a9cd4016e 100644 (file)
  */
 package org.sonar.server.favorite.ws;
 
-import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.function.Predicate;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -88,9 +86,7 @@ public class SearchAction implements FavoritesWsAction {
   private SearchResults toSearchResults(SearchRequest request) {
     userSession.checkLoggedIn();
     try (DbSession dbSession = dbClient.openSession(false)) {
-      List<ComponentDto> authorizedFavorites = favoriteFinder.list().stream()
-        .filter(isAuthorized(dbSession))
-        .collect(MoreCollectors.toList());
+      List<ComponentDto> authorizedFavorites = getAuthorizedFavorites(dbSession);
       Paging paging = Paging.forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal(authorizedFavorites.size());
       List<ComponentDto> displayedFavorites = authorizedFavorites.stream()
         .skip(paging.offset())
@@ -101,21 +97,24 @@ public class SearchAction implements FavoritesWsAction {
     }
   }
 
-  private Predicate<ComponentDto> isAuthorized(DbSession dbSession) {
-    Collection<String> rootProjectsUuids = dbClient.authorizationDao().selectAuthorizedRootProjectsUuids(dbSession, userSession.getUserId(), UserRole.USER);
-    Set<String> authorizedProjectUuids = rootProjectsUuids
-      .stream()
-      .collect(MoreCollectors.toSet(rootProjectsUuids.size()));
-    return dto -> authorizedProjectUuids.contains(dto.projectUuid());
+  private List<ComponentDto> getAuthorizedFavorites(DbSession dbSession) {
+    List<ComponentDto> componentDtos = favoriteFinder.list();
+    Set<Long> favoriteComponentIds = componentDtos.stream()
+      .map(ComponentDto::getId)
+      .collect(MoreCollectors.toSet(componentDtos.size()));
+    Set<Long> authorizedFavoriteComponentIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, favoriteComponentIds, userSession.getUserId(), UserRole.USER);
+    return componentDtos.stream()
+      .filter(dto -> authorizedFavoriteComponentIds.contains(dto.getId()))
+      .collect(MoreCollectors.toList());
   }
 
   private Map<String, OrganizationDto> getOrganizationsOfComponents(DbSession dbSession, List<ComponentDto> displayedFavorites) {
     Set<String> organizationUuids = displayedFavorites.stream()
-        .map(ComponentDto::getOrganizationUuid)
-        .collect(MoreCollectors.toSet());
+      .map(ComponentDto::getOrganizationUuid)
+      .collect(MoreCollectors.toSet());
     return dbClient.organizationDao().selectByUuids(dbSession, organizationUuids)
-        .stream()
-        .collect(MoreCollectors.uniqueIndex(OrganizationDto::getUuid));
+      .stream()
+      .collect(MoreCollectors.uniqueIndex(OrganizationDto::getUuid));
   }
 
   private static class SearchResults {
index d7d0a8bbd458d1bf2be4a37bbd5ea3960f92a0b8..a24d4c8a37abe08fdc67f00bee6cb00ab8eaf35d 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.server.notification.ws;
 
 import com.google.common.base.Splitter;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -150,14 +149,14 @@ public class ListAction implements NotificationsWsAction {
   }
 
   private Map<Long, ComponentDto> searchProjects(DbSession dbSession, List<PropertyDto> properties) {
-    Set<String> authorizedComponentUuids = new HashSet<>(dbClient.authorizationDao().selectAuthorizedRootProjectsUuids(dbSession, userSession.getUserId(), UserRole.USER));
     Set<Long> componentIds = properties.stream()
       .map(PropertyDto::getResourceId)
       .filter(Objects::nonNull)
       .collect(MoreCollectors.toSet(properties.size()));
+    Set<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, componentIds, userSession.getUserId(), UserRole.USER);
     return dbClient.componentDao().selectByIds(dbSession, componentIds)
       .stream()
-      .filter(c -> authorizedComponentUuids.contains(c.uuid()))
+      .filter(c -> authorizedProjectIds.contains(c.getId()))
       .collect(MoreCollectors.uniqueIndex(ComponentDto::getId));
   }