LCOV - code coverage report
Current view: top level - server/restapi/account - SetPreferences.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 22 23 95.7 %
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.common.base.Strings;
      18             : import com.google.gerrit.entities.Account;
      19             : import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
      20             : import com.google.gerrit.extensions.config.DownloadScheme;
      21             : import com.google.gerrit.extensions.registration.DynamicMap;
      22             : import com.google.gerrit.extensions.registration.Extension;
      23             : import com.google.gerrit.extensions.restapi.BadRequestException;
      24             : import com.google.gerrit.extensions.restapi.IdString;
      25             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      26             : import com.google.gerrit.extensions.restapi.Response;
      27             : import com.google.gerrit.extensions.restapi.RestApiException;
      28             : import com.google.gerrit.extensions.restapi.RestModifyView;
      29             : import com.google.gerrit.server.CurrentUser;
      30             : import com.google.gerrit.server.UserInitiated;
      31             : import com.google.gerrit.server.account.AccountResource;
      32             : import com.google.gerrit.server.account.AccountState;
      33             : import com.google.gerrit.server.account.AccountsUpdate;
      34             : import com.google.gerrit.server.account.StoredPreferences;
      35             : import com.google.gerrit.server.permissions.GlobalPermission;
      36             : import com.google.gerrit.server.permissions.PermissionBackend;
      37             : import com.google.gerrit.server.permissions.PermissionBackendException;
      38             : import com.google.inject.Inject;
      39             : import com.google.inject.Provider;
      40             : import com.google.inject.Singleton;
      41             : import java.io.IOException;
      42             : import org.eclipse.jgit.errors.ConfigInvalidException;
      43             : 
      44             : /**
      45             :  * REST endpoint to set general preferences for an account.
      46             :  *
      47             :  * <p>This REST endpoint handles {@code PUT /accounts/<account-identifier>/preferences} requests.
      48             :  *
      49             :  * <p>Diff preferences can be set by {@link SetDiffPreferences} and edit preferences can be set by
      50             :  * {@link SetEditPreferences}.
      51             :  *
      52             :  * <p>Default general preferences that apply for all accounts can be set by {@link
      53             :  * com.google.gerrit.server.restapi.config.SetPreferences}.
      54             :  */
      55             : @Singleton
      56             : public class SetPreferences implements RestModifyView<AccountResource, GeneralPreferencesInfo> {
      57             :   private final Provider<CurrentUser> self;
      58             :   private final PermissionBackend permissionBackend;
      59             :   private final Provider<AccountsUpdate> accountsUpdateProvider;
      60             :   private final DynamicMap<DownloadScheme> downloadSchemes;
      61             : 
      62             :   @Inject
      63             :   SetPreferences(
      64             :       Provider<CurrentUser> self,
      65             :       PermissionBackend permissionBackend,
      66             :       @UserInitiated Provider<AccountsUpdate> accountsUpdateProvider,
      67         148 :       DynamicMap<DownloadScheme> downloadSchemes) {
      68         148 :     this.self = self;
      69         148 :     this.permissionBackend = permissionBackend;
      70         148 :     this.accountsUpdateProvider = accountsUpdateProvider;
      71         148 :     this.downloadSchemes = downloadSchemes;
      72         148 :   }
      73             : 
      74             :   @Override
      75             :   public Response<GeneralPreferencesInfo> apply(AccountResource rsrc, GeneralPreferencesInfo input)
      76             :       throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
      77           8 :     if (!self.get().hasSameAccountId(rsrc.getUser())) {
      78           2 :       permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
      79             :     }
      80             : 
      81           8 :     checkDownloadScheme(input.downloadScheme);
      82           8 :     StoredPreferences.validateMy(input.my);
      83           8 :     Account.Id id = rsrc.getUser().getAccountId();
      84             : 
      85           8 :     return Response.ok(
      86             :         accountsUpdateProvider
      87           8 :             .get()
      88           8 :             .update("Set General Preferences via API", id, u -> u.setGeneralPreferences(input))
      89           8 :             .map(AccountState::generalPreferences)
      90           8 :             .orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString()))));
      91             :   }
      92             : 
      93             :   private void checkDownloadScheme(String downloadScheme) throws BadRequestException {
      94           8 :     if (Strings.isNullOrEmpty(downloadScheme)) {
      95           8 :       return;
      96             :     }
      97             : 
      98           1 :     for (Extension<DownloadScheme> e : downloadSchemes) {
      99           1 :       if (e.getExportName().equals(downloadScheme) && e.getProvider().get().isEnabled()) {
     100           1 :         return;
     101             :       }
     102           0 :     }
     103           1 :     throw new BadRequestException("Unsupported download scheme: " + downloadScheme);
     104             :   }
     105             : }

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