LCOV - code coverage report
Current view: top level - server/restapi/account - SetEditPreferences.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) 2014 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.EditPreferencesInfo;
      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 edit preferences for an account.
      42             :  *
      43             :  * <p>This REST endpoint handles {@code PUT /accounts/<account-identifier>/preferences.edit}
      44             :  * requests.
      45             :  *
      46             :  * <p>General preferences can be set by {@link SetPreferences} and diff preferences can be set by
      47             :  * {@link SetDiffPreferences}.
      48             :  *
      49             :  * <p>Default edit preferences that apply for all accounts can be set by {@link
      50             :  * com.google.gerrit.server.restapi.config.SetEditPreferences}.
      51             :  */
      52             : @Singleton
      53             : public class SetEditPreferences implements RestModifyView<AccountResource, EditPreferencesInfo> {
      54             : 
      55             :   private final Provider<CurrentUser> self;
      56             :   private final PermissionBackend permissionBackend;
      57             :   private final Provider<AccountsUpdate> accountsUpdateProvider;
      58             : 
      59             :   @Inject
      60             :   SetEditPreferences(
      61             :       Provider<CurrentUser> self,
      62             :       PermissionBackend permissionBackend,
      63         148 :       @UserInitiated Provider<AccountsUpdate> accountsUpdateProvider) {
      64         148 :     this.self = self;
      65         148 :     this.permissionBackend = permissionBackend;
      66         148 :     this.accountsUpdateProvider = accountsUpdateProvider;
      67         148 :   }
      68             : 
      69             :   @Override
      70             :   public Response<EditPreferencesInfo> apply(AccountResource rsrc, EditPreferencesInfo input)
      71             :       throws RestApiException, RepositoryNotFoundException, IOException, ConfigInvalidException,
      72             :           PermissionBackendException {
      73           1 :     if (!self.get().hasSameAccountId(rsrc.getUser())) {
      74           0 :       permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
      75             :     }
      76             : 
      77           1 :     if (input == null) {
      78           0 :       throw new BadRequestException("input must be provided");
      79             :     }
      80             : 
      81           1 :     Account.Id id = rsrc.getUser().getAccountId();
      82           1 :     return Response.ok(
      83             :         accountsUpdateProvider
      84           1 :             .get()
      85           1 :             .update("Set Edit Preferences via API", id, u -> u.setEditPreferences(input))
      86           1 :             .map(AccountState::editPreferences)
      87           1 :             .orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString()))));
      88             :   }
      89             : }

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