You are here

MasqueradeCacheContext.php in Masquerade 8.2

File

src/Cache/MasqueradeCacheContext.php
View source
<?php

namespace Drupal\masquerade\Cache;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CacheContextInterface;
use Drupal\Core\Cache\Context\RequestStackCacheContextBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines the MasqueradeCacheContext service, for "masquerade" caching.
 *
 * Cache context ID: 'session.is_masquerading'.
 */
class MasqueradeCacheContext extends RequestStackCacheContextBase implements CacheContextInterface {

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return new TranslatableMarkup('User is masquerading');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {
    if ($request = $this->requestStack
      ->getCurrentRequest()) {
      if ($request
        ->hasSession() && ($session = $request
        ->getSession())) {
        if ($session
          ->has('masquerading')) {

          // Previous account supposed to be Authenticated.
          return '1';
        }
      }
    }
    return '0';
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    return new CacheableMetadata();
  }

}

Classes

Namesort descending Description
MasqueradeCacheContext Defines the MasqueradeCacheContext service, for "masquerade" caching.