Line data Source code
1 : // Copyright (C) 2020 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.change; 16 : 17 : import com.google.gerrit.entities.Account; 18 : import com.google.gerrit.extensions.api.changes.AttentionSetInput; 19 : import com.google.gerrit.extensions.api.changes.NotifyHandling; 20 : import com.google.gerrit.extensions.common.AccountInfo; 21 : import com.google.gerrit.extensions.restapi.AuthException; 22 : import com.google.gerrit.extensions.restapi.BadRequestException; 23 : import com.google.gerrit.extensions.restapi.Response; 24 : import com.google.gerrit.extensions.restapi.RestCollectionModifyView; 25 : import com.google.gerrit.server.account.AccountLoader; 26 : import com.google.gerrit.server.account.AccountResolver; 27 : import com.google.gerrit.server.account.ServiceUserClassifier; 28 : import com.google.gerrit.server.change.AddToAttentionSetOp; 29 : import com.google.gerrit.server.change.AttentionSetEntryResource; 30 : import com.google.gerrit.server.change.ChangeResource; 31 : import com.google.gerrit.server.change.NotifyResolver; 32 : import com.google.gerrit.server.permissions.ChangePermission; 33 : import com.google.gerrit.server.permissions.PermissionBackend; 34 : import com.google.gerrit.server.update.BatchUpdate; 35 : import com.google.gerrit.server.util.AttentionSetUtil; 36 : import com.google.gerrit.server.util.time.TimeUtil; 37 : import com.google.inject.Inject; 38 : import com.google.inject.Singleton; 39 : 40 : /** Adds a single user to the attention set. */ 41 : @Singleton 42 : public class AddToAttentionSet 43 : implements RestCollectionModifyView< 44 : ChangeResource, AttentionSetEntryResource, AttentionSetInput> { 45 : 46 : private final BatchUpdate.Factory updateFactory; 47 : private final AccountResolver accountResolver; 48 : private final AddToAttentionSetOp.Factory opFactory; 49 : private final AccountLoader.Factory accountLoaderFactory; 50 : private final PermissionBackend permissionBackend; 51 : private final NotifyResolver notifyResolver; 52 : private final ServiceUserClassifier serviceUserClassifier; 53 : 54 : @Inject 55 : AddToAttentionSet( 56 : BatchUpdate.Factory updateFactory, 57 : AccountResolver accountResolver, 58 : AddToAttentionSetOp.Factory opFactory, 59 : AccountLoader.Factory accountLoaderFactory, 60 : PermissionBackend permissionBackend, 61 : NotifyResolver notifyResolver, 62 145 : ServiceUserClassifier serviceUserClassifier) { 63 145 : this.updateFactory = updateFactory; 64 145 : this.accountResolver = accountResolver; 65 145 : this.opFactory = opFactory; 66 145 : this.accountLoaderFactory = accountLoaderFactory; 67 145 : this.permissionBackend = permissionBackend; 68 145 : this.notifyResolver = notifyResolver; 69 145 : this.serviceUserClassifier = serviceUserClassifier; 70 145 : } 71 : 72 : @Override 73 : public Response<AccountInfo> apply(ChangeResource changeResource, AttentionSetInput input) 74 : throws Exception { 75 6 : AttentionSetUtil.validateInput(input); 76 6 : Account.Id attentionUserId = 77 6 : AttentionSetUtil.resolveAccount(accountResolver, changeResource.getNotes(), input.user); 78 : 79 6 : if (serviceUserClassifier.isServiceUser(attentionUserId)) { 80 1 : throw new BadRequestException( 81 1 : String.format( 82 : "%s is a robot, and robots can't be added to the attention set.", input.user)); 83 : } 84 6 : if (!permissionBackend 85 6 : .absentUser(attentionUserId) 86 6 : .change(changeResource.getNotes()) 87 6 : .test(ChangePermission.READ)) { 88 1 : throw new AuthException("read not permitted for " + attentionUserId); 89 : } 90 : 91 6 : try (BatchUpdate bu = 92 6 : updateFactory.create( 93 6 : changeResource.getChange().getProject(), changeResource.getUser(), TimeUtil.now())) { 94 6 : AddToAttentionSetOp op = opFactory.create(attentionUserId, input.reason, true); 95 6 : bu.addOp(changeResource.getId(), op); 96 6 : NotifyHandling notify = input.notify == null ? NotifyHandling.OWNER : input.notify; 97 6 : NotifyResolver.Result notifyResult = notifyResolver.resolve(notify, input.notifyDetails); 98 6 : bu.setNotify(notifyResult); 99 6 : bu.execute(); 100 6 : return Response.ok(accountLoaderFactory.create(true).fillOne(attentionUserId)); 101 : } 102 : } 103 : }