LCOV - code coverage report
Current view: top level - server/restapi/account - SetDiffPreferences.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 13 15 86.7 %
Date: 2022-11-19 15:00:39 Functions: 3 4 75.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.entities.Account;
      18             : import com.google.gerrit.extensions.client.DiffPreferencesInfo;
      19             : import com.google.gerrit.extensions.restapi.BadRequestException;
      20             : import com.google.gerrit.extensions.restapi.IdString;
      21             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      22             : import com.google.gerrit.extensions.restapi.Response;
      23             : import com.google.gerrit.extensions.restapi.RestApiException;
      24             : import com.google.gerrit.extensions.restapi.RestModifyView;
      25             : import com.google.gerrit.server.CurrentUser;
      26             : import com.google.gerrit.server.UserInitiated;
      27             : import com.google.gerrit.server.account.AccountResource;
      28             : import com.google.gerrit.server.account.AccountState;
      29             : import com.google.gerrit.server.account.AccountsUpdate;
      30             : import com.google.gerrit.server.permissions.GlobalPermission;
      31             : import com.google.gerrit.server.permissions.PermissionBackend;
      32             : import com.google.gerrit.server.permissions.PermissionBackendException;
      33             : import com.google.inject.Inject;
      34             : import com.google.inject.Provider;
      35             : import com.google.inject.Singleton;
      36             : import java.io.IOException;
      37             : import org.eclipse.jgit.errors.ConfigInvalidException;
      38             : import org.eclipse.jgit.errors.RepositoryNotFoundException;
      39             : 
      40             : /**
      41             :  * REST endpoint to set diff preferences for an account.
      42             :  *
      43             :  * <p>This REST endpoint handles {@code PUT /accounts/<account-identifier>/preferences.diff}
      44             :  * requests.
      45             :  *
      46             :  * <p>General preferences can be set by {@link SetPreferences} and edit preferences can be set by
      47             :  * {@link SetEditPreferences}.
      48             :  *
      49             :  * <p>Default diff preferences that apply for all accounts can be set by {@link
      50             :  * com.google.gerrit.server.restapi.config.SetDiffPreferences}.
      51             :  */
      52             : @Singleton
      53             : public class SetDiffPreferences implements RestModifyView<AccountResource, DiffPreferencesInfo> {
      54             :   private final Provider<CurrentUser> self;
      55             :   private final PermissionBackend permissionBackend;
      56             :   private final Provider<AccountsUpdate> accountsUpdateProvider;
      57             : 
      58             :   @Inject
      59             :   SetDiffPreferences(
      60             :       Provider<CurrentUser> self,
      61             :       PermissionBackend permissionBackend,
      62         148 :       @UserInitiated Provider<AccountsUpdate> accountsUpdateProvider) {
      63         148 :     this.self = self;
      64         148 :     this.permissionBackend = permissionBackend;
      65         148 :     this.accountsUpdateProvider = accountsUpdateProvider;
      66         148 :   }
      67             : 
      68             :   @Override
      69             :   public Response<DiffPreferencesInfo> apply(AccountResource rsrc, DiffPreferencesInfo input)
      70             :       throws RestApiException, ConfigInvalidException, RepositoryNotFoundException, IOException,
      71             :           PermissionBackendException {
      72           1 :     if (!self.get().hasSameAccountId(rsrc.getUser())) {
      73           0 :       permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
      74             :     }
      75             : 
      76           1 :     if (input == null) {
      77           0 :       throw new BadRequestException("input must be provided");
      78             :     }
      79             : 
      80           1 :     Account.Id id = rsrc.getUser().getAccountId();
      81           1 :     return Response.ok(
      82             :         accountsUpdateProvider
      83           1 :             .get()
      84           1 :             .update("Set Diff Preferences via API", id, u -> u.setDiffPreferences(input))
      85           1 :             .map(AccountState::diffPreferences)
      86           1 :             .orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString()))));
      87             :   }
      88             : }

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