You are here

public function SamlUserService::handleSamlData in SAML Authentication 8

Take appropriate action on provided SAML data.

Parameters

array $saml_data:

Throws

\Exception

File

src/SamlUserService.php, line 76
Contains Drupal\samlauth\SamlService.

Class

SamlUserService
Class SamlUserService.

Namespace

Drupal\samlauth

Code

public function handleSamlData(array $saml_data) {
  $unique_id_attribute = $this->config
    ->get('unique_id_attribute');

  // We depend on the unique ID being present, so make sure it's there.
  if (!isset($saml_data[$unique_id_attribute][0])) {
    throw new Exception('Configured unique ID is not present in SAML response!');
  }
  $unique_id = $saml_data[$unique_id_attribute][0];
  $uid = $this
    ->findUidByUniqueId($unique_id);
  if (!$uid) {
    $mail_attribute = $this->config
      ->get('map_users_email');
    if ($this->config
      ->get('map_users') && ($account = user_load_by_mail($saml_data[$mail_attribute]))) {
      $this
        ->associateSamlIdWithAccount($unique_id, $account);
    }
    else {
      if ($this->config
        ->get('create_users')) {
        $account = $this
          ->createUserFromSamlData($saml_data);
      }
      else {
        throw new Exception('No existing user account matches the SAML ID provided. This authentication service is not configured to create new accounts.');
      }
    }
  }
  else {
    $account = User::load($uid);
  }
  if ($account
    ->isBlocked()) {
    throw new Exception('Requested account is blocked.');
  }
  user_login_finalize($account);
}