Line data Source code
1 : // Copyright (C) 2011 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.gerrit.entities.Project; 20 : import com.google.gerrit.server.config.AllProjectsName; 21 : import com.google.gerrit.server.permissions.PermissionBackend; 22 : import com.google.gerrit.server.permissions.PermissionBackendException; 23 : import com.google.gerrit.server.permissions.ProjectPermission; 24 : import com.google.inject.Inject; 25 : import com.google.inject.Singleton; 26 : import java.util.HashSet; 27 : import java.util.List; 28 : import java.util.Optional; 29 : import java.util.Set; 30 : 31 : @Singleton 32 : public class SuggestParentCandidates { 33 : private final ProjectCache projectCache; 34 : private final PermissionBackend permissionBackend; 35 : private final AllProjectsName allProjects; 36 : 37 : @Inject 38 : SuggestParentCandidates( 39 1 : ProjectCache projectCache, PermissionBackend permissionBackend, AllProjectsName allProjects) { 40 1 : this.projectCache = projectCache; 41 1 : this.permissionBackend = permissionBackend; 42 1 : this.allProjects = allProjects; 43 1 : } 44 : 45 : public List<Project.NameKey> getNameKeys() throws PermissionBackendException { 46 0 : return permissionBackend.currentUser().filter(ProjectPermission.ACCESS, readableParents()) 47 0 : .stream() 48 0 : .sorted() 49 0 : .collect(toList()); 50 : } 51 : 52 : private Set<Project.NameKey> readableParents() { 53 0 : Set<Project.NameKey> parents = new HashSet<>(); 54 0 : for (Project.NameKey p : projectCache.all()) { 55 0 : Optional<ProjectState> ps = projectCache.get(p); 56 0 : if (ps.isPresent() && ps.get().statePermitsRead()) { 57 0 : Project.NameKey parent = ps.get().getProject().getParent(); 58 0 : if (parent != null) { 59 0 : parents.add(parent); 60 : } 61 : } 62 0 : } 63 0 : parents.add(allProjects); 64 0 : return parents; 65 : } 66 : }