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.project; 16 : 17 : import static java.util.stream.Collectors.toList; 18 : 19 : import com.google.common.collect.ArrayListMultimap; 20 : import com.google.common.collect.Multimap; 21 : import com.google.gerrit.entities.Project; 22 : import com.google.gerrit.extensions.common.ProjectInfo; 23 : import com.google.gerrit.server.config.AllProjectsName; 24 : import com.google.gerrit.server.permissions.PermissionBackend; 25 : import com.google.gerrit.server.permissions.PermissionBackendException; 26 : import com.google.gerrit.server.permissions.ProjectPermission; 27 : import com.google.inject.Inject; 28 : import com.google.inject.Singleton; 29 : import java.util.ArrayList; 30 : import java.util.HashMap; 31 : import java.util.List; 32 : import java.util.Map; 33 : 34 : /** Retrieve child projects (ie. projects whose access inherits from a given parent.) */ 35 : @Singleton 36 : public class ChildProjects { 37 : private final ProjectCache projectCache; 38 : private final PermissionBackend permissionBackend; 39 : private final AllProjectsName allProjects; 40 : private final ProjectJson json; 41 : 42 : @Inject 43 : ChildProjects( 44 : ProjectCache projectCache, 45 : PermissionBackend permissionBackend, 46 : AllProjectsName allProjectsName, 47 149 : ProjectJson json) { 48 149 : this.projectCache = projectCache; 49 149 : this.permissionBackend = permissionBackend; 50 149 : this.allProjects = allProjectsName; 51 149 : this.json = json; 52 149 : } 53 : 54 : /** Gets all child projects recursively. */ 55 : public List<ProjectInfo> list(Project.NameKey parent) throws PermissionBackendException { 56 6 : Map<Project.NameKey, Project> projects = readAllReadableProjects(); 57 6 : Multimap<Project.NameKey, Project.NameKey> children = parentToChildren(projects); 58 6 : PermissionBackend.WithUser perm = permissionBackend.currentUser(); 59 : 60 6 : List<ProjectInfo> results = new ArrayList<>(); 61 6 : depthFirstFormat(results, perm, projects, children, parent); 62 6 : return results; 63 : } 64 : 65 : private Map<Project.NameKey, Project> readAllReadableProjects() { 66 6 : Map<Project.NameKey, Project> projects = new HashMap<>(); 67 6 : for (Project.NameKey name : projectCache.all()) { 68 : 69 6 : ProjectState c = 70 : projectCache 71 6 : .get(name) 72 6 : .orElseThrow( 73 : () -> 74 0 : new IllegalStateException( 75 : "race while traversing projects. got " 76 : + name 77 : + " when loading all projects, but can't load it now")); 78 6 : if (c != null && c.statePermitsRead()) { 79 6 : projects.put(c.getNameKey(), c.getProject()); 80 : } 81 6 : } 82 6 : return projects; 83 : } 84 : 85 : /** Map of parent project to direct child. */ 86 : private Multimap<Project.NameKey, Project.NameKey> parentToChildren( 87 : Map<Project.NameKey, Project> projects) { 88 6 : Multimap<Project.NameKey, Project.NameKey> m = ArrayListMultimap.create(); 89 6 : for (Map.Entry<Project.NameKey, Project> e : projects.entrySet()) { 90 6 : if (!allProjects.equals(e.getKey())) { 91 6 : m.put(e.getValue().getParent(allProjects), e.getKey()); 92 : } 93 6 : } 94 6 : return m; 95 : } 96 : 97 : private void depthFirstFormat( 98 : List<ProjectInfo> results, 99 : PermissionBackend.WithUser perm, 100 : Map<Project.NameKey, Project> projects, 101 : Multimap<Project.NameKey, Project.NameKey> children, 102 : Project.NameKey parent) 103 : throws PermissionBackendException { 104 6 : List<Project.NameKey> canSee = 105 6 : perm.filter(ProjectPermission.ACCESS, children.get(parent)).stream() 106 6 : .sorted() 107 6 : .collect(toList()); 108 6 : children.removeAll(parent); // removing all entries prevents cycles. 109 : 110 6 : for (Project.NameKey c : canSee) { 111 6 : results.add(json.format(projects.get(c))); 112 6 : depthFirstFormat(results, perm, projects, children, c); 113 6 : } 114 6 : } 115 : }