You are here

private function LoginHandler::registerNewUser in Shibboleth Authentication 8

Adds user to the shib_auth table in the database.

Parameters

bool $success:

Return value

bool

Throws

\Exception

1 call to LoginHandler::registerNewUser()
LoginHandler::shibLogin in src/Login/LoginHandler.php

File

src/Login/LoginHandler.php, line 206

Class

LoginHandler
Class LoginHandler.

Namespace

Drupal\shib_auth\Login

Code

private function registerNewUser($success = FALSE) {
  $user_data = [
    'name' => $this->shib_session
      ->getTargetedId(),
    'mail' => $this->custom_data_store
      ->get('custom_email'),
    'pass' => $this
      ->genPassword(),
    'status' => 1,
  ];
  try {

    // Create Drupal user.
    $this->user = $this->user_store
      ->create($user_data);
    if (!($results = $this->user
      ->save())) {

      // Throw exception if Drupal user creation fails.
      throw new \Exception();
    }
  } catch (\Exception $e) {
    if ($e
      ->getCode() == self::MYSQL_ER_DUP_KEY) {
      $this
        ->setErrorMessage(t('There was an error creating your user. A user with your email address already exists.'));
      throw new \Exception('Error creating new Drupal user from Shibboleth Session. Duplicate user row.');
    }
    else {
      $this
        ->setErrorMessage(t('There was an error creating your user.'));
      throw new \Exception('Error creating new Drupal user from Shibboleth Session.');
    }
  }
  try {

    // Insert shib data into shib_authmap table.
    $shib_data = [
      'uid' => $this->user
        ->id(),
      'targeted_id' => $this->shib_session
        ->getTargetedId(),
      'idp' => $this->shib_session
        ->getIdp(),
      'created' => \Drupal::time()
        ->getRequestTime(),
    ];
    $success = $this->db
      ->insert('shib_authmap')
      ->fields($shib_data)
      ->execute();
    if (!$success) {

      // Throw exception if shib_authmap insert fails.
      throw new \Exception();
    }
  } catch (\Exception $e) {
    $this
      ->setErrorMessage(t('There was an error creating your user.'));
    throw new \Exception('Error creating new Drupal user from Shibboleth Session. Database insert on shib_authmap failed.');
  }
  return TRUE;
}