LCOV - code coverage report
Current view: top level - server/restapi/account - PutDisplayName.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 5 20 25.0 %
Date: 2022-11-19 15:00:39 Functions: 1 4 25.0 %

          Line data    Source code
       1             : // Copyright (C) 2020 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.common.Nullable;
      19             : import com.google.gerrit.extensions.api.accounts.DisplayNameInput;
      20             : import com.google.gerrit.extensions.restapi.AuthException;
      21             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      22             : import com.google.gerrit.extensions.restapi.Response;
      23             : import com.google.gerrit.extensions.restapi.RestModifyView;
      24             : import com.google.gerrit.server.CurrentUser;
      25             : import com.google.gerrit.server.IdentifiedUser;
      26             : import com.google.gerrit.server.ServerInitiated;
      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             : 
      39             : /**
      40             :  * REST endpoint to set the display name of an account.
      41             :  *
      42             :  * <p>This REST endpoint handles {@code PUT /accounts/<account-identifier>/displayname} requests.
      43             :  *
      44             :  * <p>The display name is a free-form text that a user can set for their own account. It defines how
      45             :  * the user's name will be rendered in the UI in most screens. It is optional, and if not set, then
      46             :  * the UI falls back to whatever is configured as the default display name, e.g. the full name.
      47             :  */
      48             : @Singleton
      49             : public class PutDisplayName implements RestModifyView<AccountResource, DisplayNameInput> {
      50             :   private final Provider<CurrentUser> self;
      51             :   private final PermissionBackend permissionBackend;
      52             :   private final Provider<AccountsUpdate> accountsUpdateProvider;
      53             : 
      54             :   @Inject
      55             :   PutDisplayName(
      56             :       Provider<CurrentUser> self,
      57             :       PermissionBackend permissionBackend,
      58         148 :       @ServerInitiated Provider<AccountsUpdate> accountsUpdateProvider) {
      59         148 :     this.self = self;
      60         148 :     this.permissionBackend = permissionBackend;
      61         148 :     this.accountsUpdateProvider = accountsUpdateProvider;
      62         148 :   }
      63             : 
      64             :   @Override
      65             :   public Response<String> apply(AccountResource rsrc, @Nullable DisplayNameInput input)
      66             :       throws AuthException, ResourceNotFoundException, IOException, PermissionBackendException,
      67             :           ConfigInvalidException {
      68           0 :     IdentifiedUser user = rsrc.getUser();
      69           0 :     if (!self.get().hasSameAccountId(user)) {
      70           0 :       permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
      71             :     }
      72           0 :     if (input == null) {
      73           0 :       input = new DisplayNameInput();
      74             :     }
      75             : 
      76           0 :     String newDisplayName = input.displayName;
      77           0 :     AccountState accountState =
      78             :         accountsUpdateProvider
      79           0 :             .get()
      80           0 :             .update(
      81             :                 "Set Display Name via API",
      82           0 :                 user.getAccountId(),
      83           0 :                 u -> u.setDisplayName(newDisplayName))
      84           0 :             .orElseThrow(() -> new ResourceNotFoundException("account not found"));
      85           0 :     return Strings.isNullOrEmpty(accountState.account().displayName())
      86           0 :         ? Response.none()
      87           0 :         : Response.ok(accountState.account().displayName());
      88             :   }
      89             : }

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