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.query.project; 16 : 17 : import com.google.common.flogger.FluentLogger; 18 : import com.google.gerrit.index.project.ProjectData; 19 : import com.google.gerrit.index.query.IsVisibleToPredicate; 20 : import com.google.gerrit.server.CurrentUser; 21 : import com.google.gerrit.server.index.IndexUtils; 22 : import com.google.gerrit.server.permissions.PermissionBackend; 23 : import com.google.gerrit.server.permissions.ProjectPermission; 24 : import com.google.gerrit.server.query.account.AccountQueryBuilder; 25 : 26 : public class ProjectIsVisibleToPredicate extends IsVisibleToPredicate<ProjectData> { 27 6 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 28 : 29 : protected final PermissionBackend permissionBackend; 30 : protected final CurrentUser user; 31 : 32 : public ProjectIsVisibleToPredicate(PermissionBackend permissionBackend, CurrentUser user) { 33 6 : super(AccountQueryBuilder.FIELD_VISIBLETO, IndexUtils.describe(user)); 34 6 : this.permissionBackend = permissionBackend; 35 6 : this.user = user; 36 6 : } 37 : 38 : @Override 39 : public boolean match(ProjectData pd) { 40 5 : if (!pd.getProject().getState().permitsRead()) { 41 0 : logger.atFine().log("Filter out non-readable project: %s", pd); 42 0 : return false; 43 : } 44 : 45 5 : boolean canSee = 46 : permissionBackend 47 5 : .user(user) 48 5 : .project(pd.getProject().getNameKey()) 49 5 : .testOrFalse(ProjectPermission.ACCESS); 50 5 : if (!canSee) { 51 3 : logger.atFine().log("Filter out non-visible project: %s", pd); 52 : } 53 5 : return canSee; 54 : } 55 : 56 : @Override 57 : public int getCost() { 58 0 : return 1; 59 : } 60 : }