PagesRestrictionSubscriber.php in Pages Restriction Access 8
File
src/Event/PagesRestrictionSubscriber.php
View source
<?php
namespace Drupal\pages_restriction\Event;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Config\ConfigFactory;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\pages_restriction\Service\PagesRestrictionHelper;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\Core\Url;
class PagesRestrictionSubscriber implements EventSubscriberInterface {
protected $pagesRestrictionHelper;
protected $pathMatcher;
protected $request;
protected $configFactory;
protected $account;
private $session;
private $currentPath;
protected $aliasManager;
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;
}
public function onRequestCheckRestrictedPages(GetResponseEvent $event) {
$config = $this->configFactory
->get('pages_restriction.settings');
$roles = $this->account
->getRoles();
$bypass_role = $config
->get('bypass_role');
if (!$this->account
->isAnonymous()) {
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;
}
$currentPath = $this->currentPath
->getPath();
$currentPath = $this->aliasManager
->getAliasByPath($currentPath);
$pages_restriction_bypass = $this->session
->get('pages_restriction_bypass');
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;
}
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'onRequestCheckRestrictedPages',
215,
];
return $events;
}
}