LCOV - code coverage report
Current view: top level - server/restapi/account - PutName.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 22 24 91.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.AccountFieldName;
      20             : import com.google.gerrit.extensions.common.NameInput;
      21             : import com.google.gerrit.extensions.restapi.AuthException;
      22             : import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
      23             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      24             : import com.google.gerrit.extensions.restapi.Response;
      25             : import com.google.gerrit.extensions.restapi.RestModifyView;
      26             : import com.google.gerrit.server.CurrentUser;
      27             : import com.google.gerrit.server.IdentifiedUser;
      28             : import com.google.gerrit.server.ServerInitiated;
      29             : import com.google.gerrit.server.account.AccountResource;
      30             : import com.google.gerrit.server.account.AccountState;
      31             : import com.google.gerrit.server.account.AccountsUpdate;
      32             : import com.google.gerrit.server.account.Realm;
      33             : import com.google.gerrit.server.account.externalids.ExternalIds;
      34             : import com.google.gerrit.server.permissions.GlobalPermission;
      35             : import com.google.gerrit.server.permissions.PermissionBackend;
      36             : import com.google.gerrit.server.permissions.PermissionBackendException;
      37             : import com.google.inject.Inject;
      38             : import com.google.inject.Provider;
      39             : import com.google.inject.Singleton;
      40             : import java.io.IOException;
      41             : import org.eclipse.jgit.errors.ConfigInvalidException;
      42             : 
      43             : /**
      44             :  * REST endpoint to set the full name of an account.
      45             :  *
      46             :  * <p>This REST endpoint handles {@code PUT /accounts/<account-identifier>/name} requests.
      47             :  *
      48             :  * <p>Whether a full name can be set depends on whether the used {@link Realm} supports this.
      49             :  */
      50             : @Singleton
      51             : public class PutName implements RestModifyView<AccountResource, NameInput> {
      52             :   private final Provider<CurrentUser> self;
      53             :   private final Realm realm;
      54             :   private final PermissionBackend permissionBackend;
      55             :   private final ExternalIds externalIds;
      56             :   private final Provider<AccountsUpdate> accountsUpdateProvider;
      57             : 
      58             :   @Inject
      59             :   PutName(
      60             :       Provider<CurrentUser> self,
      61             :       Realm realm,
      62             :       PermissionBackend permissionBackend,
      63             :       ExternalIds externalIds,
      64         148 :       @ServerInitiated Provider<AccountsUpdate> accountsUpdateProvider) {
      65         148 :     this.self = self;
      66         148 :     this.realm = realm;
      67         148 :     this.permissionBackend = permissionBackend;
      68         148 :     this.externalIds = externalIds;
      69         148 :     this.accountsUpdateProvider = accountsUpdateProvider;
      70         148 :   }
      71             : 
      72             :   @Override
      73             :   public Response<String> apply(AccountResource rsrc, NameInput input)
      74             :       throws AuthException, MethodNotAllowedException, ResourceNotFoundException, IOException,
      75             :           PermissionBackendException, ConfigInvalidException {
      76           2 :     if (!self.get().hasSameAccountId(rsrc.getUser())) {
      77           1 :       permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
      78             :     }
      79           2 :     return apply(rsrc.getUser(), input);
      80             :   }
      81             : 
      82             :   public Response<String> apply(IdentifiedUser user, NameInput input)
      83             :       throws MethodNotAllowedException, ResourceNotFoundException, IOException,
      84             :           ConfigInvalidException {
      85           2 :     if (input == null) {
      86           1 :       input = new NameInput();
      87             :     }
      88             : 
      89           2 :     Account.Id accountId = user.getAccountId();
      90           2 :     if (realm.accountBelongsToRealm(externalIds.byAccount(accountId))
      91           0 :         && !realm.allowsEdit(AccountFieldName.FULL_NAME)) {
      92           0 :       throw new MethodNotAllowedException("realm does not allow editing name");
      93             :     }
      94             : 
      95           2 :     String newName = input.name;
      96           2 :     AccountState accountState =
      97             :         accountsUpdateProvider
      98           2 :             .get()
      99           2 :             .update("Set Full Name via API", accountId, u -> u.setFullName(newName))
     100           2 :             .orElseThrow(() -> new ResourceNotFoundException("account not found"));
     101           2 :     return Strings.isNullOrEmpty(accountState.account().fullName())
     102           1 :         ? Response.none()
     103           1 :         : Response.ok(accountState.account().fullName());
     104             :   }
     105             : }

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