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.account; 16 : 17 : import static com.google.common.collect.ImmutableSet.toImmutableSet; 18 : import static com.google.gerrit.server.project.ProjectCache.noSuchProject; 19 : 20 : import com.google.common.collect.Sets; 21 : import com.google.common.collect.Streams; 22 : import com.google.gerrit.common.Nullable; 23 : import com.google.gerrit.entities.Account; 24 : import com.google.gerrit.entities.AccountGroup; 25 : import com.google.gerrit.entities.InternalGroup; 26 : import com.google.gerrit.entities.Project; 27 : import com.google.gerrit.server.group.InternalGroupDescription; 28 : import com.google.gerrit.server.group.SystemGroupBackend; 29 : import com.google.gerrit.server.project.NoSuchProjectException; 30 : import com.google.gerrit.server.project.ProjectCache; 31 : import com.google.gerrit.server.project.ProjectState; 32 : import com.google.inject.Inject; 33 : import com.google.inject.Singleton; 34 : import java.io.IOException; 35 : import java.util.Collections; 36 : import java.util.HashSet; 37 : import java.util.Optional; 38 : import java.util.Set; 39 : 40 : @Singleton 41 : public class GroupMembers { 42 : 43 : private final GroupCache groupCache; 44 : private final GroupControl.Factory groupControlFactory; 45 : private final AccountCache accountCache; 46 : private final ProjectCache projectCache; 47 : 48 : @Inject 49 : GroupMembers( 50 : GroupCache groupCache, 51 : GroupControl.Factory groupControlFactory, 52 : AccountCache accountCache, 53 150 : ProjectCache projectCache) { 54 150 : this.groupCache = groupCache; 55 150 : this.groupControlFactory = groupControlFactory; 56 150 : this.accountCache = accountCache; 57 150 : this.projectCache = projectCache; 58 150 : } 59 : 60 : /** 61 : * Recursively enumerate the members of the given group. Should not be used with the 62 : * PROJECT_OWNERS magical group. 63 : * 64 : * <p>Group members for which an account doesn't exist are filtered out. 65 : */ 66 : public Set<Account> listAccounts(AccountGroup.UUID groupUUID) throws IOException { 67 5 : if (SystemGroupBackend.PROJECT_OWNERS.equals(groupUUID)) { 68 0 : throw new IllegalStateException("listAccounts called with PROJECT_OWNERS argument"); 69 : } 70 : try { 71 5 : return listAccounts(groupUUID, null, new HashSet<>()); 72 0 : } catch (NoSuchProjectException e) { 73 0 : throw new IllegalStateException(e); 74 : } 75 : } 76 : 77 : /** 78 : * Recursively enumerate the members of the given group. The project should be specified so the 79 : * PROJECT_OWNERS magical group can be expanded. 80 : * 81 : * <p>Group members for which an account doesn't exist are filtered out. 82 : */ 83 : public Set<Account> listAccounts(AccountGroup.UUID groupUUID, Project.NameKey project) 84 : throws NoSuchProjectException, IOException { 85 5 : return listAccounts(groupUUID, project, new HashSet<>()); 86 : } 87 : 88 : private Set<Account> listAccounts( 89 : final AccountGroup.UUID groupUUID, 90 : @Nullable final Project.NameKey project, 91 : final Set<AccountGroup.UUID> seen) 92 : throws NoSuchProjectException, IOException { 93 10 : if (SystemGroupBackend.PROJECT_OWNERS.equals(groupUUID)) { 94 0 : return getProjectOwners(project, seen); 95 : } 96 10 : Optional<InternalGroup> group = groupCache.get(groupUUID); 97 10 : if (group.isPresent()) { 98 10 : return getGroupMembers(group.get(), project, seen); 99 : } 100 4 : return Collections.emptySet(); 101 : } 102 : 103 : private Set<Account> getProjectOwners(final Project.NameKey project, Set<AccountGroup.UUID> seen) 104 : throws NoSuchProjectException, IOException { 105 0 : seen.add(SystemGroupBackend.PROJECT_OWNERS); 106 0 : if (project == null) { 107 0 : return Collections.emptySet(); 108 : } 109 : 110 0 : ProjectState projectState = projectCache.get(project).orElseThrow(noSuchProject(project)); 111 0 : final HashSet<Account> projectOwners = new HashSet<>(); 112 0 : for (AccountGroup.UUID ownerGroup : projectState.getAllOwners()) { 113 0 : if (!seen.contains(ownerGroup)) { 114 0 : projectOwners.addAll(listAccounts(ownerGroup, project, seen)); 115 : } 116 0 : } 117 0 : return projectOwners; 118 : } 119 : 120 : private Set<Account> getGroupMembers( 121 : InternalGroup group, @Nullable Project.NameKey project, Set<AccountGroup.UUID> seen) 122 : throws NoSuchProjectException, IOException { 123 10 : seen.add(group.getGroupUUID()); 124 10 : GroupControl groupControl = groupControlFactory.controlFor(new InternalGroupDescription(group)); 125 : 126 10 : Set<Account> directMembers = 127 10 : group.getMembers().stream() 128 10 : .filter(groupControl::canSeeMember) 129 10 : .map(accountCache::get) 130 10 : .flatMap(Streams::stream) 131 10 : .map(AccountState::account) 132 10 : .collect(toImmutableSet()); 133 : 134 10 : Set<Account> indirectMembers = new HashSet<>(); 135 10 : if (groupControl.canSeeGroup()) { 136 9 : for (AccountGroup.UUID subgroupUuid : group.getSubgroups()) { 137 0 : if (!seen.contains(subgroupUuid)) { 138 0 : indirectMembers.addAll(listAccounts(subgroupUuid, project, seen)); 139 : } 140 0 : } 141 : } 142 : 143 10 : return Sets.union(directMembers, indirectMembers); 144 : } 145 : }