Line data Source code
1 : // Copyright (C) 2022 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.project; 16 : 17 : import com.google.gerrit.entities.SubmitRequirement; 18 : import com.google.gerrit.extensions.registration.DynamicMap; 19 : import com.google.gerrit.extensions.restapi.AuthException; 20 : import com.google.gerrit.extensions.restapi.ChildCollection; 21 : import com.google.gerrit.extensions.restapi.IdString; 22 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 23 : import com.google.gerrit.extensions.restapi.RestApiException; 24 : import com.google.gerrit.extensions.restapi.RestView; 25 : import com.google.gerrit.server.CurrentUser; 26 : import com.google.gerrit.server.permissions.PermissionBackend; 27 : import com.google.gerrit.server.permissions.PermissionBackendException; 28 : import com.google.gerrit.server.permissions.ProjectPermission; 29 : import com.google.gerrit.server.project.ProjectResource; 30 : import com.google.gerrit.server.project.SubmitRequirementResource; 31 : import com.google.inject.Inject; 32 : import com.google.inject.Provider; 33 : import com.google.inject.Singleton; 34 : 35 : @Singleton 36 : public class SubmitRequirementsCollection 37 : implements ChildCollection<ProjectResource, SubmitRequirementResource> { 38 : private final Provider<CurrentUser> user; 39 : private final PermissionBackend permissionBackend; 40 : private final DynamicMap<RestView<SubmitRequirementResource>> views; 41 : private final Provider<ListSubmitRequirements> list; 42 : 43 : @Inject 44 : SubmitRequirementsCollection( 45 : Provider<CurrentUser> user, 46 : PermissionBackend permissionBackend, 47 : DynamicMap<RestView<SubmitRequirementResource>> views, 48 144 : Provider<ListSubmitRequirements> list) { 49 144 : this.user = user; 50 144 : this.permissionBackend = permissionBackend; 51 144 : this.list = list; 52 144 : this.views = views; 53 144 : } 54 : 55 : @Override 56 : public RestView<ProjectResource> list() throws RestApiException { 57 1 : return list.get(); 58 : } 59 : 60 : @Override 61 : public SubmitRequirementResource parse(ProjectResource parent, IdString id) 62 : throws AuthException, ResourceNotFoundException, PermissionBackendException { 63 3 : if (!user.get().isIdentifiedUser()) { 64 1 : throw new AuthException("Authentication required"); 65 : } 66 : 67 3 : permissionBackend 68 3 : .currentUser() 69 3 : .project(parent.getNameKey()) 70 3 : .check(ProjectPermission.READ_CONFIG); 71 : 72 3 : SubmitRequirement submitRequirement = 73 3 : parent.getProjectState().getConfig().getSubmitRequirementSections().get(id.get()); 74 : 75 3 : if (submitRequirement == null) { 76 3 : throw new ResourceNotFoundException( 77 3 : String.format("Submit requirement '%s' does not exist", id)); 78 : } 79 3 : return new SubmitRequirementResource(parent, submitRequirement); 80 : } 81 : 82 : @Override 83 : public DynamicMap<RestView<SubmitRequirementResource>> views() { 84 1 : return views; 85 : } 86 : }