LCOV - code coverage report
Current view: top level - server/account/externalids - ExternalIds.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 21 28 75.0 %
Date: 2022-11-19 15:00:39 Functions: 9 15 60.0 %

          Line data    Source code
       1             : // Copyright (C) 2016 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.externalids;
      16             : 
      17             : import static com.google.common.collect.ImmutableSet.toImmutableSet;
      18             : 
      19             : import com.google.common.collect.ImmutableSet;
      20             : import com.google.common.collect.ImmutableSetMultimap;
      21             : import com.google.gerrit.entities.Account;
      22             : import com.google.gerrit.server.config.AuthConfig;
      23             : import com.google.inject.Inject;
      24             : import com.google.inject.Singleton;
      25             : import java.io.IOException;
      26             : import java.util.Optional;
      27             : import org.eclipse.jgit.errors.ConfigInvalidException;
      28             : import org.eclipse.jgit.lib.ObjectId;
      29             : 
      30             : /**
      31             :  * Class to access external IDs.
      32             :  *
      33             :  * <p>The external IDs are either read from NoteDb or retrieved from the cache.
      34             :  */
      35             : @Singleton
      36             : public class ExternalIds {
      37             :   private final ExternalIdReader externalIdReader;
      38             :   private final ExternalIdCache externalIdCache;
      39             :   private final AuthConfig authConfig;
      40             :   private final ExternalIdKeyFactory externalIdKeyFactory;
      41             : 
      42             :   @Inject
      43             :   public ExternalIds(
      44             :       ExternalIdReader externalIdReader,
      45             :       ExternalIdCache externalIdCache,
      46             :       ExternalIdKeyFactory externalIdKeyFactory,
      47         152 :       AuthConfig authConfig) {
      48         152 :     this.externalIdReader = externalIdReader;
      49         152 :     this.externalIdCache = externalIdCache;
      50         152 :     this.externalIdKeyFactory = externalIdKeyFactory;
      51         152 :     this.authConfig = authConfig;
      52         152 :   }
      53             : 
      54             :   /** Returns all external IDs. */
      55             :   public ImmutableSet<ExternalId> all() throws IOException, ConfigInvalidException {
      56           3 :     return externalIdReader.all();
      57             :   }
      58             : 
      59             :   /** Returns all external IDs from the specified revision of the refs/meta/external-ids branch. */
      60             :   public ImmutableSet<ExternalId> all(ObjectId rev) throws IOException, ConfigInvalidException {
      61           0 :     return externalIdReader.all(rev);
      62             :   }
      63             : 
      64             :   /** Returns the specified external ID. */
      65             :   public Optional<ExternalId> get(ExternalId.Key key) throws IOException {
      66          81 :     Optional<ExternalId> externalId = Optional.empty();
      67          81 :     if (authConfig.isUserNameCaseInsensitiveMigrationMode()) {
      68           1 :       externalId =
      69           1 :           externalIdCache.byKey(externalIdKeyFactory.create(key.scheme(), key.id(), false));
      70             :     }
      71          81 :     if (!externalId.isPresent()) {
      72          81 :       externalId = externalIdCache.byKey(key);
      73             :     }
      74          81 :     return externalId;
      75             :   }
      76             : 
      77             :   /** Returns the specified external ID from the given revision. */
      78             :   public Optional<ExternalId> get(ExternalId.Key key, ObjectId rev)
      79             :       throws IOException, ConfigInvalidException {
      80           0 :     return externalIdReader.get(key, rev);
      81             :   }
      82             : 
      83             :   /** Returns the external IDs of the specified account. */
      84             :   public ImmutableSet<ExternalId> byAccount(Account.Id accountId) throws IOException {
      85         151 :     return externalIdCache.byAccount(accountId);
      86             :   }
      87             : 
      88             :   /** Returns the external IDs of the specified account that have the given scheme. */
      89             :   public ImmutableSet<ExternalId> byAccount(Account.Id accountId, String scheme)
      90             :       throws IOException {
      91           3 :     return byAccount(accountId).stream()
      92           3 :         .filter(e -> e.key().isScheme(scheme))
      93           3 :         .collect(toImmutableSet());
      94             :   }
      95             : 
      96             :   /** Returns the external IDs of the specified account. */
      97             :   public ImmutableSet<ExternalId> byAccount(Account.Id accountId, ObjectId rev) throws IOException {
      98         151 :     return externalIdCache.byAccount(accountId, rev);
      99             :   }
     100             : 
     101             :   /** Returns the external IDs of the specified account that have the given scheme. */
     102             :   public ImmutableSet<ExternalId> byAccount(Account.Id accountId, String scheme, ObjectId rev)
     103             :       throws IOException {
     104           0 :     return byAccount(accountId, rev).stream()
     105           0 :         .filter(e -> e.key().isScheme(scheme))
     106           0 :         .collect(toImmutableSet());
     107             :   }
     108             : 
     109             :   /** Returns all external IDs by account. */
     110             :   public ImmutableSetMultimap<Account.Id, ExternalId> allByAccount() throws IOException {
     111           0 :     return externalIdCache.allByAccount();
     112             :   }
     113             : 
     114             :   /**
     115             :    * Returns the external ID with the given email.
     116             :    *
     117             :    * <p>Each email should belong to a single external ID only. This means if more than one external
     118             :    * ID is returned there is an inconsistency in the external IDs.
     119             :    *
     120             :    * <p>The external IDs are retrieved from the external ID cache. Each access to the external ID
     121             :    * cache requires reading the SHA1 of the refs/meta/external-ids branch. If external IDs for
     122             :    * multiple emails are needed it is more efficient to use {@link #byEmails(String...)} as this
     123             :    * method reads the SHA1 of the refs/meta/external-ids branch only once (and not once per email).
     124             :    *
     125             :    * @see #byEmails(String...)
     126             :    */
     127             :   public ImmutableSet<ExternalId> byEmail(String email) throws IOException {
     128         108 :     return externalIdCache.byEmail(email);
     129             :   }
     130             : 
     131             :   /**
     132             :    * Returns the external IDs for the given emails.
     133             :    *
     134             :    * <p>Each email should belong to a single external ID only. This means if more than one external
     135             :    * ID for an email is returned there is an inconsistency in the external IDs.
     136             :    *
     137             :    * <p>The external IDs are retrieved from the external ID cache. Each access to the external ID
     138             :    * cache requires reading the SHA1 of the refs/meta/external-ids branch. If external IDs for
     139             :    * multiple emails are needed it is more efficient to use this method instead of {@link
     140             :    * #byEmail(String)} as this method reads the SHA1 of the refs/meta/external-ids branch only once
     141             :    * (and not once per email).
     142             :    *
     143             :    * @see #byEmail(String)
     144             :    */
     145             :   public ImmutableSetMultimap<String, ExternalId> byEmails(String... emails) throws IOException {
     146           1 :     return externalIdCache.byEmails(emails);
     147             :   }
     148             : 
     149             :   /** Returns all external IDs by email. */
     150             :   public ImmutableSetMultimap<String, ExternalId> allByEmail() throws IOException {
     151           0 :     return externalIdCache.allByEmail();
     152             :   }
     153             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750