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.and; 18 : import static com.google.gerrit.extensions.conditions.BooleanCondition.or; 19 : 20 : import com.google.gerrit.entities.Change; 21 : import com.google.gerrit.extensions.common.InputWithMessage; 22 : import com.google.gerrit.extensions.conditions.BooleanCondition; 23 : import com.google.gerrit.extensions.restapi.AuthException; 24 : import com.google.gerrit.extensions.restapi.MethodNotAllowedException; 25 : import com.google.gerrit.extensions.restapi.Response; 26 : import com.google.gerrit.extensions.restapi.RestApiException; 27 : import com.google.gerrit.extensions.restapi.RestModifyView; 28 : import com.google.gerrit.extensions.webui.UiAction; 29 : import com.google.gerrit.server.change.ChangeResource; 30 : import com.google.gerrit.server.change.SetPrivateOp; 31 : import com.google.gerrit.server.config.GerritServerConfig; 32 : import com.google.gerrit.server.permissions.GlobalPermission; 33 : import com.google.gerrit.server.permissions.PermissionBackend; 34 : import com.google.gerrit.server.update.BatchUpdate; 35 : import com.google.gerrit.server.update.UpdateException; 36 : import com.google.gerrit.server.util.time.TimeUtil; 37 : import com.google.inject.Inject; 38 : import com.google.inject.Singleton; 39 : import org.eclipse.jgit.lib.Config; 40 : 41 : @Singleton 42 : public class PostPrivate 43 : implements RestModifyView<ChangeResource, InputWithMessage>, UiAction<ChangeResource> { 44 : private final PermissionBackend permissionBackend; 45 : private final BatchUpdate.Factory updateFactory; 46 : private final SetPrivateOp.Factory setPrivateOpFactory; 47 : private final boolean disablePrivateChanges; 48 : 49 : @Inject 50 : PostPrivate( 51 : PermissionBackend permissionBackend, 52 : BatchUpdate.Factory updateFactory, 53 : SetPrivateOp.Factory setPrivateOpFactory, 54 145 : @GerritServerConfig Config config) { 55 145 : this.permissionBackend = permissionBackend; 56 145 : this.updateFactory = updateFactory; 57 145 : this.setPrivateOpFactory = setPrivateOpFactory; 58 145 : this.disablePrivateChanges = config.getBoolean("change", null, "disablePrivateChanges", false); 59 145 : } 60 : 61 : @Override 62 : public Response<String> apply(ChangeResource rsrc, InputWithMessage input) 63 : throws RestApiException, UpdateException { 64 16 : if (disablePrivateChanges) { 65 1 : throw new MethodNotAllowedException("private changes are disabled"); 66 : } 67 : 68 16 : if (!canSetPrivate(rsrc).value()) { 69 1 : throw new AuthException("not allowed to mark private"); 70 : } 71 : 72 16 : if (rsrc.getChange().isPrivate()) { 73 0 : return Response.ok(); 74 : } 75 : 76 16 : SetPrivateOp op = setPrivateOpFactory.create(true, input); 77 16 : try (BatchUpdate u = updateFactory.create(rsrc.getProject(), rsrc.getUser(), TimeUtil.now())) { 78 16 : u.addOp(rsrc.getId(), op).execute(); 79 : } 80 : 81 16 : return Response.created(); 82 : } 83 : 84 : @Override 85 : public Description getDescription(ChangeResource rsrc) { 86 57 : Change change = rsrc.getChange(); 87 57 : return new UiAction.Description() 88 57 : .setLabel("Mark private") 89 57 : .setTitle("Mark change as private") 90 57 : .setVisible( 91 57 : and( 92 57 : !disablePrivateChanges && !change.isPrivate() && change.isNew(), 93 57 : canSetPrivate(rsrc))); 94 : } 95 : 96 : private BooleanCondition canSetPrivate(ChangeResource rsrc) { 97 59 : PermissionBackend.WithUser user = permissionBackend.user(rsrc.getUser()); 98 59 : return or( 99 59 : rsrc.isUserOwner() && !rsrc.getChange().isMerged(), 100 59 : user.testCond(GlobalPermission.ADMINISTRATE_SERVER)); 101 : } 102 : }