]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8325 Authorization#keepAuthorizedProjectIds now returns a set
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Nov 2016 13:33:23 +0000 (14:33 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Nov 2016 14:36:46 +0000 (15:36 +0100)
server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java
sonar-db/src/main/java/org/sonar/db/DatabaseUtils.java
sonar-db/src/main/java/org/sonar/db/permission/AuthorizationDao.java
sonar-db/src/main/java/org/sonar/db/permission/AuthorizationMapper.java

index 5ec548b0722dfedcffad2d8c4e7488e91a3373a1..61d6e70e5c86dd752543e4e65a4631c7e6d0aacd 100644 (file)
@@ -139,7 +139,7 @@ public class SearchAction implements MeasuresWsAction {
 
     private List<ComponentDto> getAuthorizedProjects(List<ComponentDto> projectDtos) {
       Map<String, Long> projectIdsByUuids = projectDtos.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
-      Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession,
+      Set<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession,
         projectDtos.stream().map(ComponentDto::getId).collect(toList()),
         userSession.getUserId(), UserRole.USER);
       return projectDtos.stream()
index 278d932390f22962e8022af85107c003b56a35db..aa76ec2849dfed397c04827c41aade7638044144 100644 (file)
@@ -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 <OUTPUT, INPUT extends Comparable<INPUT>> List<OUTPUT> executeLargeInputs(Collection<INPUT> input, Function<List<INPUT>, List<OUTPUT>> function) {
+    return executeLargeInputs(input, function, size -> size == 0 ? Collections.emptyList() : new ArrayList<>(size));
+  }
+
+  public static <OUTPUT, INPUT extends Comparable<INPUT>> Set<OUTPUT> executeLargeInputsIntoSet(Collection<INPUT> input, Function<List<INPUT>, Set<OUTPUT>> function) {
+    return executeLargeInputs(input, function, size -> size == 0 ? Collections.emptySet() : new HashSet<>(size));
+  }
+
+  private static <OUTPUT, INPUT extends Comparable<INPUT>, RESULT extends Collection<OUTPUT>> RESULT executeLargeInputs(Collection<INPUT> input,
+    Function<List<INPUT>, RESULT> function, java.util.function.Function<Integer, RESULT> outputInitializer) {
     if (input.isEmpty()) {
-      return Collections.emptyList();
+      return outputInitializer.apply(0);
     }
-    List<OUTPUT> results = new ArrayList<>(input.size());
+    RESULT results = outputInitializer.apply(input.size());
     for (List<INPUT> partition : toUniqueAndSortedPartitions(input)) {
-      List<OUTPUT> subResults = function.apply(partition);
+      RESULT subResults = function.apply(partition);
       if (subResults != null) {
         results.addAll(subResults);
       }
index 2cd2195ab91a4d19a855cd37d4c825bb30cf3285..9b60f6101170a44426d700baebd67b8e7835ddb6 100644 (file)
@@ -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<Long> keepAuthorizedProjectIds(DbSession dbSession, Collection<Long> componentIds, @Nullable Integer userId, String role) {
-    return executeLargeInputs(
+  public Set<Long> keepAuthorizedProjectIds(DbSession dbSession, Collection<Long> componentIds, @Nullable Integer userId, String role) {
+    return executeLargeInputsIntoSet(
       componentIds,
       partition -> {
         if (userId == null) {
index 00cbcd9e28ed44cb49f48cd2c3f1dd008c5dbd7e..df686f1cac6c96348a5a833059fd632c5f10cdd5 100644 (file)
@@ -43,9 +43,9 @@ public interface AuthorizationMapper {
   int countUsersWithGlobalPermissionExcludingUser(@Param("organizationUuid") String organizationUuid, @Param("permission") String permission,
     @Param("excludedUserId") long excludedUserId);
 
-  List<Long> keepAuthorizedProjectIdsForAnonymous(@Param("role") String role, @Param("componentIds") Collection<Long> componentIds);
+  Set<Long> keepAuthorizedProjectIdsForAnonymous(@Param("role") String role, @Param("componentIds") Collection<Long> componentIds);
 
-  List<Long> keepAuthorizedProjectIdsForUser(@Param("userId") long userId, @Param("role") String role, @Param("componentIds") Collection<Long> componentIds);
+  Set<Long> keepAuthorizedProjectIdsForUser(@Param("userId") long userId, @Param("role") String role, @Param("componentIds") Collection<Long> componentIds);
 
   List<String> keepAuthorizedComponentKeysForAnonymous(@Param("role") String role, @Param("componentKeys") Collection<String> componentKeys);