LCOV - code coverage report
Current view: top level - server/query/change - ChangeIsVisibleToPredicate.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 38 68.4 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2010 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.query.change;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.entities.Change;
      19             : import com.google.gerrit.exceptions.StorageException;
      20             : import com.google.gerrit.index.query.IsVisibleToPredicate;
      21             : import com.google.gerrit.server.AnonymousUser;
      22             : import com.google.gerrit.server.CurrentUser;
      23             : import com.google.gerrit.server.InternalUser;
      24             : import com.google.gerrit.server.index.IndexUtils;
      25             : import com.google.gerrit.server.permissions.ChangePermission;
      26             : import com.google.gerrit.server.permissions.PermissionBackend;
      27             : import com.google.gerrit.server.permissions.PermissionBackendException;
      28             : import com.google.gerrit.server.project.ProjectCache;
      29             : import com.google.gerrit.server.project.ProjectState;
      30             : import com.google.inject.Inject;
      31             : import com.google.inject.Provider;
      32             : import com.google.inject.assistedinject.Assisted;
      33             : import java.util.Optional;
      34             : import org.eclipse.jgit.errors.RepositoryNotFoundException;
      35             : 
      36             : public class ChangeIsVisibleToPredicate extends IsVisibleToPredicate<ChangeData> {
      37         104 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      38             : 
      39             :   public interface Factory {
      40             :     ChangeIsVisibleToPredicate forUser(CurrentUser user);
      41             :   }
      42             : 
      43             :   protected final CurrentUser user;
      44             :   protected final ProjectCache projectCache;
      45             :   private final PermissionBackend.WithUser withUser;
      46             : 
      47             :   @Inject
      48             :   public ChangeIsVisibleToPredicate(
      49             :       PermissionBackend permissionBackend,
      50             :       ProjectCache projectCache,
      51             :       Provider<AnonymousUser> anonymousUserProvider,
      52             :       @Assisted CurrentUser user) {
      53         104 :     super(ChangeQueryBuilder.FIELD_VISIBLETO, IndexUtils.describe(user));
      54         104 :     this.user = user;
      55         104 :     this.projectCache = projectCache;
      56         104 :     withUser =
      57         104 :         user.isIdentifiedUser()
      58         104 :             ? permissionBackend.absentUser(user.getAccountId())
      59         104 :             : permissionBackend.user(
      60           6 :                 Optional.of(user)
      61           6 :                     .filter(u -> u instanceof GroupBackedUser || u instanceof InternalUser)
      62           6 :                     .orElseGet(anonymousUserProvider::get));
      63         104 :   }
      64             : 
      65             :   @Override
      66             :   public boolean match(ChangeData cd) {
      67          76 :     if (cd.fastIsVisibleTo(user)) {
      68           4 :       return true;
      69             :     }
      70          76 :     Change change = cd.change();
      71          76 :     if (change == null) {
      72           0 :       return false;
      73             :     }
      74             : 
      75          76 :     Optional<ProjectState> projectState = projectCache.get(cd.project());
      76          76 :     if (!projectState.isPresent()) {
      77           0 :       logger.atFine().log("Filter out change %s of non-existing project %s", cd, cd.project());
      78           0 :       return false;
      79             :     }
      80          76 :     if (!projectState.get().statePermitsRead()) {
      81           0 :       logger.atFine().log("Filter out change %s of non-readable project %s", cd, cd.project());
      82           0 :       return false;
      83             :     }
      84             : 
      85             :     try {
      86          76 :       if (!withUser.change(cd).test(ChangePermission.READ)) {
      87          13 :         logger.atFine().log("Filter out non-visisble change: %s", cd);
      88          13 :         return false;
      89             :       }
      90           0 :     } catch (PermissionBackendException e) {
      91           0 :       Throwable cause = e.getCause();
      92           0 :       if (cause instanceof RepositoryNotFoundException) {
      93           0 :         logger.atWarning().withCause(e).log(
      94             :             "Filter out change %s because the corresponding repository %s was not found",
      95           0 :             cd, cd.project());
      96           0 :         return false;
      97             :       }
      98           0 :       throw new StorageException("unable to check permissions on change " + cd.getId(), e);
      99          76 :     }
     100             : 
     101          76 :     cd.cacheVisibleTo(user);
     102          76 :     return true;
     103             :   }
     104             : 
     105             :   @Override
     106             :   public int getCost() {
     107           5 :     return 1;
     108             :   }
     109             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750