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 static java.util.stream.Collectors.toList; 18 : 19 : import com.google.gerrit.entities.Account; 20 : import com.google.gerrit.entities.Project; 21 : import com.google.gerrit.extensions.client.ProjectWatchInfo; 22 : import com.google.gerrit.extensions.restapi.AuthException; 23 : import com.google.gerrit.extensions.restapi.Response; 24 : import com.google.gerrit.extensions.restapi.RestModifyView; 25 : import com.google.gerrit.extensions.restapi.UnprocessableEntityException; 26 : import com.google.gerrit.server.IdentifiedUser; 27 : import com.google.gerrit.server.UserInitiated; 28 : import com.google.gerrit.server.account.AccountResource; 29 : import com.google.gerrit.server.account.AccountsUpdate; 30 : import com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey; 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 : import java.io.IOException; 38 : import java.util.List; 39 : import java.util.Objects; 40 : import org.eclipse.jgit.errors.ConfigInvalidException; 41 : 42 : /** 43 : * REST endpoint to delete project watches from an account. 44 : * 45 : * <p>This REST endpoint handles {@code POST /accounts/<account-identifier>/watched.projects:delete} 46 : * requests. 47 : */ 48 : @Singleton 49 : public class DeleteWatchedProjects 50 : implements RestModifyView<AccountResource, List<ProjectWatchInfo>> { 51 : private final Provider<IdentifiedUser> self; 52 : private final PermissionBackend permissionBackend; 53 : private final Provider<AccountsUpdate> accountsUpdateProvider; 54 : 55 : @Inject 56 : DeleteWatchedProjects( 57 : Provider<IdentifiedUser> self, 58 : PermissionBackend permissionBackend, 59 148 : @UserInitiated Provider<AccountsUpdate> accountsUpdateProvider) { 60 148 : this.self = self; 61 148 : this.permissionBackend = permissionBackend; 62 148 : this.accountsUpdateProvider = accountsUpdateProvider; 63 148 : } 64 : 65 : @Override 66 : public Response<?> apply(AccountResource rsrc, List<ProjectWatchInfo> input) 67 : throws AuthException, UnprocessableEntityException, IOException, ConfigInvalidException, 68 : PermissionBackendException { 69 2 : if (!self.get().hasSameAccountId(rsrc.getUser())) { 70 0 : permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER); 71 : } 72 2 : if (input == null) { 73 0 : return Response.none(); 74 : } 75 : 76 2 : Account.Id accountId = rsrc.getUser().getAccountId(); 77 2 : accountsUpdateProvider 78 2 : .get() 79 2 : .update( 80 : "Delete Project Watches via API", 81 : accountId, 82 : u -> 83 2 : u.deleteProjectWatches( 84 2 : input.stream() 85 2 : .filter(Objects::nonNull) 86 2 : .map(w -> ProjectWatchKey.create(Project.nameKey(w.project), w.filter)) 87 2 : .collect(toList()))); 88 2 : return Response.none(); 89 : } 90 : }