Line data Source code
1 : // Copyright (C) 2017 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.extensions.api.accounts.StatusInput; 19 : import com.google.gerrit.extensions.restapi.AuthException; 20 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 21 : import com.google.gerrit.extensions.restapi.Response; 22 : import com.google.gerrit.extensions.restapi.RestModifyView; 23 : import com.google.gerrit.server.CurrentUser; 24 : import com.google.gerrit.server.IdentifiedUser; 25 : import com.google.gerrit.server.ServerInitiated; 26 : import com.google.gerrit.server.account.AccountResource; 27 : import com.google.gerrit.server.account.AccountState; 28 : import com.google.gerrit.server.account.AccountsUpdate; 29 : import com.google.gerrit.server.permissions.GlobalPermission; 30 : import com.google.gerrit.server.permissions.PermissionBackend; 31 : import com.google.gerrit.server.permissions.PermissionBackendException; 32 : import com.google.inject.Inject; 33 : import com.google.inject.Provider; 34 : import com.google.inject.Singleton; 35 : import java.io.IOException; 36 : import org.eclipse.jgit.errors.ConfigInvalidException; 37 : 38 : /** 39 : * REST endpoint to set the status of an account. 40 : * 41 : * <p>This REST endpoint handles {@code PUT /accounts/<account-identifier>/status} requests. 42 : * 43 : * <p>The account status is a free-form text that a user can set for the own account (e.g. the 'OOO' 44 : * string is often used to signal that the user is out-of-office). 45 : */ 46 : @Singleton 47 : public class PutStatus implements RestModifyView<AccountResource, StatusInput> { 48 : private final Provider<CurrentUser> self; 49 : private final PermissionBackend permissionBackend; 50 : private final Provider<AccountsUpdate> accountsUpdateProvider; 51 : 52 : @Inject 53 : PutStatus( 54 : Provider<CurrentUser> self, 55 : PermissionBackend permissionBackend, 56 148 : @ServerInitiated Provider<AccountsUpdate> accountsUpdateProvider) { 57 148 : this.self = self; 58 148 : this.permissionBackend = permissionBackend; 59 148 : this.accountsUpdateProvider = accountsUpdateProvider; 60 148 : } 61 : 62 : @Override 63 : public Response<String> apply(AccountResource rsrc, StatusInput input) 64 : throws AuthException, ResourceNotFoundException, IOException, PermissionBackendException, 65 : ConfigInvalidException { 66 1 : if (!self.get().hasSameAccountId(rsrc.getUser())) { 67 1 : permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT); 68 : } 69 1 : return apply(rsrc.getUser(), input); 70 : } 71 : 72 : public Response<String> apply(IdentifiedUser user, StatusInput input) 73 : throws ResourceNotFoundException, IOException, ConfigInvalidException { 74 1 : if (input == null) { 75 0 : input = new StatusInput(); 76 : } 77 : 78 1 : String newStatus = input.status; 79 1 : AccountState accountState = 80 : accountsUpdateProvider 81 1 : .get() 82 1 : .update("Set Status via API", user.getAccountId(), u -> u.setStatus(newStatus)) 83 1 : .orElseThrow(() -> new ResourceNotFoundException("account not found")); 84 1 : return Strings.isNullOrEmpty(accountState.account().status()) 85 1 : ? Response.none() 86 1 : : Response.ok(accountState.account().status()); 87 : } 88 : }