Line data Source code
1 : // Copyright (C) 2020 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; 16 : 17 : import static com.google.common.flogger.LazyArgs.lazy; 18 : 19 : import com.google.common.collect.ImmutableSet; 20 : import com.google.common.flogger.FluentLogger; 21 : import com.google.gerrit.server.account.GroupBackend; 22 : import com.google.gerrit.server.account.GroupMembership; 23 : import com.google.gerrit.server.account.externalids.ExternalId; 24 : import com.google.inject.Inject; 25 : import com.google.inject.assistedinject.Assisted; 26 : import java.util.Collection; 27 : 28 : /** 29 : * Represents a user that does not have a Gerrit account. 30 : * 31 : * <p>This user is limited in what they can do on Gerrit. For now, we only guarantee that permission 32 : * checking - including ref filtering works. 33 : * 34 : * <p>This class is thread-safe. 35 : */ 36 : public class ExternalUser extends CurrentUser { 37 1 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 38 : 39 : public interface Factory { 40 : ExternalUser create( 41 : Collection<String> emailAddresses, 42 : Collection<ExternalId.Key> externalIdKeys, 43 : PropertyMap propertyMap); 44 : } 45 : 46 : private final GroupBackend groupBackend; 47 : private final ImmutableSet<String> emailAddresses; 48 : private final ImmutableSet<ExternalId.Key> externalIdKeys; 49 : 50 : private GroupMembership effectiveGroups; 51 : 52 : @Inject 53 : public ExternalUser( 54 : GroupBackend groupBackend, 55 : @Assisted Collection<String> emailAddresses, 56 : @Assisted Collection<ExternalId.Key> externalIdKeys, 57 : @Assisted PropertyMap propertyMap) { 58 1 : super(propertyMap); 59 1 : this.groupBackend = groupBackend; 60 1 : this.emailAddresses = ImmutableSet.copyOf(emailAddresses); 61 1 : this.externalIdKeys = ImmutableSet.copyOf(externalIdKeys); 62 1 : } 63 : 64 : @Override 65 : public ImmutableSet<String> getEmailAddresses() { 66 0 : return emailAddresses; 67 : } 68 : 69 : @Override 70 : public ImmutableSet<ExternalId.Key> getExternalIdKeys() { 71 1 : return externalIdKeys; 72 : } 73 : 74 : @Override 75 : public GroupMembership getEffectiveGroups() { 76 1 : synchronized (this) { 77 1 : if (effectiveGroups == null) { 78 1 : effectiveGroups = groupBackend.membershipsOf(this); 79 1 : logger.atFinest().log( 80 1 : "Known groups of %s: %s", getLoggableName(), lazy(effectiveGroups::getKnownGroups)); 81 : } 82 1 : } 83 1 : return effectiveGroups; 84 : } 85 : 86 : @Override 87 : public Object getCacheKey() { 88 1 : return this; // Caching is tied to this exact instance. 89 : } 90 : }