Line data Source code
1 : // Copyright (C) 2013 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.access; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.entities.Project; 19 : import com.google.gerrit.extensions.api.access.ProjectAccessInfo; 20 : import com.google.gerrit.extensions.restapi.AuthException; 21 : import com.google.gerrit.extensions.restapi.IdString; 22 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 23 : import com.google.gerrit.extensions.restapi.Response; 24 : import com.google.gerrit.extensions.restapi.RestReadView; 25 : import com.google.gerrit.extensions.restapi.TopLevelResource; 26 : import com.google.gerrit.server.permissions.PermissionBackend; 27 : import com.google.gerrit.server.permissions.ProjectPermission; 28 : import com.google.gerrit.server.project.ProjectCache; 29 : import com.google.gerrit.server.restapi.project.GetAccess; 30 : import com.google.inject.Inject; 31 : import java.util.ArrayList; 32 : import java.util.List; 33 : import java.util.Map; 34 : import java.util.TreeMap; 35 : import org.kohsuke.args4j.Option; 36 : 37 : /** 38 : * REST endpoint to list members of the {@link AccessCollection}. 39 : * 40 : * <p>This REST endpoint handles {@code GET /access/} requests. 41 : */ 42 : public class ListAccess implements RestReadView<TopLevelResource> { 43 : 44 3 : @Option( 45 : name = "--project", 46 : aliases = {"-p"}, 47 : metaVar = "PROJECT", 48 : usage = "projects for which the access rights should be returned") 49 : private List<String> projects = new ArrayList<>(); 50 : 51 : private final PermissionBackend permissionBackend; 52 : private final ProjectCache projectCache; 53 : private final GetAccess getAccess; 54 : 55 : @Inject 56 : public ListAccess( 57 3 : PermissionBackend permissionBackend, ProjectCache projectCache, GetAccess getAccess) { 58 3 : this.permissionBackend = permissionBackend; 59 3 : this.projectCache = projectCache; 60 3 : this.getAccess = getAccess; 61 3 : } 62 : 63 : @Override 64 : public Response<Map<String, ProjectAccessInfo>> apply(TopLevelResource resource) 65 : throws Exception { 66 3 : Map<String, ProjectAccessInfo> access = new TreeMap<>(); 67 3 : for (String p : projects) { 68 1 : if (Strings.nullToEmpty(p).isEmpty()) { 69 1 : continue; 70 : } 71 : 72 1 : Project.NameKey projectName = Project.nameKey(IdString.fromUrl(p).get().trim()); 73 : 74 1 : if (!projectCache.get(projectName).isPresent()) { 75 1 : throw new ResourceNotFoundException(projectName.get()); 76 : } 77 : 78 : try { 79 1 : permissionBackend.currentUser().project(projectName).check(ProjectPermission.ACCESS); 80 1 : } catch (AuthException e) { 81 1 : throw new ResourceNotFoundException(projectName.get(), e); 82 1 : } 83 : 84 1 : access.put(projectName.get(), getAccess.apply(projectName)); 85 1 : } 86 3 : return Response.ok(access); 87 : } 88 : }