diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-02-13 23:38:53 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-02-14 10:16:41 +0100 |
commit | d56f6930ac2d9c1402f06df78ac7860e79d2ecc1 (patch) | |
tree | b35bf2fe290b147474e456b76fa09dd7acd24367 /server/sonar-server/src | |
parent | bbf5c35f095f2136e0f768bcc1f1b4eb8e856248 (diff) | |
download | sonarqube-d56f6930ac2d9c1402f06df78ac7860e79d2ecc1.tar.gz sonarqube-d56f6930ac2d9c1402f06df78ac7860e79d2ecc1.zip |
SONAR-8647 Do not load favorites if anonymous
Diffstat (limited to 'server/sonar-server/src')
2 files changed, 39 insertions, 15 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java index 123c8d0e922..8785138925a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java @@ -33,6 +33,7 @@ import java.util.stream.Collector; import java.util.stream.Stream; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -170,8 +171,8 @@ public class SearchProjectsAction implements ComponentsWsAction { private SearchResults searchData(DbSession dbSession, SearchProjectsRequest request, @Nullable OrganizationDto organization) { List<String> criteria = toCriteria(firstNonNull(request.getFilter(), "")); - List<ComponentDto> favoriteProjects = searchFavoriteProjects(dbSession); - Set<String> projectUuids = buildFilterOnFavoriteProjectUuids(criteria, favoriteProjects); + Set<String> favoriteProjectUuids = loadFavoriteProjectUuids(dbSession); + Set<String> projectUuids = buildFilterOnFavoriteProjectUuids(criteria, favoriteProjectUuids); ProjectMeasuresQuery query = newProjectMeasuresQuery(criteria, projectUuids); Optional.ofNullable(organization) @@ -187,32 +188,38 @@ public class SearchProjectsAction implements ComponentsWsAction { Ordering<ComponentDto> ordering = Ordering.explicit(esResults.getIds()).onResultOf(ComponentDto::uuid); List<ComponentDto> projects = ordering.immutableSortedCopy(dbClient.componentDao().selectByUuids(dbSession, esResults.getIds())); - return new SearchResults(projects, favoriteProjects.stream().map(ComponentDto::uuid).collect(toSet()), esResults); + return new SearchResults(projects, favoriteProjectUuids, esResults); } @CheckForNull - private Set<String> buildFilterOnFavoriteProjectUuids(List<String> criteria, List<ComponentDto> favoriteProjects) { - boolean hasIsFavouriteCriterion = hasIsFavouriteCriterion(criteria); - if (hasIsFavouriteCriterion) { - return favoriteProjects.stream() - .map(ComponentDto::uuid) - .collect(toSet()); + private Set<String> buildFilterOnFavoriteProjectUuids(List<String> criteria, Set<String> favoriteProjectUuids) { + if (hasIsFavouriteCriterion(criteria)) { + return favoriteProjectUuids; } return null; } - private List<ComponentDto> searchFavoriteProjects(DbSession dbSession) { - List<Long> favoriteDbIds = dbClient.propertiesDao().selectByQuery( + private Set<String> loadFavoriteProjectUuids(DbSession dbSession) { + if (!userSession.isLoggedIn()) { + return Collections.emptySet(); + } + + List<PropertyDto> props = dbClient.propertiesDao().selectByQuery( PropertyQuery.builder() .setUserId(userSession.getUserId()) .setKey("favourite") .build(), - dbSession) - .stream() + dbSession); + + List<Long> favoriteDbIds = props.stream() .map(PropertyDto::getResourceId) - .collect(Collectors.toList()); + .collect(Collectors.toList(props.size())); - return dbClient.componentDao().selectByIds(dbSession, favoriteDbIds); + return dbClient.componentDao().selectByIds(dbSession, favoriteDbIds).stream() + .filter(ComponentDto::isEnabled) + .filter(f -> f.qualifier().equals(Qualifiers.PROJECT)) + .map(ComponentDto::uuid) + .collect(Collectors.toSet()); } private static SearchProjectsRequest toRequest(Request httpRequest) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java index a958d0c03fa..8d0d0c852cd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java @@ -235,6 +235,7 @@ public class SearchProjectsActionTest { @Test public void filter_favourite_projects_with_query_with_or_without_a_specified_organization() { + userSession.logIn(); OrganizationDto organization1 = db.organizations().insert(); OrganizationDto organization2 = db.organizations().insert(); OrganizationDto organization3 = db.organizations().insert(); @@ -283,6 +284,7 @@ public class SearchProjectsActionTest { @Test public void filter_projects_on_favorites() { + userSession.logIn(); ComponentDto javaProject = insertProjectInDbAndEs(newProjectDto(db.getDefaultOrganization(), "java-id").setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 10_000d))); ComponentDto markDownProject = insertProjectInDbAndEs(newProjectDto(db.getDefaultOrganization(), "markdown-id").setName("Sonar Markdown"), @@ -300,6 +302,21 @@ public class SearchProjectsActionTest { } @Test + public void filtering_on_favorites_returns_empty_results_if_not_logged_in() { + ComponentDto javaProject = insertProjectInDbAndEs(newProjectDto(db.getDefaultOrganization(), "java-id").setName("Sonar Java"), + newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 10_000d))); + insertProjectInDbAndEs(newProjectDto(db.organizations().insert()).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d))); + addFavourite(javaProject); + dbSession.commit(); + request.setFilter("isFavorite"); + userSession.anonymous(); + + SearchProjectsWsResponse result = call(request); + + assertThat(result.getComponentsCount()).isEqualTo(0); + } + + @Test public void do_not_return_isFavorite_if_anonymous_user() { insertProjectInDbAndEs(newProjectDto(db.getDefaultOrganization()).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81))); insertMetrics(COVERAGE); |