LCOV - code coverage report
Current view: top level - server/restapi/account - GetDiffPreferences.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 11 12 91.7 %
Date: 2022-11-19 15:00:39 Functions: 2 3 66.7 %

          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.entities.Account;
      18             : import com.google.gerrit.extensions.client.DiffPreferencesInfo;
      19             : import com.google.gerrit.extensions.restapi.IdString;
      20             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      21             : import com.google.gerrit.extensions.restapi.Response;
      22             : import com.google.gerrit.extensions.restapi.RestApiException;
      23             : import com.google.gerrit.extensions.restapi.RestReadView;
      24             : import com.google.gerrit.server.CurrentUser;
      25             : import com.google.gerrit.server.account.AccountCache;
      26             : import com.google.gerrit.server.account.AccountResource;
      27             : import com.google.gerrit.server.account.AccountState;
      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             : /**
      38             :  * REST endpoint to get the diff preferences of an account.
      39             :  *
      40             :  * <p>This REST endpoint handles {@code GET /accounts/<account-identifier>/preferences.diff}
      41             :  * requests.
      42             :  *
      43             :  * <p>General preferences can be retrieved by {@link GetPreferences} and edit preferences can be
      44             :  * retrieved by {@link GetEditPreferences}.
      45             :  *
      46             :  * <p>Default diff preferences that apply for all accounts can be retrieved by {@link
      47             :  * com.google.gerrit.server.restapi.config.GetDiffPreferences}.
      48             :  */
      49             : @Singleton
      50             : public class GetDiffPreferences implements RestReadView<AccountResource> {
      51             :   private final Provider<CurrentUser> self;
      52             :   private final PermissionBackend permissionBackend;
      53             :   private final AccountCache accountCache;
      54             : 
      55             :   @Inject
      56             :   GetDiffPreferences(
      57         148 :       Provider<CurrentUser> self, PermissionBackend permissionBackend, AccountCache accountCache) {
      58         148 :     this.self = self;
      59         148 :     this.permissionBackend = permissionBackend;
      60         148 :     this.accountCache = accountCache;
      61         148 :   }
      62             : 
      63             :   @Override
      64             :   public Response<DiffPreferencesInfo> apply(AccountResource rsrc)
      65             :       throws RestApiException, ConfigInvalidException, IOException, PermissionBackendException {
      66           1 :     if (!self.get().hasSameAccountId(rsrc.getUser())) {
      67           0 :       permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
      68             :     }
      69             : 
      70           1 :     Account.Id id = rsrc.getUser().getAccountId();
      71           1 :     return Response.ok(
      72             :         accountCache
      73           1 :             .get(id)
      74           1 :             .map(AccountState::diffPreferences)
      75           1 :             .orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString()))));
      76             :   }
      77             : }

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