You are here

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

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

File

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

Class

BootSubscriber
For handling chocolatechip cookie on boot.

Namespace

Drupal\bakery\EventSubscriber

Code

private function repairInit($cookie) {

  /** @var int $count */
  $count = $this->userStorage
    ->getQuery()
    ->condition('init', $cookie['init'])
    ->count()
    ->execute();
  if ($count > 1) {

    // Uh oh.
    $this
      ->getLogger('bakery')
      ->notice('Account uniqueness problem: Multiple users found with init %init.', [
      '%init' => $cookie['init'],
    ]);
    $this
      ->messenger()
      ->addError($this
      ->t('Account uniqueness problem detected. <a href="@contact">Please contact the site administrator.</a>', [
      '@contact' => $this
        ->getConfig()
        ->get('bakery_master') . 'contact',
    ]));
  }
  if ($count == 1) {

    /** @var \Drupal\user\UserInterface[] $account */
    $account = $this->userStorage
      ->loadByProperties([
      'init' => $cookie['init'],
    ]);
    if (is_array($account)) {
      $account = reset($account);
    }
    if ($account) {
      $this
        ->getLogger('bakery')
        ->notice('Fixing out of sync uid %uid. Changed name %name_old to %name_new, mail %mail_old to %mail_new.', [
        '%uid' => $account
          ->id(),
        '%name_old' => $account
          ->getAccountName(),
        '%name_new' => $cookie['name'],
        '%mail_old' => $account
          ->getEmail(),
        '%mail_new' => $cookie['mail'],
      ]);
      $account
        ->setEmail($cookie['mail']);
      $account
        ->setUsername($cookie['name']);
      $account
        ->save();

      // Reload.

      /** @var \Drupal\user\UserInterface[] $account */
      $account = $this->userStorage
        ->loadByProperties([
        'name' => $cookie['name'],
        'mail' => $cookie['mail'],
      ]);
      return reset($account);
    }
  }
  return FALSE;
}