Line data Source code
1 : // Copyright (C) 2014 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.entities.Account; 18 : import com.google.gerrit.extensions.client.EditPreferencesInfo; 19 : import com.google.gerrit.extensions.restapi.IdString; 20 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 21 : import com.google.gerrit.extensions.restapi.Response; 22 : import com.google.gerrit.extensions.restapi.RestApiException; 23 : import com.google.gerrit.extensions.restapi.RestReadView; 24 : import com.google.gerrit.server.CurrentUser; 25 : import com.google.gerrit.server.account.AccountCache; 26 : import com.google.gerrit.server.account.AccountResource; 27 : import com.google.gerrit.server.account.AccountState; 28 : import com.google.gerrit.server.permissions.GlobalPermission; 29 : import com.google.gerrit.server.permissions.PermissionBackend; 30 : import com.google.gerrit.server.permissions.PermissionBackendException; 31 : import com.google.inject.Inject; 32 : import com.google.inject.Provider; 33 : import com.google.inject.Singleton; 34 : import java.io.IOException; 35 : import org.eclipse.jgit.errors.ConfigInvalidException; 36 : 37 : /** 38 : * REST endpoint to get the edit preferences of an account. 39 : * 40 : * <p>This REST endpoint handles {@code GET /accounts/<account-identifier>/preferences.edit} 41 : * requests. 42 : * 43 : * <p>General preferences can be retrieved by {@link GetPreferences} and diff preferences can be 44 : * retrieved by {@link GetDiffPreferences}. 45 : * 46 : * <p>Default edit preferences that apply for all accounts can be retrieved by {@link 47 : * com.google.gerrit.server.restapi.config.GetEditPreferences}. 48 : */ 49 : @Singleton 50 : public class GetEditPreferences implements RestReadView<AccountResource> { 51 : private final Provider<CurrentUser> self; 52 : private final PermissionBackend permissionBackend; 53 : private final AccountCache accountCache; 54 : 55 : @Inject 56 : GetEditPreferences( 57 148 : Provider<CurrentUser> self, PermissionBackend permissionBackend, AccountCache accountCache) { 58 148 : this.self = self; 59 148 : this.permissionBackend = permissionBackend; 60 148 : this.accountCache = accountCache; 61 148 : } 62 : 63 : @Override 64 : public Response<EditPreferencesInfo> apply(AccountResource rsrc) 65 : throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException { 66 1 : if (!self.get().hasSameAccountId(rsrc.getUser())) { 67 0 : permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT); 68 : } 69 : 70 1 : Account.Id id = rsrc.getUser().getAccountId(); 71 1 : return Response.ok( 72 : accountCache 73 1 : .get(id) 74 1 : .map(AccountState::editPreferences) 75 1 : .orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString())))); 76 : } 77 : }