From: Julien Lancelot Date: Thu, 3 Nov 2016 13:33:23 +0000 (+0100) Subject: SONAR-8325 Authorization#keepAuthorizedProjectIds now returns a set X-Git-Tag: 6.2-RC1~222 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d965fca5ff557a3d2cab134435ce05b8d8ab5df1;p=sonarqube.git SONAR-8325 Authorization#keepAuthorizedProjectIds now returns a set --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java index 5ec548b0722..61d6e70e5c8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java @@ -139,7 +139,7 @@ public class SearchAction implements MeasuresWsAction { private List getAuthorizedProjects(List projectDtos) { Map projectIdsByUuids = projectDtos.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId)); - Collection authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, + Set authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectDtos.stream().map(ComponentDto::getId).collect(toList()), userSession.getUserId(), UserRole.USER); return projectDtos.stream() diff --git a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java index 278d932390f..aa76ec2849d 100644 --- a/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java +++ b/sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java @@ -33,6 +33,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Date; +import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.function.Consumer; @@ -123,12 +124,21 @@ public class DatabaseUtils { * and with MsSQL when there's more than 2000 parameters in a query */ public static > List executeLargeInputs(Collection input, Function, List> function) { + return executeLargeInputs(input, function, size -> size == 0 ? Collections.emptyList() : new ArrayList<>(size)); + } + + public static > Set executeLargeInputsIntoSet(Collection input, Function, Set> function) { + return executeLargeInputs(input, function, size -> size == 0 ? Collections.emptySet() : new HashSet<>(size)); + } + + private static , RESULT extends Collection> RESULT executeLargeInputs(Collection input, + Function, RESULT> function, java.util.function.Function outputInitializer) { if (input.isEmpty()) { - return Collections.emptyList(); + return outputInitializer.apply(0); } - List results = new ArrayList<>(input.size()); + RESULT results = outputInitializer.apply(input.size()); for (List partition : toUniqueAndSortedPartitions(input)) { - List subResults = function.apply(partition); + RESULT subResults = function.apply(partition); if (subResults != null) { results.addAll(subResults); } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/AuthorizationDao.java b/sonar-db/src/main/java/org/sonar/db/permission/AuthorizationDao.java index 2cd2195ab91..9b60f610117 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/AuthorizationDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/AuthorizationDao.java @@ -31,6 +31,7 @@ import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import static org.sonar.db.DatabaseUtils.executeLargeInputs; +import static org.sonar.db.DatabaseUtils.executeLargeInputsIntoSet; /** * The SQL requests used to verify authorization (the permissions @@ -95,8 +96,8 @@ public class AuthorizationDao implements Dao { return mapper(dbSession).countUsersWithGlobalPermissionExcludingUser(organizationUuid, permission, excludedUSerId); } - public Collection keepAuthorizedProjectIds(DbSession dbSession, Collection componentIds, @Nullable Integer userId, String role) { - return executeLargeInputs( + public Set keepAuthorizedProjectIds(DbSession dbSession, Collection componentIds, @Nullable Integer userId, String role) { + return executeLargeInputsIntoSet( componentIds, partition -> { if (userId == null) { diff --git a/sonar-db/src/main/java/org/sonar/db/permission/AuthorizationMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/AuthorizationMapper.java index 00cbcd9e28e..df686f1cac6 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/AuthorizationMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/AuthorizationMapper.java @@ -43,9 +43,9 @@ public interface AuthorizationMapper { int countUsersWithGlobalPermissionExcludingUser(@Param("organizationUuid") String organizationUuid, @Param("permission") String permission, @Param("excludedUserId") long excludedUserId); - List keepAuthorizedProjectIdsForAnonymous(@Param("role") String role, @Param("componentIds") Collection componentIds); + Set keepAuthorizedProjectIdsForAnonymous(@Param("role") String role, @Param("componentIds") Collection componentIds); - List keepAuthorizedProjectIdsForUser(@Param("userId") long userId, @Param("role") String role, @Param("componentIds") Collection componentIds); + Set keepAuthorizedProjectIdsForUser(@Param("userId") long userId, @Param("role") String role, @Param("componentIds") Collection componentIds); List keepAuthorizedComponentKeysForAnonymous(@Param("role") String role, @Param("componentKeys") Collection componentKeys);