LCOV - code coverage report
Current view: top level - server/account - DefaultRealm.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 21 29 72.4 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2009 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 com.google.common.annotations.VisibleForTesting;
      18             : import com.google.common.base.Strings;
      19             : import com.google.gerrit.common.Nullable;
      20             : import com.google.gerrit.entities.Account;
      21             : import com.google.gerrit.exceptions.StorageException;
      22             : import com.google.gerrit.extensions.client.AccountFieldName;
      23             : import com.google.gerrit.extensions.client.AuthType;
      24             : import com.google.gerrit.server.config.AuthConfig;
      25             : import com.google.inject.Inject;
      26             : import com.google.inject.Provider;
      27             : import com.google.inject.Singleton;
      28             : import java.io.IOException;
      29             : import java.util.Set;
      30             : 
      31             : @Singleton
      32             : public class DefaultRealm extends AbstractRealm {
      33             :   private final EmailExpander emailExpander;
      34             :   private final Provider<Emails> emails;
      35             :   private final AuthConfig authConfig;
      36             : 
      37             :   @Inject
      38             :   @VisibleForTesting
      39         152 :   public DefaultRealm(EmailExpander emailExpander, Provider<Emails> emails, AuthConfig authConfig) {
      40         152 :     this.emailExpander = emailExpander;
      41         152 :     this.emails = emails;
      42         152 :     this.authConfig = authConfig;
      43         152 :   }
      44             : 
      45             :   @Override
      46             :   public boolean allowsEdit(AccountFieldName field) {
      47          10 :     if (authConfig.getAuthType() == AuthType.HTTP) {
      48           1 :       switch (field) {
      49             :         case USER_NAME:
      50           1 :           return false;
      51             :         case FULL_NAME:
      52           1 :           return Strings.emptyToNull(authConfig.getHttpDisplaynameHeader()) == null;
      53             :         case REGISTER_NEW_EMAIL:
      54           1 :           return authConfig.isAllowRegisterNewEmail()
      55           1 :               && Strings.emptyToNull(authConfig.getHttpEmailHeader()) == null;
      56             :         default:
      57           0 :           return true;
      58             :       }
      59             :     }
      60          10 :     switch (field) {
      61             :       case REGISTER_NEW_EMAIL:
      62           6 :         return authConfig.isAllowRegisterNewEmail();
      63             :       case FULL_NAME:
      64             :       case USER_NAME:
      65             :       default:
      66           7 :         return true;
      67             :     }
      68             :   }
      69             : 
      70             :   @Override
      71             :   public AuthRequest authenticate(AuthRequest who) {
      72          15 :     if (who.getEmailAddress() == null
      73          15 :         && who.getLocalUser() != null
      74          15 :         && emailExpander.canExpand(who.getLocalUser())) {
      75           0 :       who.setEmailAddress(emailExpander.expand(who.getLocalUser()));
      76             :     }
      77          15 :     return who;
      78             :   }
      79             : 
      80             :   @Override
      81          15 :   public void onCreateAccount(AuthRequest who, Account account) {}
      82             : 
      83             :   @Nullable
      84             :   @Override
      85             :   public Account.Id lookup(String accountName) throws IOException {
      86          33 :     if (emailExpander.canExpand(accountName)) {
      87             :       try {
      88           0 :         Set<Account.Id> c = emails.get().getAccountFor(emailExpander.expand(accountName));
      89           0 :         if (1 == c.size()) {
      90           0 :           return c.iterator().next();
      91             :         }
      92           0 :       } catch (StorageException e) {
      93           0 :         throw new IOException("Failed to query accounts by email", e);
      94           0 :       }
      95             :     }
      96          33 :     return null;
      97             :   }
      98             : }

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