Line data Source code
1 : // Copyright (C) 2017 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 static com.google.gerrit.extensions.conditions.BooleanCondition.or; 18 : 19 : import com.google.gerrit.common.Nullable; 20 : import com.google.gerrit.extensions.common.InputWithMessage; 21 : import com.google.gerrit.extensions.conditions.BooleanCondition; 22 : import com.google.gerrit.extensions.restapi.AuthException; 23 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 24 : import com.google.gerrit.extensions.restapi.Response; 25 : import com.google.gerrit.extensions.restapi.RestApiException; 26 : import com.google.gerrit.extensions.restapi.RestModifyView; 27 : import com.google.gerrit.server.change.ChangeResource; 28 : import com.google.gerrit.server.change.SetPrivateOp; 29 : import com.google.gerrit.server.permissions.GlobalPermission; 30 : import com.google.gerrit.server.permissions.PermissionBackend; 31 : import com.google.gerrit.server.update.BatchUpdate; 32 : import com.google.gerrit.server.update.UpdateException; 33 : import com.google.gerrit.server.util.time.TimeUtil; 34 : import com.google.inject.Inject; 35 : import com.google.inject.Singleton; 36 : 37 : @Singleton 38 : public class DeletePrivate implements RestModifyView<ChangeResource, InputWithMessage> { 39 : private final PermissionBackend permissionBackend; 40 : private final BatchUpdate.Factory updateFactory; 41 : private final SetPrivateOp.Factory setPrivateOpFactory; 42 : 43 : @Inject 44 : DeletePrivate( 45 : PermissionBackend permissionBackend, 46 : BatchUpdate.Factory updateFactory, 47 145 : SetPrivateOp.Factory setPrivateOpFactory) { 48 145 : this.permissionBackend = permissionBackend; 49 145 : this.updateFactory = updateFactory; 50 145 : this.setPrivateOpFactory = setPrivateOpFactory; 51 145 : } 52 : 53 : @Override 54 : public Response<String> apply(ChangeResource rsrc, @Nullable InputWithMessage input) 55 : throws RestApiException, UpdateException { 56 2 : if (!canDeletePrivate(rsrc).value()) { 57 0 : throw new AuthException("not allowed to unmark private"); 58 : } 59 : 60 2 : if (!rsrc.getChange().isPrivate()) { 61 1 : throw new ResourceConflictException("change is not private"); 62 : } 63 : 64 2 : SetPrivateOp op = setPrivateOpFactory.create(false, input); 65 2 : try (BatchUpdate u = updateFactory.create(rsrc.getProject(), rsrc.getUser(), TimeUtil.now())) { 66 2 : u.addOp(rsrc.getId(), op).execute(); 67 : } 68 : 69 2 : return Response.none(); 70 : } 71 : 72 : protected BooleanCondition canDeletePrivate(ChangeResource rsrc) { 73 57 : PermissionBackend.WithUser user = permissionBackend.user(rsrc.getUser()); 74 57 : return or(rsrc.isUserOwner(), user.testCond(GlobalPermission.ADMINISTRATE_SERVER)); 75 : } 76 : }