Line data Source code
1 : // Copyright (C) 2016 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.extensions.common.Input; 18 : import com.google.gerrit.extensions.restapi.AuthException; 19 : import com.google.gerrit.extensions.restapi.Response; 20 : import com.google.gerrit.extensions.restapi.RestModifyView; 21 : import com.google.gerrit.server.CurrentUser; 22 : import com.google.gerrit.server.account.AccountResource; 23 : import com.google.gerrit.server.index.account.AccountIndexer; 24 : import com.google.gerrit.server.permissions.GlobalPermission; 25 : import com.google.gerrit.server.permissions.PermissionBackend; 26 : import com.google.gerrit.server.permissions.PermissionBackendException; 27 : import com.google.inject.Inject; 28 : import com.google.inject.Provider; 29 : import com.google.inject.Singleton; 30 : import java.io.IOException; 31 : 32 : /** 33 : * REST endpoint to (re)index an account. 34 : * 35 : * <p>This REST endpoint handles {@code POST /accounts/<account-identifier>/index} requests. 36 : * 37 : * <p>If the document of an account in the account index is stale, this REST endpoint can be used to 38 : * update the index. 39 : */ 40 : @Singleton 41 : public class Index implements RestModifyView<AccountResource, Input> { 42 : 43 : private final Provider<AccountIndexer> accountIndexer; 44 : private final PermissionBackend permissionBackend; 45 : private final Provider<CurrentUser> self; 46 : 47 : @Inject 48 : Index( 49 : Provider<AccountIndexer> accountIndexer, 50 : PermissionBackend permissionBackend, 51 148 : Provider<CurrentUser> self) { 52 148 : this.accountIndexer = accountIndexer; 53 148 : this.permissionBackend = permissionBackend; 54 148 : this.self = self; 55 148 : } 56 : 57 : @Override 58 : public Response<?> apply(AccountResource rsrc, Input input) 59 : throws IOException, AuthException, PermissionBackendException { 60 3 : if (!self.get().hasSameAccountId(rsrc.getUser())) { 61 3 : permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT); 62 : } 63 : 64 3 : accountIndexer.get().index(rsrc.getUser().getAccountId()); 65 3 : return Response.none(); 66 : } 67 : }