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.gerrit.entities.Account; 18 : import com.google.gerrit.extensions.client.GeneralPreferencesInfo; 19 : import com.google.gerrit.extensions.config.DownloadScheme; 20 : import com.google.gerrit.extensions.registration.DynamicMap; 21 : import com.google.gerrit.extensions.registration.Extension; 22 : import com.google.gerrit.extensions.restapi.IdString; 23 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 24 : import com.google.gerrit.extensions.restapi.Response; 25 : import com.google.gerrit.extensions.restapi.RestApiException; 26 : import com.google.gerrit.extensions.restapi.RestReadView; 27 : import com.google.gerrit.server.CurrentUser; 28 : import com.google.gerrit.server.account.AccountCache; 29 : import com.google.gerrit.server.account.AccountResource; 30 : import com.google.gerrit.server.account.AccountState; 31 : import com.google.gerrit.server.permissions.GlobalPermission; 32 : import com.google.gerrit.server.permissions.PermissionBackend; 33 : import com.google.gerrit.server.permissions.PermissionBackendException; 34 : import com.google.inject.Inject; 35 : import com.google.inject.Provider; 36 : import com.google.inject.Singleton; 37 : 38 : /** 39 : * REST endpoint to get the general preferences of an account. 40 : * 41 : * <p>This REST endpoint handles {@code GET /accounts/<account-identifier>/preferences} requests. 42 : * 43 : * <p>Diff preferences can be retrieved by {@link GetDiffPreferences} and edit preferences can be 44 : * retrieved by {@link GetEditPreferences}. 45 : * 46 : * <p>Default general preferences that apply for all accounts can be retrieved by {@link 47 : * com.google.gerrit.server.restapi.config.GetPreferences}. 48 : */ 49 : @Singleton 50 : public class GetPreferences implements RestReadView<AccountResource> { 51 : private final Provider<CurrentUser> self; 52 : private final PermissionBackend permissionBackend; 53 : private final AccountCache accountCache; 54 : private final DynamicMap<DownloadScheme> downloadSchemes; 55 : 56 : @Inject 57 : GetPreferences( 58 : Provider<CurrentUser> self, 59 : PermissionBackend permissionBackend, 60 : AccountCache accountCache, 61 148 : DynamicMap<DownloadScheme> downloadSchemes) { 62 148 : this.self = self; 63 148 : this.permissionBackend = permissionBackend; 64 148 : this.accountCache = accountCache; 65 148 : this.downloadSchemes = downloadSchemes; 66 148 : } 67 : 68 : @Override 69 : public Response<GeneralPreferencesInfo> apply(AccountResource rsrc) 70 : throws RestApiException, PermissionBackendException { 71 6 : if (!self.get().hasSameAccountId(rsrc.getUser())) { 72 1 : permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT); 73 : } 74 : 75 6 : Account.Id id = rsrc.getUser().getAccountId(); 76 6 : GeneralPreferencesInfo preferencesInfo = 77 : accountCache 78 6 : .get(id) 79 6 : .map(AccountState::generalPreferences) 80 6 : .orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString()))); 81 6 : return Response.ok(unsetDownloadSchemeIfUnsupported(preferencesInfo)); 82 : } 83 : 84 : private GeneralPreferencesInfo unsetDownloadSchemeIfUnsupported( 85 : GeneralPreferencesInfo preferencesInfo) { 86 6 : if (preferencesInfo.downloadScheme == null) { 87 6 : return preferencesInfo; 88 : } 89 : 90 1 : for (Extension<DownloadScheme> e : downloadSchemes) { 91 1 : if (e.getExportName().equals(preferencesInfo.downloadScheme) 92 1 : && e.getProvider().get().isEnabled()) { 93 1 : return preferencesInfo; 94 : } 95 0 : } 96 : 97 1 : preferencesInfo.downloadScheme = null; 98 1 : return preferencesInfo; 99 : } 100 : }