LCOV - code coverage report
Current view: top level - server/restapi/account - SshKeys.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 18 22 81.8 %
Date: 2022-11-19 15:00:39 Functions: 4 5 80.0 %

          Line data    Source code
       1             : // Copyright (C) 2013 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 com.google.gerrit.extensions.registration.DynamicMap;
      18             : import com.google.gerrit.extensions.restapi.AuthException;
      19             : import com.google.gerrit.extensions.restapi.ChildCollection;
      20             : import com.google.gerrit.extensions.restapi.IdString;
      21             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      22             : import com.google.gerrit.extensions.restapi.RestView;
      23             : import com.google.gerrit.server.CurrentUser;
      24             : import com.google.gerrit.server.IdentifiedUser;
      25             : import com.google.gerrit.server.account.AccountResource;
      26             : import com.google.gerrit.server.account.AccountSshKey;
      27             : import com.google.gerrit.server.account.VersionedAuthorizedKeys;
      28             : import com.google.gerrit.server.permissions.GlobalPermission;
      29             : import com.google.gerrit.server.permissions.PermissionBackend;
      30             : import com.google.gerrit.server.permissions.PermissionBackendException;
      31             : import com.google.inject.Inject;
      32             : import com.google.inject.Provider;
      33             : import com.google.inject.Singleton;
      34             : import java.io.IOException;
      35             : import org.eclipse.jgit.errors.ConfigInvalidException;
      36             : 
      37             : @Singleton
      38             : public class SshKeys implements ChildCollection<AccountResource, AccountResource.SshKey> {
      39             :   private final DynamicMap<RestView<AccountResource.SshKey>> views;
      40             :   private final GetSshKeys list;
      41             :   private final Provider<CurrentUser> self;
      42             :   private final PermissionBackend permissionBackend;
      43             :   private final VersionedAuthorizedKeys.Accessor authorizedKeys;
      44             : 
      45             :   @Inject
      46             :   SshKeys(
      47             :       DynamicMap<RestView<AccountResource.SshKey>> views,
      48             :       GetSshKeys list,
      49             :       Provider<CurrentUser> self,
      50             :       PermissionBackend permissionBackend,
      51         148 :       VersionedAuthorizedKeys.Accessor authorizedKeys) {
      52         148 :     this.views = views;
      53         148 :     this.list = list;
      54         148 :     this.self = self;
      55         148 :     this.permissionBackend = permissionBackend;
      56         148 :     this.authorizedKeys = authorizedKeys;
      57         148 :   }
      58             : 
      59             :   @Override
      60             :   public RestView<AccountResource> list() {
      61           0 :     return list;
      62             :   }
      63             : 
      64             :   @Override
      65             :   public AccountResource.SshKey parse(AccountResource rsrc, IdString id)
      66             :       throws ResourceNotFoundException, IOException, ConfigInvalidException,
      67             :           PermissionBackendException {
      68           3 :     if (!self.get().hasSameAccountId(rsrc.getUser())) {
      69             :       try {
      70           2 :         permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
      71           2 :       } catch (AuthException e) {
      72             :         // If lacking MODIFY_ACCOUNT claim the resource does not exist.
      73           2 :         throw new ResourceNotFoundException(id, e);
      74           2 :       }
      75             :     }
      76           3 :     return parse(rsrc.getUser(), id);
      77             :   }
      78             : 
      79             :   public AccountResource.SshKey parse(IdentifiedUser user, IdString id)
      80             :       throws ResourceNotFoundException, IOException, ConfigInvalidException {
      81             :     try {
      82           3 :       int seq = Integer.parseInt(id.get(), 10);
      83           3 :       AccountSshKey sshKey = authorizedKeys.getKey(user.getAccountId(), seq);
      84           3 :       if (sshKey == null) {
      85           0 :         throw new ResourceNotFoundException(id);
      86             :       }
      87           3 :       return new AccountResource.SshKey(user, sshKey);
      88           0 :     } catch (NumberFormatException e) {
      89           0 :       throw new ResourceNotFoundException(id, e);
      90             :     }
      91             :   }
      92             : 
      93             :   @Override
      94             :   public DynamicMap<RestView<AccountResource.SshKey>> views() {
      95           2 :     return views;
      96             :   }
      97             : }

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