You are here

class PagesRestrictionSubscriber in Pages Restriction Access 8

Default class for Subscriber.

Hierarchy

  • class \Drupal\pages_restriction\Event\PagesRestrictionSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PagesRestrictionSubscriber

1 string reference to 'PagesRestrictionSubscriber'
pages_restriction.services.yml in ./pages_restriction.services.yml
pages_restriction.services.yml
1 service uses PagesRestrictionSubscriber
pages_restriction.event_subscriber in ./pages_restriction.services.yml
Drupal\pages_restriction\Event\PagesRestrictionSubscriber

File

src/Event/PagesRestrictionSubscriber.php, line 23

Namespace

Drupal\pages_restriction\Event
View source
class PagesRestrictionSubscriber implements EventSubscriberInterface {

  /**
   * PagesRestrictionHelper.
   *
   * @var \Drupal\pages_restriction\Service\PagesRestrictionHelper
   */
  protected $pagesRestrictionHelper;

  /**
   * Path Matcher.
   *
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  protected $pathMatcher;

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The current account.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $account;

  /**
   * Symfony session handler.
   *
   * @var \Symfony\Component\HttpFoundation\Session\Session
   */
  private $session;

  /**
   * Current Path Stack.
   *
   * @var Drupal\Core\Path\CurrentPathStack
   */
  private $currentPath;

  /**
   * Alias Manager.
   *
   * @var \Drupal\Core\Path\AliasManager
   */
  protected $aliasManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactory $config_factory, RequestStack $request_stack, PathMatcherInterface $path_matcher, PagesRestrictionHelper $pages_restriction_helper, AccountProxyInterface $account, Session $session, CurrentPathStack $current_path, AliasManagerInterface $alias_manager) {
    $this->pathMatcher = $path_matcher;
    $this->request = $request_stack
      ->getCurrentRequest();
    $this->configFactory = $config_factory;
    $this->pagesRestrictionHelper = $pages_restriction_helper;
    $this->account = $account;
    $this->session = $session;
    $this->currentPath = $current_path;
    $this->aliasManager = $alias_manager;
  }

  /**
   * On Request Check Restricted Pages.
   */
  public function onRequestCheckRestrictedPages(GetResponseEvent $event) {
    $config = $this->configFactory
      ->get('pages_restriction.settings');
    $roles = $this->account
      ->getRoles();
    $bypass_role = $config
      ->get('bypass_role');

    // Check bypass only for logged user.
    if (!$this->account
      ->isAnonymous()) {

      // Check roles.
      foreach ($roles as $rid) {
        if (!empty($bypass_role[$rid])) {
          return FALSE;
        }
      }
    }
    $restrictedPages = $config
      ->get('pages_restriction');
    if (empty($restrictedPages)) {
      return FALSE;
    }
    $restrictedPages = explode(PHP_EOL, $restrictedPages);
    if (empty($this->request
      ->getRequestUri())) {
      return FALSE;
    }

    // Get current path.
    $currentPath = $this->currentPath
      ->getPath();
    $currentPath = $this->aliasManager
      ->getAliasByPath($currentPath);

    // Check for bypass on session.
    $pages_restriction_bypass = $this->session
      ->get('pages_restriction_bypass');

    // If user has bypass skip.
    if (!empty($pages_restriction_bypass) && in_array($currentPath, $pages_restriction_bypass)) {
      return FALSE;
    }
    $restrictedPaths = (array) $this->pagesRestrictionHelper
      ->getRestrictedPagesByConfig($restrictedPages);
    if (empty($restrictedPaths) || !in_array($currentPath, $restrictedPaths)) {
      return FALSE;
    }
    foreach ($restrictedPages as $restrictedPage) {
      if (empty($restrictedPage[0]) || empty($restrictedPage[1])) {
        continue;
      }
      $restrictedPage = explode('|', $restrictedPage);
      $restrictedPath = Xss::filter($restrictedPage[0]);
      $restrictedPath = trim($restrictedPath);
      if ($restrictedPath != $currentPath) {
        continue;
      }
      $targetPage = Xss::filter($restrictedPage[1]);
      $targetPage = trim($targetPage);
      if (!empty($config
        ->get('keep_parameters'))) {
        $queryParameters = $this->request->query
          ->all();
        $queryParameters = [
          'query' => $queryParameters,
        ];
        $targetPage = Url::fromUserInput($targetPage, $queryParameters)
          ->toString();
      }
      $response = new RedirectResponse($targetPage);
      $response
        ->send();
      $event
        ->stopPropagation();
      exit;
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'onRequestCheckRestrictedPages',
      215,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PagesRestrictionSubscriber::$account protected property The current account.
PagesRestrictionSubscriber::$aliasManager protected property Alias Manager.
PagesRestrictionSubscriber::$configFactory protected property The config factory.
PagesRestrictionSubscriber::$currentPath private property Current Path Stack.
PagesRestrictionSubscriber::$pagesRestrictionHelper protected property PagesRestrictionHelper.
PagesRestrictionSubscriber::$pathMatcher protected property Path Matcher.
PagesRestrictionSubscriber::$request protected property The current request.
PagesRestrictionSubscriber::$session private property Symfony session handler.
PagesRestrictionSubscriber::getSubscribedEvents public static function
PagesRestrictionSubscriber::onRequestCheckRestrictedPages public function On Request Check Restricted Pages.
PagesRestrictionSubscriber::__construct public function