]> source.dussan.org Git - sonarqube.git/commitdiff
Remove most usages of AuthorizationDao#keepAuthorizedProjectIds()
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 10 May 2017 19:38:57 +0000 (21:38 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 10 May 2017 21:08:31 +0000 (23:08 +0200)
replaced by UserSession#keepAuthorizedComponents()

server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationDao.java
server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/favorite/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java
server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/favorite/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java

index 06eda85127486168eab0a9c5eb43bf6c59db3bf1..7f7639c141651100d0924c0cff01fdac838da743 100644 (file)
@@ -124,6 +124,10 @@ public class AuthorizationDao implements Dao {
     return mapper(dbSession).selectOrganizationUuidsOfUserWithGlobalPermission(userId, permission);
   }
 
+  /**
+   * @deprecated replaced by {@link #keepAuthorizedProjectUuids(DbSession, Collection, Integer, String)}
+   */
+  @Deprecated
   public Set<Long> keepAuthorizedProjectIds(DbSession dbSession, Collection<Long> componentIds, @Nullable Integer userId, String permission) {
     return executeLargeInputsIntoSet(
       componentIds,
index 5c57fbb2f66eb1a922ea40887c1227ea8137addb..658de71faa9867ea44410530b2d291a37ad32851 100644 (file)
  */
 package org.sonar.server.component.ws;
 
-import java.util.Collection;
 import java.util.List;
-import java.util.Map;
 import java.util.Optional;
-import java.util.Set;
 import org.sonar.api.i18n.I18n;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.resources.ResourceTypes;
@@ -32,7 +29,7 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.Paging;
-import org.sonar.core.util.stream.MoreCollectors;
+import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
@@ -46,13 +43,11 @@ import org.sonarqube.ws.WsComponents.SearchWsResponse;
 import org.sonarqube.ws.client.component.SearchWsRequest;
 
 import static com.google.common.base.Preconditions.checkArgument;
-import static org.sonar.api.web.UserRole.USER;
 import static org.sonar.core.util.Protobuf.setNullable;
-import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
 import static org.sonar.server.util.LanguageParamUtils.getExampleValue;
 import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys;
-import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
+import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_SEARCH;
 import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_LANGUAGE;
@@ -156,23 +151,7 @@ public class SearchAction implements ComponentsWsAction {
 
   private List<ComponentDto> searchComponents(DbSession dbSession, OrganizationDto organization, ComponentQuery query, Paging paging) {
     List<ComponentDto> componentDtos = dbClient.componentDao().selectByQuery(dbSession, organization.getUuid(), query, paging.offset(), paging.pageSize());
-    return filterAuthorizedComponents(dbSession, componentDtos);
-  }
-
-  private List<ComponentDto> filterAuthorizedComponents(DbSession dbSession, List<ComponentDto> componentDtos) {
-    if (userSession.isRoot()) {
-      // the method AuthorizationDao#keepAuthorizedProjectIds() should be replaced by
-      // a call to UserSession, which would transparently support roots.
-      // Meanwhile root is explicitly handled.
-      return componentDtos;
-    }
-    Set<String> projectUuids = componentDtos.stream().map(ComponentDto::projectUuid).collect(MoreCollectors.toSet());
-    List<ComponentDto> projects = dbClient.componentDao().selectByUuids(dbSession, projectUuids);
-    Map<String, Long> projectIdsByUuids = projects.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
-    Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectIdsByUuids.values(), userSession.getUserId(), USER);
-    return componentDtos.stream()
-      .filter(component -> authorizedProjectIds.contains(projectIdsByUuids.get(component.projectUuid())))
-      .collect(MoreCollectors.toList());
+    return userSession.keepAuthorizedComponents(UserRole.USER, componentDtos);
   }
 
   private static SearchWsResponse buildResponse(List<ComponentDto> components, OrganizationDto organization, Paging paging) {
index 88bdc89961789bf41d0539ec883cd81a9cd4016e..800345779c9bb682c8fe7df9a319abf2bb94649c 100644 (file)
@@ -86,7 +86,7 @@ public class SearchAction implements FavoritesWsAction {
   private SearchResults toSearchResults(SearchRequest request) {
     userSession.checkLoggedIn();
     try (DbSession dbSession = dbClient.openSession(false)) {
-      List<ComponentDto> authorizedFavorites = getAuthorizedFavorites(dbSession);
+      List<ComponentDto> authorizedFavorites = getAuthorizedFavorites();
       Paging paging = Paging.forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal(authorizedFavorites.size());
       List<ComponentDto> displayedFavorites = authorizedFavorites.stream()
         .skip(paging.offset())
@@ -97,15 +97,9 @@ public class SearchAction implements FavoritesWsAction {
     }
   }
 
-  private List<ComponentDto> getAuthorizedFavorites(DbSession dbSession) {
+  private List<ComponentDto> getAuthorizedFavorites() {
     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());
+    return userSession.keepAuthorizedComponents(UserRole.USER, componentDtos);
   }
 
   private Map<String, OrganizationDto> getOrganizationsOfComponents(DbSession dbSession, List<ComponentDto> displayedFavorites) {
index dc51adcc989af40a1bae941e0c49b74ab85bd08e..573c6c83e9bd03b16b1da455fd168abaa4249cfa 100644 (file)
@@ -70,7 +70,6 @@ import static org.sonar.api.rule.Severity.BLOCKER;
 import static org.sonar.api.rules.RuleType.BUG;
 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
-import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
 import static org.sonar.server.es.SearchOptions.MAX_LIMIT;
 import static org.sonar.server.issue.AbstractChangeTagsAction.TAGS_PARAMETER;
@@ -302,7 +301,7 @@ public class BulkChangeAction implements IssuesWsAction {
       List<IssueDto> allIssues = dbClient.issueDao().selectByKeys(dbSession, issueKeys);
 
       List<ComponentDto> allProjects = getComponents(dbSession, allIssues.stream().map(IssueDto::getProjectUuid).collect(MoreCollectors.toSet()));
-      this.projectsByUuid = getAuthorizedProjects(dbSession, allProjects).stream().collect(uniqueIndex(ComponentDto::uuid, identity()));
+      this.projectsByUuid = getAuthorizedProjects(allProjects).stream().collect(uniqueIndex(ComponentDto::uuid, identity()));
       this.issues = getAuthorizedIssues(allIssues);
       this.componentsByUuid = getComponents(dbSession,
         issues.stream().map(DefaultIssue::componentUuid).collect(MoreCollectors.toSet())).stream()
@@ -321,14 +320,8 @@ public class BulkChangeAction implements IssuesWsAction {
       return dbClient.componentDao().selectByUuids(dbSession, componentUuids);
     }
 
-    private List<ComponentDto> getAuthorizedProjects(DbSession dbSession, List<ComponentDto> projectDtos) {
-      Map<String, Long> projectIdsByUuids = projectDtos.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
-      Set<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession,
-        projectDtos.stream().map(ComponentDto::getId).collect(toList()),
-        userSession.getUserId(), UserRole.USER);
-      return projectDtos.stream()
-        .filter(project -> authorizedProjectIds.contains(projectIdsByUuids.get(project.projectUuid())))
-        .collect(MoreCollectors.toList());
+    private List<ComponentDto> getAuthorizedProjects(List<ComponentDto> projectDtos) {
+      return userSession.keepAuthorizedComponents(UserRole.USER, projectDtos);
     }
 
     private List<DefaultIssue> getAuthorizedIssues(List<IssueDto> allIssues) {
index bc21efd66fb9a05250c45314264ce4082810dd77..6c216afb2140e58732e6aa8db94afd1dde21c591 100644 (file)
@@ -49,7 +49,6 @@ import static org.sonar.api.resources.Qualifiers.PROJECT;
 import static org.sonar.api.resources.Qualifiers.SUBVIEW;
 import static org.sonar.api.resources.Qualifiers.VIEW;
 import static org.sonar.core.util.stream.MoreCollectors.toList;
-import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
 import static org.sonar.server.measure.ws.MeasureDtoToWsMeasure.updateMeasureBuilder;
 import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createMetricKeysParameter;
 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
@@ -141,21 +140,7 @@ public class SearchAction implements MeasuresWsAction {
     }
 
     private List<ComponentDto> getAuthorizedProjects(List<ComponentDto> componentDtos) {
-      if (userSession.isRoot()) {
-        // the method AuthorizationDao#keepAuthorizedProjectIds() should be replaced by
-        // a call to UserSession, which would transparently support roots.
-        // Meanwhile root is explicitly handled.
-        return componentDtos;
-      }
-      Set<String> projectUuids = componentDtos.stream().map(ComponentDto::projectUuid).collect(MoreCollectors.toSet());
-      List<ComponentDto> projectDtos = dbClient.componentDao().selectByUuids(dbSession, projectUuids);
-      Map<String, Long> projectIdsByUuids = projectDtos.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
-      Set<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession,
-        projectDtos.stream().map(ComponentDto::getId).collect(toList()),
-        userSession.getUserId(), UserRole.USER);
-      return componentDtos.stream()
-        .filter(component -> authorizedProjectIds.contains(projectIdsByUuids.get(component.projectUuid())))
-        .collect(MoreCollectors.toList());
+      return userSession.keepAuthorizedComponents(UserRole.USER, componentDtos);
     }
 
     private List<MetricDto> searchMetrics() {
index 2a119d7a7ed6de4730dfe6c434a635ba9b59cb67..6e3836f783c66b4f3fdcc4e66efd9d99139fdea7 100644 (file)
@@ -21,25 +21,20 @@ package org.sonar.server.project.ws;
 
 import com.google.common.io.Resources;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.List;
-import java.util.Map;
 import java.util.Optional;
-import java.util.Set;
 import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.core.util.stream.MoreCollectors;
+import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.user.UserSession;
 
 import static java.util.Optional.ofNullable;
-import static org.sonar.api.web.UserRole.USER;
-import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
 import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_INDEX;
 
@@ -99,7 +94,7 @@ public class IndexAction implements ProjectsWsAction {
   @Override
   public void handle(Request request, Response response) throws Exception {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      List<ComponentDto> projects = getAuthorizedComponents(dbSession, searchComponents(dbSession, request));
+      List<ComponentDto> projects = getAuthorizedComponents(searchComponents(dbSession, request));
       JsonWriter json = response.newJsonWriter();
       json.beginArray();
       for (ComponentDto project : projects) {
@@ -132,20 +127,8 @@ public class IndexAction implements ProjectsWsAction {
     return projects;
   }
 
-  private List<ComponentDto> getAuthorizedComponents(DbSession dbSession, List<ComponentDto> components) {
-    if (userSession.isRoot() || components.isEmpty()) {
-      // the method AuthorizationDao#keepAuthorizedProjectIds() should be replaced by
-      // a call to UserSession, which would transparently support roots.
-      // Meanwhile root is explicitly handled.
-      return components;
-    }
-    Set<String> projectUuids = components.stream().map(ComponentDto::projectUuid).collect(MoreCollectors.toSet());
-    List<ComponentDto> projects = dbClient.componentDao().selectByUuids(dbSession, projectUuids);
-    Map<String, Long> projectIdsByUuids = projects.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
-    Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectIdsByUuids.values(), userSession.getUserId(), USER);
-    return components.stream()
-      .filter(component -> authorizedProjectIds.contains(projectIdsByUuids.get(component.projectUuid())))
-      .collect(MoreCollectors.toList());
+  private List<ComponentDto> getAuthorizedComponents(List<ComponentDto> components) {
+    return userSession.keepAuthorizedComponents(UserRole.USER, components);
   }
 
   private static void addProject(JsonWriter json, ComponentDto project) {
index b3d35693ad0399475b3cdc9d7ae402c6f63f72a6..0a22c4c68da69bd8df8adb9d7e34d1218562e27b 100644 (file)
@@ -228,7 +228,7 @@ public class SearchActionTest {
     ComponentDto directory = newDirectory(module, "path/to/directoy").setUuid("directory-uuid").setKey("directory-key").setName("Directory Name");
     db.components().insertComponents(project, module, directory,
       newFileDto(module, directory, "file-uuid").setKey("file-key").setLanguage("java").setName("File Name"));
-    setBrowsePermissionOnUser(project);
+    userSession.addProjectPermission(UserRole.USER, project);
 
     String response = ws.newRequest()
       .setMediaType(MediaTypes.JSON)
@@ -245,8 +245,7 @@ public class SearchActionTest {
   }
 
   private void setBrowsePermissionOnUser(ComponentDto... projects) {
-    Arrays.stream(projects).forEach(project -> db.users().insertProjectPermissionOnUser(user, UserRole.USER, project));
-    db.getSession().commit();
+    Arrays.stream(projects).forEach(project -> userSession.addProjectPermission(UserRole.USER, project));
   }
 
   private SearchWsResponse call(SearchWsRequest wsRequest) {
index 0b5f908c9ecabcc1edaaa0c6e3dc54f81ac396a8..25dcf91d6a4f5d0731fdc1fb92acdb449a6f1043 100644 (file)
@@ -28,12 +28,10 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.permission.UserPermissionDto;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.favorite.FavoriteFinder;
 import org.sonar.server.tester.UserSessionRule;
@@ -62,7 +60,6 @@ public class SearchActionTest {
   @Rule
   public DbTester db = DbTester.create();
   private DbClient dbClient = db.getDbClient();
-  private DbSession dbSession = db.getSession();
 
   private FavoriteFinder favoriteFinder = new FavoriteFinder(dbClient, userSession);
 
@@ -132,7 +129,6 @@ public class SearchActionTest {
     ComponentDto otherUserFavorite = ComponentTesting.newPrivateProjectDto(organizationDto).setKey("K42");
     db.components().insertComponent(otherUserFavorite);
     db.favorites().add(otherUserFavorite, 42);
-    dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(organizationDto.getUuid(), UserRole.USER, 42, otherUserFavorite.getId()));
     db.commit();
 
     SearchResponse result = call();
@@ -186,8 +182,8 @@ public class SearchActionTest {
   private void addComponent(ComponentDto component) {
     db.components().insertComponent(component);
     db.favorites().add(component, USER_ID);
-    dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(component.getOrganizationUuid(), UserRole.USER, USER_ID, component.getId()));
     db.commit();
+    userSession.addProjectPermission(UserRole.USER, component);
   }
 
   private SearchResponse call(@Nullable Integer page, @Nullable Integer pageSize) {
index 120234b0eafe1fc66528cffb7dd255b3e22e19ea..8b15aa45dc649d66632a8e1e071bc46afd329509 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.measure.ws;
 
 import com.google.common.base.Joiner;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
@@ -510,9 +511,6 @@ public class SearchActionTest {
   }
 
   private void setBrowsePermissionOnUser(ComponentDto... projects) {
-    for (ComponentDto project : projects) {
-      db.users().insertProjectPermissionOnUser(user, UserRole.USER, project);
-    }
-    dbSession.commit();
+    Arrays.stream(projects).forEach(p -> userSession.addProjectPermission(UserRole.USER, p));
   }
 }
index a732a8ead2dd5953337bbda32aed829dae7c8d80..8c321b2e9cdffe69cb4b5a5c17a033748870dbbd 100644 (file)
@@ -224,8 +224,7 @@ public class IndexActionTest {
   }
 
   private void setBrowsePermissionOnUser(ComponentDto... projects) {
-    Arrays.stream(projects).forEach(project -> db.users().insertProjectPermissionOnUser(user, UserRole.USER, project));
-    db.getSession().commit();
+    Arrays.stream(projects).forEach(project -> userSession.addProjectPermission(UserRole.USER, project));
   }
 
   private void verifyResult(String json, String expectedJsonFile) {