You are here

public function CasUserManager::getEmailForNewAccount in CAS 2.x

Same name and namespace in other branches
  1. 8 src/Service/CasUserManager.php \Drupal\cas\Service\CasUserManager::getEmailForNewAccount()

Return the email address that should be assigned to an auto-register user.

Parameters

\Drupal\cas\CasPropertyBag $cas_property_bag: The CasPropertyBag associated with the user's login attempt.

Return value

string The email address.

Throws

\Drupal\cas\Exception\CasLoginException Thrown when the email address cannot be derived properly.

1 call to CasUserManager::getEmailForNewAccount()
CasUserManager::login in src/Service/CasUserManager.php
Attempts to log the user in to the Drupal site.

File

src/Service/CasUserManager.php, line 334

Class

CasUserManager
Provides the 'cas.user_manager' service default implementation.

Namespace

Drupal\cas\Service

Code

public function getEmailForNewAccount(CasPropertyBag $cas_property_bag) {
  $email_assignment_strategy = $this->settings
    ->get('cas.settings')
    ->get('user_accounts.email_assignment_strategy');
  if ($email_assignment_strategy === self::EMAIL_ASSIGNMENT_STANDARD) {
    return $cas_property_bag
      ->getUsername() . '@' . $this->settings
      ->get('cas.settings')
      ->get('user_accounts.email_hostname');
  }
  elseif ($email_assignment_strategy === self::EMAIL_ASSIGNMENT_ATTRIBUTE) {
    $email_attribute = $this->settings
      ->get('cas.settings')
      ->get('user_accounts.email_attribute');
    if (empty($email_attribute) || !array_key_exists($email_attribute, $cas_property_bag
      ->getAttributes())) {
      throw new CasLoginException('Specified CAS email attribute does not exist.', CasLoginException::ATTRIBUTE_PARSING_ERROR);
    }
    $val = $cas_property_bag
      ->getAttributes()[$email_attribute];
    if (empty($val)) {
      throw new CasLoginException('Empty data found for CAS email attribute.', CasLoginException::ATTRIBUTE_PARSING_ERROR);
    }

    // The attribute value may actually be an array of values, but we need it
    // to only contain 1 value.
    if (is_array($val) && count($val) !== 1) {
      throw new CasLoginException('Specified CAS email attribute was formatted in an unexpected way.', CasLoginException::ATTRIBUTE_PARSING_ERROR);
    }
    if (is_array($val)) {
      $val = $val[0];
    }
    return trim($val);
  }
  else {
    throw new CasLoginException('Invalid email address assignment type for auto user registration specified in settings.');
  }
}