LCOV - code coverage report
Current view: top level - server/restapi/account - Capabilities.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 7 28 25.0 %
Date: 2022-11-19 15:00:39 Functions: 2 5 40.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.restapi.account;
      16             : 
      17             : import static com.google.gerrit.server.permissions.DefaultPermissionMappings.globalOrPluginPermissionName;
      18             : import static com.google.gerrit.server.permissions.DefaultPermissionMappings.globalPermission;
      19             : 
      20             : import com.google.gerrit.extensions.api.access.GlobalOrPluginPermission;
      21             : import com.google.gerrit.extensions.api.access.PluginPermission;
      22             : import com.google.gerrit.extensions.registration.DynamicMap;
      23             : import com.google.gerrit.extensions.restapi.AuthException;
      24             : import com.google.gerrit.extensions.restapi.ChildCollection;
      25             : import com.google.gerrit.extensions.restapi.IdString;
      26             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      27             : import com.google.gerrit.extensions.restapi.RestView;
      28             : import com.google.gerrit.server.CurrentUser;
      29             : import com.google.gerrit.server.IdentifiedUser;
      30             : import com.google.gerrit.server.account.AccountResource;
      31             : import com.google.gerrit.server.account.AccountResource.Capability;
      32             : import com.google.gerrit.server.permissions.GlobalPermission;
      33             : import com.google.gerrit.server.permissions.PermissionBackend;
      34             : import com.google.gerrit.server.permissions.PermissionBackendException;
      35             : import com.google.inject.Inject;
      36             : import com.google.inject.Provider;
      37             : import com.google.inject.Singleton;
      38             : import java.util.Optional;
      39             : 
      40             : @Singleton
      41             : public class Capabilities implements ChildCollection<AccountResource, AccountResource.Capability> {
      42             :   private final Provider<CurrentUser> self;
      43             :   private final PermissionBackend permissionBackend;
      44             :   private final DynamicMap<RestView<AccountResource.Capability>> views;
      45             :   private final Provider<GetCapabilities> get;
      46             : 
      47             :   @Inject
      48             :   Capabilities(
      49             :       Provider<CurrentUser> self,
      50             :       PermissionBackend permissionBackend,
      51             :       DynamicMap<RestView<AccountResource.Capability>> views,
      52         138 :       Provider<GetCapabilities> get) {
      53         138 :     this.self = self;
      54         138 :     this.permissionBackend = permissionBackend;
      55         138 :     this.views = views;
      56         138 :     this.get = get;
      57         138 :   }
      58             : 
      59             :   @Override
      60             :   public GetCapabilities list() throws ResourceNotFoundException {
      61           2 :     return get.get();
      62             :   }
      63             : 
      64             :   @Override
      65             :   public Capability parse(AccountResource parent, IdString id)
      66             :       throws ResourceNotFoundException, AuthException, PermissionBackendException {
      67           0 :     permissionBackend.checkUsesDefaultCapabilities();
      68           0 :     IdentifiedUser target = parent.getUser();
      69           0 :     if (!self.get().hasSameAccountId(target)) {
      70           0 :       permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
      71             :     }
      72             : 
      73           0 :     GlobalOrPluginPermission perm = parse(id);
      74           0 :     if (permissionBackend.absentUser(target.getAccountId()).test(perm)) {
      75           0 :       return new AccountResource.Capability(target, globalOrPluginPermissionName(perm));
      76             :     }
      77           0 :     throw new ResourceNotFoundException(id);
      78             :   }
      79             : 
      80             :   private GlobalOrPluginPermission parse(IdString id) throws ResourceNotFoundException {
      81           0 :     String name = id.get();
      82           0 :     Optional<GlobalPermission> perm = globalPermission(name);
      83           0 :     if (perm.isPresent()) {
      84           0 :       return perm.get();
      85             :     }
      86             : 
      87           0 :     int dash = name.lastIndexOf('-');
      88           0 :     if (dash < 0) {
      89           0 :       throw new ResourceNotFoundException(id);
      90             :     }
      91             : 
      92           0 :     String pluginName = name.substring(0, dash);
      93           0 :     String capability = name.substring(dash + 1);
      94           0 :     if (pluginName.isEmpty() || capability.isEmpty()) {
      95           0 :       throw new ResourceNotFoundException(id);
      96             :     }
      97           0 :     return new PluginPermission(pluginName, capability);
      98             :   }
      99             : 
     100             :   @Override
     101             :   public DynamicMap<RestView<Capability>> views() {
     102           0 :     return views;
     103             :   }
     104             : }

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