Line data Source code
1 : // Copyright (C) 2015 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.auth.ldap; 16 : 17 : import com.google.common.cache.LoadingCache; 18 : import com.google.gerrit.entities.AccountGroup; 19 : import com.google.gerrit.server.account.GroupMembership; 20 : import com.google.gerrit.server.account.ListGroupMembership; 21 : import com.google.gerrit.server.project.ProjectCache; 22 : import java.util.HashSet; 23 : import java.util.Set; 24 : import java.util.concurrent.ExecutionException; 25 : import org.eclipse.jgit.lib.Config; 26 : 27 : class LdapGroupMembership implements GroupMembership { 28 : private final LoadingCache<String, Set<AccountGroup.UUID>> membershipCache; 29 : private final ProjectCache projectCache; 30 : private final String id; 31 : private final boolean guessRelevantGroups; 32 : private GroupMembership membership; 33 : 34 : LdapGroupMembership( 35 : LoadingCache<String, Set<AccountGroup.UUID>> membershipCache, 36 : ProjectCache projectCache, 37 : String id, 38 1 : Config gerritConfig) { 39 1 : this.membershipCache = membershipCache; 40 1 : this.projectCache = projectCache; 41 1 : this.id = id; 42 1 : this.guessRelevantGroups = gerritConfig.getBoolean("ldap", "guessRelevantGroups", true); 43 1 : } 44 : 45 : @Override 46 : public boolean contains(AccountGroup.UUID groupId) { 47 0 : return get().contains(groupId); 48 : } 49 : 50 : @Override 51 : public boolean containsAnyOf(Iterable<AccountGroup.UUID> groupIds) { 52 0 : return get().containsAnyOf(groupIds); 53 : } 54 : 55 : @Override 56 : public Set<AccountGroup.UUID> intersection(Iterable<AccountGroup.UUID> groupIds) { 57 0 : return get().intersection(groupIds); 58 : } 59 : 60 : @Override 61 : public Set<AccountGroup.UUID> getKnownGroups() { 62 0 : Set<AccountGroup.UUID> g = new HashSet<>(get().getKnownGroups()); 63 0 : if (guessRelevantGroups) { 64 0 : g.retainAll(projectCache.guessRelevantGroupUUIDs()); 65 : } 66 0 : return g; 67 : } 68 : 69 : private synchronized GroupMembership get() { 70 0 : if (membership == null) { 71 : try { 72 0 : membership = new ListGroupMembership(membershipCache.get(id)); 73 0 : } catch (ExecutionException e) { 74 0 : LdapGroupBackend.logger 75 0 : .atWarning() 76 0 : .withCause(e) 77 0 : .log("Cannot lookup membershipsOf %s in LDAP", id); 78 0 : membership = GroupMembership.EMPTY; 79 0 : } 80 : } 81 0 : return membership; 82 : } 83 : }