You are here

public function BakeryService::requestAccount in Bakery Single Sign-On System 8.2

Request account information from master to create account locally.

Parameters

string $name: The username or e-mail to request information for to create.

bool $or_email: Load account by name or email. Useful for getting account data from a password request where you get name or email.

Return value

int|false The newly created local UID or FALSE.

File

src/BakeryService.php, line 101
Services used in bakery SSO functions.

Class

BakeryService
Common functionalities used in both controller and module.

Namespace

Drupal\bakery

Code

public function requestAccount($name, $or_email = FALSE) {
  global $base_url;
  $existing_account = user_load_by_name($name);
  if (!$existing_account && $or_email) {
    $existing_account = user_load_by_mail($name);
  }

  // We return FALSE in cases that the account already exists locally or if
  // there was an error along the way of requesting and creating it.
  if ($existing_account) {
    return FALSE;
  }

  // Save a stub account so we have a slave UID to send.
  $language = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $account = User::create([
    'name' => $name,
    // 'mail' => 'email',
    'pass' => user_password(),
    // This username must be unique and accept only a-Z,0-9, - _ @ .
    'init' => 'bakery_temp/' . mt_rand(),
    'langcode',
    $language,
    'preferred_langcode',
    $language,
    'preferred_admin_langcode',
    $language,
    'status' => 1,
  ]);

  // Save user.
  try {
    $account
      ->enforceIsNew();
    $account
      ->save();
  } catch (EntityStorageException $e) {
    \Drupal::logger('bakery')
      ->error('Unable to create stub account for @name', [
      '@name' => $name,
    ]);
    return FALSE;
  }
  $response = $this->kitchen
    ->ship(new Gingerbread($name, $or_email, rtrim($base_url, '/') . '/', $account
    ->id()));
  if ($response && $response
    ->getStatusCode() == 200) {

    // Parse result and create account.
    $cookie = $this->kitchen
      ->tasteData($response
      ->getBody());
    if ($cookie === FALSE) {

      // Invalid response.
      \Drupal::logger('bakery')
        ->error('Invalid response from master when attempting to create local account for @name', [
        '@name' => $name,
      ]);
      $account
        ->delete();
      return FALSE;
    }

    // Create account.
    $account
      ->set('init', $cookie['uid']);
    $this
      ->updateUserFields($account, $cookie);

    // Save user.
    if ($account
      ->save()) {
      \Drupal::logger('bakery')
        ->notice('Created account for @name', [
        '@name' => $name,
      ]);
      return $account
        ->id();
    }
  }
  if ($response) {
    $message = $response
      ->getBody();
    \Drupal::logger('bakery')
      ->error('Received response !code from master with message @message', [
      '!code' => $response
        ->getStatusCode(),
      '@message' => (string) $message,
    ]);
  }
  else {
    \Drupal::logger('bakery')
      ->error('Unable to create account for @name', [
      '@name' => $name,
    ]);
  }
  $account
    ->delete();
  return FALSE;
}