You are here

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

1 call to BootSubscriber::somethingAnonymous()
BootSubscriber::onEvent in src/EventSubscriber/BootSubscriber.php
On boot event we need to test the cookie.

File

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

Class

BootSubscriber
For handling chocolatechip cookie on boot.

Namespace

Drupal\bakery\EventSubscriber

Code

private function somethingAnonymous(GetResponseEvent $event, array $cookie) {

  // User is anonymous. If they do not have an account we'll create one by
  // requesting their information from the master site. If they do have an
  // account we may need to correct some disparant information.

  /** @var \Drupal\user\UserInterface[] $account */
  $account = $this->userStorage
    ->loadByProperties([
    'name' => $cookie['name'],
    'mail' => $cookie['mail'],
  ]);
  $account = reset($account);
  if ($this->bakeryService
    ->isChild()) {

    // Fix out of sync users with valid init.
    if (!$account && $cookie['master']) {
      $account = $this
        ->repairInit($cookie);
    }

    // Create the account if it doesn't exist.
    if (!$account && $cookie['master']) {
      $account = $this
        ->bootstrapAccount($event, $cookie);
    }
    if ($account && $cookie['master'] && $account
      ->id() && $account
      ->get('init')->value != $cookie['init']) {

      // User existed previously but init is wrong.
      // Fix it to ensure account remains in sync.
      // Make sure that there aren't any OTHER accounts with this init.

      /** @var int $count */
      $count = $this->userStorage
        ->getQuery()
        ->condition('init', $cookie['init'])
        ->count()
        ->execute();
      if ($count == 0) {
        $account
          ->set('init', $cookie['init'])
          ->save();
        $this
          ->getLogger('bakery')
          ->notice('uid %uid out of sync. Changed init field from %oldinit to %newinit', [
          '%oldinit' => $account
            ->getInitialEmail(),
          '%newinit' => $cookie['init'],
          '%uid' => $account
            ->id(),
        ]);
      }
      else {

        // Username and email matched,
        // but init belonged to a DIFFERENT account.
        // Something got seriously tangled up.
        $this
          ->getLogger('bakery')
          ->notice('Accounts mixed up! Username %user and init %init disagree with each other!', [
          '%user' => $account
            ->getAccountName(),
          '%init' => $cookie['init'],
        ]);
      }
    }
  }
  if ($account) {

    // If the login attempt fails we need to destroy the cookie to prevent
    // infinite redirects (with infinite failed login messages).
    $login = $this->bakeryService
      ->userExternalLogin($account);
    if ($login) {

      // If an anonymous user has just been logged in, trigger a 'refresh'
      // of the current page.
      // TODO take into account destination query.
      $event
        ->setResponse(new RedirectResponse(\Drupal::service('path.current')
        ->getPath()));
    }
    else {
      $this->kitchen
        ->eat(Kitchen::CHOCOLATE_CHIP);
    }
  }
}