LCOV - code coverage report
Current view: top level - server/query/approval - UserInPredicate.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 14 21 66.7 %
Date: 2022-11-19 15:00:39 Functions: 3 6 50.0 %

          Line data    Source code
       1             : // Copyright (C) 2021 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.approval;
      16             : 
      17             : import com.google.gerrit.entities.Account;
      18             : import com.google.gerrit.entities.AccountGroup;
      19             : import com.google.gerrit.entities.PatchSet;
      20             : import com.google.gerrit.index.query.Predicate;
      21             : import com.google.gerrit.server.IdentifiedUser;
      22             : import com.google.inject.Inject;
      23             : import com.google.inject.assistedinject.Assisted;
      24             : import java.util.Collection;
      25             : import java.util.Objects;
      26             : 
      27             : /** Predicate that matches group memberships of users such as uploader or approver. */
      28             : public class UserInPredicate extends ApprovalPredicate {
      29             :   interface Factory {
      30             :     UserInPredicate create(Field field, AccountGroup.UUID group);
      31             :   }
      32             : 
      33         152 :   enum Field {
      34         152 :     UPLOADER,
      35         152 :     APPROVER
      36             :   }
      37             : 
      38             :   private final IdentifiedUser.GenericFactory identifiedUserFactory;
      39             :   private final Field field;
      40             :   private final AccountGroup.UUID group;
      41             : 
      42             :   @Inject
      43             :   UserInPredicate(
      44             :       IdentifiedUser.GenericFactory identifiedUserFactory,
      45             :       @Assisted Field field,
      46           5 :       @Assisted AccountGroup.UUID group) {
      47           5 :     this.identifiedUserFactory = identifiedUserFactory;
      48           5 :     this.field = field;
      49           5 :     this.group = group;
      50           5 :   }
      51             : 
      52             :   @Override
      53             :   public boolean match(ApprovalContext ctx) {
      54             :     Account.Id accountId;
      55           2 :     if (field == Field.UPLOADER) {
      56           2 :       PatchSet patchSet = ctx.targetPatchSet();
      57           2 :       accountId = patchSet.uploader();
      58           2 :     } else if (field == Field.APPROVER) {
      59           2 :       accountId = ctx.approverId();
      60             :     } else {
      61           0 :       throw new IllegalStateException("unknown field in group membership check: " + field);
      62             :     }
      63           2 :     return identifiedUserFactory.create(accountId).getEffectiveGroups().contains(group);
      64             :   }
      65             : 
      66             :   @Override
      67             :   public Predicate<ApprovalContext> copy(
      68             :       Collection<? extends Predicate<ApprovalContext>> children) {
      69           0 :     return new UserInPredicate(identifiedUserFactory, field, group);
      70             :   }
      71             : 
      72             :   @Override
      73             :   public int hashCode() {
      74           0 :     return Objects.hash(field, group);
      75             :   }
      76             : 
      77             :   @Override
      78             :   public boolean equals(Object other) {
      79           0 :     if (!(other instanceof UserInPredicate)) {
      80           0 :       return false;
      81             :     }
      82           0 :     UserInPredicate o = (UserInPredicate) other;
      83           0 :     return Objects.equals(o.field, field) && Objects.equals(o.group, group);
      84             :   }
      85             : }

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