You are here

protected function SamlUserService::createUserFromSamlData in SAML Authentication 8

Create a new user from SAML response data.

Parameters

array $saml_data:

Return value

static

Throws

\Exception

1 call to SamlUserService::createUserFromSamlData()
SamlUserService::handleSamlData in src/SamlUserService.php
Take appropriate action on provided SAML data.

File

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

Class

SamlUserService
Class SamlUserService.

Namespace

Drupal\samlauth

Code

protected function createUserFromSamlData(array $saml_data) {
  $user_unique_attribute = $this->config
    ->get('unique_id_attribute');
  $user_name_attribute = $this->config
    ->get('user_name_attribute');
  $user_mail_attribute = $this->config
    ->get('user_mail_attribute');
  if (!isset($saml_data[$user_name_attribute][0])) {
    throw new Exception('Missing name attribute.');
  }
  if (!isset($saml_data[$user_mail_attribute][0])) {
    throw new Exception('Missing mail attribute.');
  }
  $account = User::create();
  $account
    ->setUsername($saml_data[$user_name_attribute][0]);
  $account
    ->setPassword(user_password(50));
  $account
    ->setEmail($saml_data[$user_mail_attribute][0]);
  $account
    ->activate();

  // Allow other users to change/set user properties before saving.
  \Drupal::moduleHandler()
    ->alter('samlauth_new_user', $account, $saml_data);
  $account
    ->save();

  // Save the unique ID for later use.
  $this
    ->associateSamlIdWithAccount($saml_data[$user_unique_attribute][0], $account);
  return $account;
}