R4032LoginSubscriber.php in Redirect 403 to User Login 2.x
File
src/EventSubscriber/R4032LoginSubscriber.php
View source
<?php
namespace Drupal\r4032login\EventSubscriber;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableRedirectResponse;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\r4032login\Event\RedirectEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\Component\Utility\Xss;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class R4032LoginSubscriber extends HttpExceptionSubscriberBase {
protected $configFactory;
protected $currentUser;
protected $pathMatcher;
protected $eventDispatcher;
protected $messenger;
public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, PathMatcherInterface $path_matcher, EventDispatcherInterface $event_dispatcher, MessengerInterface $messenger) {
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
$this->pathMatcher = $path_matcher;
$this->eventDispatcher = $event_dispatcher;
$this->messenger = $messenger;
}
protected function getHandledFormats() {
return [
'html',
];
}
public function on403(GetResponseForExceptionEvent $event) {
$config = $this->configFactory
->get('r4032login.settings');
$request = $event
->getRequest();
$currentPath = $request
->getPathInfo();
if (($noRedirectPages = trim($config
->get('match_noredirect_pages'))) && $this->pathMatcher
->matchPath($currentPath, $noRedirectPages)) {
return;
}
if ($this->currentUser
->isAnonymous()) {
$redirectPath = $config
->get('user_login_path');
}
else {
$redirectPath = $config
->get('redirect_authenticated_users_to');
}
if (!empty($redirectPath)) {
$externalRedirect = UrlHelper::isExternal($redirectPath);
$options = [
'absolute' => TRUE,
];
if ($config
->get('redirect_to_destination')) {
if ($externalRedirect) {
$destination = Url::fromUserInput($currentPath, [
'absolute' => TRUE,
])
->toString();
}
elseif ($currentPath == '/') {
$destination = $currentPath;
}
else {
$destination = substr($currentPath, 1);
}
if ($queryString = $request
->getQueryString()) {
$destination .= '?' . $queryString;
}
if (empty($config
->get('destination_parameter_override'))) {
$options['query']['destination'] = $destination;
}
else {
$options['query'][$config
->get('destination_parameter_override')] = $destination;
}
}
$request->query
->remove('destination');
$redirectEvent = new RedirectEvent($redirectPath, $options);
$this->eventDispatcher
->dispatch(RedirectEvent::EVENT_NAME, $redirectEvent);
$redirectPath = $redirectEvent
->getUrl();
$options = $redirectEvent
->getOptions();
if ($externalRedirect) {
$url = Url::fromUri($redirectPath, $options)
->toString();
$response = new TrustedRedirectResponse($url);
}
else {
if ($this->currentUser
->isAnonymous() && $config
->get('display_denied_message')) {
$message = $config
->get('access_denied_message');
$messageType = $config
->get('access_denied_message_type');
$this->messenger
->addMessage(Markup::create(Xss::filterAdmin($message)), $messageType);
}
if ($redirectPath === '<front>') {
$url = \Drupal::urlGenerator()
->generate('<front>');
}
else {
$url = Url::fromUserInput($redirectPath, $options)
->toString();
}
$code = $config
->get('default_redirect_code');
$response = new CacheableRedirectResponse($url, $code);
}
$cacheMetadata = new CacheableMetadata();
$cacheMetadata
->addCacheTags([
'4xx-response',
]);
$cacheMetadata
->addCacheableDependency($config);
$response
->addCacheableDependency($cacheMetadata);
$event
->setResponse($response);
}
}
}