You are here

BakeryUserHooks.php in Bakery Single Sign-On System 8.2

Namespace

Drupal\bakery

File

src/BakeryUserHooks.php
View source
<?php

namespace Drupal\bakery;

use Drupal\bakery\Cookies\Stroopwafel;
use Drupal\bakery\Exception\MissingKeyException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
class BakeryUserHooks {

  /**
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * @var \Drupal\bakery\Kitchen
   */
  protected $kitchen;

  /**
   * When the service is created, set a value for the example variable.
   */
  public function __construct(Kitchen $kitchen, ConfigFactoryInterface $config_factory, AccountProxy $current_user) {
    $this->kitchen = $kitchen;
    $this->config = $config_factory
      ->get('bakery.settings');
    $this->currentUser = $current_user;
  }
  private function isMain() {
    return (bool) $this->config
      ->get('bakery_is_master');
  }
  public function login(UserInterface $account) {
    if ($this
      ->isMain() && $account
      ->id() != 0) {
      $this->kitchen
        ->reBakeChocolateChipCookie($account);
    }
  }
  public function logout(AccountInterface $account) {
    try {
      $cookie = $this->kitchen
        ->taste(Kitchen::CHOCOLATE_CHIP);

      // Only delete the SSO cookie if the name is the same in case there was an
      // existing session that's being logged out and SSO cookie is new session.
      if ($account
        ->id() && $cookie && $cookie['name'] === $account
        ->getAccountName()) {
        $this->kitchen
          ->eat(Kitchen::CHOCOLATE_CHIP);
      }
    } catch (MissingKeyException $exception) {

      // Let normal Drupal user processes do their thing until site is configured.
    }
  }
  public function presave(UserInterface $account) {

    // If this is the main site, its not the admin user, and there's updated
    // data, then we want to possibly send some updates.
    if ($this
      ->isMain() && $account
      ->id() > 1 && isset($account->original)) {

      // We store email/name if they changed. We want to wait with doing
      // anything else until the changes are saved locally.
      foreach ($this->config
        ->get('bakery_supported_fields') as $type => $enabled) {

        /** @var \Drupal\user\UserInterface $original */
        $original = $account->original;
        if ($enabled) {
          switch ($type) {
            case 'name':
              $o_v = $original
                ->getAccountName();
              $n_v = $account
                ->getAccountName();
              break;
            case 'mail':
              $o_v = $original
                ->getEmail();
              $n_v = $account
                ->getEmail();
              break;
            case 'status':
              $o_v = $original
                ->isActive();
              $n_v = $account
                ->isActive();
              break;
            default:

              // does this work?
              $o_v = $original
                ->get($type)
                ->getValue();
              $n_v = $account
                ->get($type)
                ->getValue();
              break;
          }
          if ($o_v != $n_v) {
            $_SESSION['bakery'][$type] = $n_v;
          }
        }
      }
    }
  }
  public function update(UserInterface $account) {

    // We need to push changes.
    if ($this
      ->isMain() && isset($_SESSION['bakery'])) {
      $this->kitchen
        ->ship(new Stroopwafel($account
        ->id(), $_SESSION['bakery']));
      unset($_SESSION['bakery']);
      if ($this->currentUser
        ->id() === $account
        ->id()) {

        // Rebake SSO cookie so user stays authenticated.
        $this->kitchen
          ->reBakeChocolateChipCookie($account);
      }
    }
  }
  public function view(UserInterface $account, &$build) {
    if (!$this
      ->isMain()) {
      $main_site = $this->config
        ->get('bakery_master');
      $init_url = _bakery_init_field_url($account
        ->getInitialEmail());
      if (parse_url($main_site, PHP_URL_HOST) == parse_url($init_url, PHP_URL_HOST)) {
        $build['summary']['primary_profile'] = [
          '#type' => 'item',
          '#title' => t('Primary profile'),
          // Take everything up to '/edit'.
          // TODO Will this create a url correctly?
          'link' => Link::fromTextAndUrl(t('Profile on primary site'), Url::fromUri(substr($init_url, 0, strlen($init_url) - 5)))
            ->toRenderable(),
          '#access' => $this->currentUser
            ->hasPermission('access user profiles'),
        ];
      }
    }
  }

}

Classes

Namesort descending Description
BakeryUserHooks