LCOV - code coverage report
Current view: top level - server/account - InternalGroupBackend.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 21 26 80.8 %
Date: 2022-11-19 15:00:39 Functions: 9 9 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 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.account;
      16             : 
      17             : import static java.util.stream.Collectors.toList;
      18             : 
      19             : import com.google.common.collect.ImmutableList;
      20             : import com.google.gerrit.common.Nullable;
      21             : import com.google.gerrit.entities.AccountGroup;
      22             : import com.google.gerrit.entities.GroupDescription;
      23             : import com.google.gerrit.entities.GroupReference;
      24             : import com.google.gerrit.entities.InternalGroup;
      25             : import com.google.gerrit.server.CurrentUser;
      26             : import com.google.gerrit.server.group.InternalGroupDescription;
      27             : import com.google.gerrit.server.group.db.Groups;
      28             : import com.google.gerrit.server.group.db.GroupsNoteDbConsistencyChecker;
      29             : import com.google.gerrit.server.project.ProjectState;
      30             : import com.google.inject.Inject;
      31             : import com.google.inject.Singleton;
      32             : import java.io.IOException;
      33             : import java.util.Collection;
      34             : import java.util.Optional;
      35             : import org.eclipse.jgit.errors.ConfigInvalidException;
      36             : import org.eclipse.jgit.lib.ObjectId;
      37             : 
      38             : /** Implementation of GroupBackend for the internal group system. */
      39             : @Singleton
      40             : public class InternalGroupBackend implements GroupBackend {
      41             :   private final GroupControl.Factory groupControlFactory;
      42             :   private final GroupCache groupCache;
      43             :   private final Groups groups;
      44             :   private final IncludingGroupMembership.Factory groupMembershipFactory;
      45             : 
      46             :   @Inject
      47             :   InternalGroupBackend(
      48             :       GroupControl.Factory groupControlFactory,
      49             :       GroupCache groupCache,
      50             :       Groups groups,
      51         150 :       IncludingGroupMembership.Factory groupMembershipFactory) {
      52         150 :     this.groupControlFactory = groupControlFactory;
      53         150 :     this.groupCache = groupCache;
      54         150 :     this.groups = groups;
      55         150 :     this.groupMembershipFactory = groupMembershipFactory;
      56         150 :   }
      57             : 
      58             :   @Override
      59             :   public boolean handles(AccountGroup.UUID uuid) {
      60             :     // See AccountGroup.isInternalGroup
      61         150 :     return ObjectId.isId(uuid.get()); // [0-9a-f]{40};
      62             :   }
      63             : 
      64             :   @Nullable
      65             :   @Override
      66             :   public GroupDescription.Internal get(AccountGroup.UUID uuid) {
      67          56 :     if (!handles(uuid)) {
      68           0 :       return null;
      69             :     }
      70             : 
      71          56 :     return groupCache.get(uuid).map(InternalGroupDescription::new).orElse(null);
      72             :   }
      73             : 
      74             :   @Override
      75             :   public Collection<GroupReference> suggest(String name, ProjectState project) {
      76             :     try {
      77          42 :       return groups
      78          42 :           .getAllGroupReferences()
      79          42 :           .filter(group -> startsWithIgnoreCase(group, name))
      80          42 :           .filter(this::isVisible)
      81          42 :           .collect(toList());
      82           0 :     } catch (IOException | ConfigInvalidException e) {
      83           0 :       return ImmutableList.of();
      84             :     }
      85             :   }
      86             : 
      87             :   private static boolean startsWithIgnoreCase(GroupReference group, String name) {
      88          42 :     return group.getName().regionMatches(true, 0, name, 0, name.length());
      89             :   }
      90             : 
      91             :   private boolean isVisible(GroupReference groupReference) {
      92          18 :     Optional<InternalGroup> group = groupCache.get(groupReference.getUUID());
      93          18 :     if (!group.isPresent()) {
      94             :       // groupRefs are read from group name notes. There is an inconsistency if this lookup fails.
      95           0 :       GroupsNoteDbConsistencyChecker.logFailToLoadFromGroupRefAsWarning(groupReference.getUUID());
      96           0 :       return false;
      97             :     }
      98          18 :     return groupControlFactory.controlFor(new InternalGroupDescription(group.get())).isVisible();
      99             :   }
     100             : 
     101             :   @Override
     102             :   public GroupMembership membershipsOf(CurrentUser user) {
     103         150 :     return groupMembershipFactory.create(user);
     104             :   }
     105             : 
     106             :   @Override
     107             :   public boolean isVisibleToAll(AccountGroup.UUID uuid) {
     108          37 :     GroupDescription.Internal g = get(uuid);
     109          37 :     return g != null && g.isVisibleToAll();
     110             :   }
     111             : }

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