LCOV - code coverage report
Current view: top level - server/config - AdministrateServerGroupsProvider.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 9 15 60.0 %
Date: 2022-11-19 15:00:39 Functions: 3 3 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.config;
      16             : 
      17             : import com.google.common.collect.ImmutableSet;
      18             : import com.google.common.flogger.FluentLogger;
      19             : import com.google.gerrit.entities.GroupReference;
      20             : import com.google.gerrit.entities.PermissionRule;
      21             : import com.google.gerrit.server.account.GroupBackend;
      22             : import com.google.gerrit.server.account.GroupBackends;
      23             : import com.google.gerrit.server.util.RequestContext;
      24             : import com.google.gerrit.server.util.ServerRequestContext;
      25             : import com.google.gerrit.server.util.ThreadLocalRequestContext;
      26             : import com.google.inject.Inject;
      27             : import com.google.inject.Provider;
      28             : import org.eclipse.jgit.lib.Config;
      29             : 
      30             : /** Loads {@link AdministrateServerGroups} from {@code gerrit.config}. */
      31             : public class AdministrateServerGroupsProvider implements Provider<ImmutableSet<GroupReference>> {
      32         151 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      33             : 
      34             :   private final ImmutableSet<GroupReference> groups;
      35             : 
      36             :   @Inject
      37             :   public AdministrateServerGroupsProvider(
      38             :       GroupBackend groupBackend,
      39             :       @GerritServerConfig Config config,
      40             :       ThreadLocalRequestContext threadContext,
      41         151 :       ServerRequestContext serverCtx) {
      42         151 :     RequestContext ctx = threadContext.setContext(serverCtx);
      43             :     try {
      44         151 :       ImmutableSet.Builder<GroupReference> builder = ImmutableSet.builder();
      45         151 :       for (String value : config.getStringList("capability", null, "administrateServer")) {
      46           0 :         PermissionRule rule = PermissionRule.fromString(value, false);
      47           0 :         String name = rule.getGroup().getName();
      48           0 :         GroupReference g = GroupBackends.findBestSuggestion(groupBackend, name);
      49           0 :         if (g != null) {
      50           0 :           builder.add(g);
      51             :         } else {
      52           0 :           logger.atWarning().log("Group \"%s\" not available, skipping.", name);
      53             :         }
      54             :       }
      55         151 :       groups = builder.build();
      56             :     } finally {
      57         151 :       threadContext.setContext(ctx);
      58             :     }
      59         151 :   }
      60             : 
      61             :   @Override
      62             :   public ImmutableSet<GroupReference> get() {
      63         151 :     return groups;
      64             :   }
      65             : }

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