Line data Source code
1 : // Copyright (C) 2014 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.extensions.api.changes.FixInput; 18 : import com.google.gerrit.extensions.client.ListChangesOption; 19 : import com.google.gerrit.extensions.common.ChangeInfo; 20 : import com.google.gerrit.extensions.restapi.AuthException; 21 : import com.google.gerrit.extensions.restapi.Response; 22 : import com.google.gerrit.extensions.restapi.RestApiException; 23 : import com.google.gerrit.extensions.restapi.RestModifyView; 24 : import com.google.gerrit.extensions.restapi.RestReadView; 25 : import com.google.gerrit.server.change.ChangeJson; 26 : import com.google.gerrit.server.change.ChangeResource; 27 : import com.google.gerrit.server.permissions.GlobalPermission; 28 : import com.google.gerrit.server.permissions.PermissionBackend; 29 : import com.google.gerrit.server.permissions.PermissionBackendException; 30 : import com.google.gerrit.server.permissions.ProjectPermission; 31 : import com.google.gerrit.server.project.NoSuchProjectException; 32 : import com.google.inject.Inject; 33 : import com.google.inject.Singleton; 34 : import java.io.IOException; 35 : 36 : @Singleton 37 : public class Check 38 : implements RestReadView<ChangeResource>, RestModifyView<ChangeResource, FixInput> { 39 : private final PermissionBackend permissionBackend; 40 : private final ChangeJson.Factory jsonFactory; 41 : 42 : @Inject 43 145 : Check(PermissionBackend permissionBackend, ChangeJson.Factory json) { 44 145 : this.permissionBackend = permissionBackend; 45 145 : this.jsonFactory = json; 46 145 : } 47 : 48 : @Override 49 : public Response<ChangeInfo> apply(ChangeResource rsrc) throws RestApiException { 50 1 : return Response.withMustRevalidate(newChangeJson().format(rsrc)); 51 : } 52 : 53 : @Override 54 : public Response<ChangeInfo> apply(ChangeResource rsrc, FixInput input) 55 : throws RestApiException, PermissionBackendException, NoSuchProjectException, IOException { 56 2 : PermissionBackend.WithUser perm = permissionBackend.currentUser(); 57 2 : if (!rsrc.isUserOwner()) { 58 : try { 59 0 : perm.project(rsrc.getProject()).check(ProjectPermission.READ_CONFIG); 60 0 : } catch (AuthException e) { 61 0 : perm.check(GlobalPermission.MAINTAIN_SERVER); 62 0 : } 63 : } 64 2 : return Response.withMustRevalidate(newChangeJson().fix(input).format(rsrc)); 65 : } 66 : 67 : private ChangeJson newChangeJson() { 68 2 : return jsonFactory.create(ListChangesOption.CHECK); 69 : } 70 : }