You are here

private function BootSubscriber::bootstrapAccount in Bakery Single Sign-On System 8.2

1 call to BootSubscriber::bootstrapAccount()
BootSubscriber::somethingAnonymous in src/EventSubscriber/BootSubscriber.php

File

src/EventSubscriber/BootSubscriber.php, line 250
For Boot event subscribe.

Class

BootSubscriber
For handling chocolatechip cookie on boot.

Namespace

Drupal\bakery\EventSubscriber

Code

private function bootstrapAccount(GetResponseEvent $event, array $cookie) {
  $checks = TRUE;

  /** @var int $mail_count */
  $mail_count = $this->userStorage
    ->getQuery()
    ->condition('uid', 0, '!=')
    ->condition('mail', '', '!=')
    ->condition('mail', $cookie['mail'], 'LIKE')
    ->count()
    ->execute();
  if ($mail_count > 0) {
    $checks = FALSE;
  }

  /** @var int $name_count */
  $name_count = $this->userStorage
    ->getQuery()
    ->condition('uid', 0, '!=')
    ->condition('name', $cookie['name'], 'LIKE')
    ->count()
    ->execute();
  if ($name_count > 0) {
    $checks = FALSE;
  }

  /** @var int $init_count */
  $init_count = $this->userStorage
    ->getQuery()
    ->condition('uid', 0, '!=')
    ->condition('init', $cookie['init'], '=')
    ->condition('name', $cookie['name'], 'LIKE')
    ->count()
    ->execute();
  if ($init_count > 0) {
    $checks = FALSE;
  }
  if ($checks) {

    // Request information from master to keep data in sync.
    $uid = $this->bakeryService
      ->requestAccount($cookie['name']);

    // In case the account creation failed we want to make sure the user
    // gets their bad cookie destroyed by not returning too early.
    if ($uid) {
      return $this->userStorage
        ->load($uid);
    }
    else {
      $this->kitchen
        ->eat(Kitchen::CHOCOLATE_CHIP);
    }
  }
  else {
    $this
      ->messenger()
      ->addStatus(t('Your user account on %site appears to have problems. Would you like to try to <a href=":url">repair it yourself</a>?', [
      '%site' => \Drupal::config('system.site')
        ->get('name'),
      ':url' => Url::fromRoute('bakery.repair')
        ->toString(),
    ]));
    $this
      ->messenger()
      ->addStatus(Xss::filter($this
      ->getConfig()
      ->get('bakery_help_text')));
    $event
      ->getRequest()
      ->getSession()
      ->set('BAKERY_CRUMBLED', TRUE);
  }
  return FALSE;
}