|
|
|
@ -8,6 +8,9 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
|
|
use Drupal\Core\Routing\TrustedRedirectResponse; |
|
|
|
use Drupal\Core\Routing\TrustedRedirectResponse; |
|
|
|
use Drupal\Core\Session\AccountProxyInterface; |
|
|
|
use Drupal\Core\Session\AccountProxyInterface; |
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
|
|
|
|
|
|
|
use Drupal\Core\Messenger\MessengerInterface; |
|
|
|
|
|
|
|
use Drupal\Core\Url; |
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Subscribes to kernel request events to redirect users based on permissions. |
|
|
|
* Subscribes to kernel request events to redirect users based on permissions. |
|
|
|
@ -31,6 +34,13 @@ class RedirectSubscriber implements EventSubscriberInterface { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected $configFactory; |
|
|
|
protected $configFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The Messenger service. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @var \Drupal\Core\Messenger\ |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected $messenger; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Constructs a new RedirectSubscriber. |
|
|
|
* Constructs a new RedirectSubscriber. |
|
|
|
* |
|
|
|
* |
|
|
|
@ -38,10 +48,13 @@ class RedirectSubscriber implements EventSubscriberInterface { |
|
|
|
* The current user. |
|
|
|
* The current user. |
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
|
|
|
* The config factory. |
|
|
|
* The config factory. |
|
|
|
|
|
|
|
* @param Drupal\Core\Messenger\MessengerInterface $messenger |
|
|
|
|
|
|
|
* The messenger. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function __construct(AccountProxyInterface $current_user, ConfigFactoryInterface $config_factory) { |
|
|
|
public function __construct(AccountProxyInterface $current_user, ConfigFactoryInterface $config_factory, MessengerInterface $messenger) { |
|
|
|
$this->currentUser = $current_user; |
|
|
|
$this->currentUser = $current_user; |
|
|
|
$this->configFactory = $config_factory; |
|
|
|
$this->configFactory = $config_factory; |
|
|
|
|
|
|
|
$this->messenger = $messenger; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -50,13 +63,13 @@ class RedirectSubscriber implements EventSubscriberInterface { |
|
|
|
public function onRequest(RequestEvent $event) { |
|
|
|
public function onRequest(RequestEvent $event) { |
|
|
|
$request = $event->getRequest(); |
|
|
|
$request = $event->getRequest(); |
|
|
|
$host = $request->getHost(); |
|
|
|
$host = $request->getHost(); |
|
|
|
|
|
|
|
$uri = $request->getRequestUri(); |
|
|
|
$config = $this->configFactory->get('url_permission_redirect.settings'); |
|
|
|
$config = $this->configFactory->get('url_permission_redirect.settings'); |
|
|
|
$protectedDomain = $config->get('protected_domain') ?? FALSE; |
|
|
|
$protectedDomain = $config->get('protected_domain') ?? FALSE; |
|
|
|
$uri = $request->getRequestUri(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Redirect logged-in users with permission. |
|
|
|
// Redirect logged-in users with access permission to protected domain. |
|
|
|
if ($protectedDomain && $this->currentUser->isAuthenticated() && |
|
|
|
if ($protectedDomain && $this->currentUser->isAuthenticated() && |
|
|
|
$this->currentUser->hasPermission('access protected domain')) { |
|
|
|
$this->currentUser->hasPermission('access protected domain')) { |
|
|
|
if ($host !== $protectedDomain) { |
|
|
|
if ($host !== $protectedDomain) { |
|
|
|
$redirect_url = 'https://' . $protectedDomain . $uri; |
|
|
|
$redirect_url = 'https://' . $protectedDomain . $uri; |
|
|
|
$event->setResponse(new TrustedRedirectResponse($redirect_url, 302)); |
|
|
|
$event->setResponse(new TrustedRedirectResponse($redirect_url, 302)); |
|
|
|
@ -64,17 +77,30 @@ class RedirectSubscriber implements EventSubscriberInterface { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Redirect anonymous users trying to log in via the public domain. |
|
|
|
// Redirect anonymous users attempting to log in from public domain. |
|
|
|
if ($host !== $protectedDomain && $uri === '/user/login') { |
|
|
|
if ($host !== $protectedDomain && $uri === '/user/login' && $this->currentUser->isAnonymous()) { |
|
|
|
$destination = $request->query->get('destination'); |
|
|
|
$destination = $request->query->get('destination'); |
|
|
|
$redirect_url = 'https://' . $protectedDomain . '/user/login'; |
|
|
|
$redirect_url = 'https://' . $protectedDomain . '/user?redirect_message=1'; |
|
|
|
|
|
|
|
|
|
|
|
// Preserve destination if it exists. |
|
|
|
|
|
|
|
if ($destination) { |
|
|
|
if ($destination) { |
|
|
|
$redirect_url .= '?destination=' . urlencode($destination); |
|
|
|
$redirect_url .= '&destination=' . urlencode($destination); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$event->setResponse(new TrustedRedirectResponse($redirect_url, 302)); |
|
|
|
$event->setResponse(new TrustedRedirectResponse($redirect_url, 302)); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Show redirect message on target domain if query parameter is present. |
|
|
|
|
|
|
|
if ($host === $protectedDomain && $request->query->get('redirect_message') === '1') { |
|
|
|
|
|
|
|
$this->messenger->addStatus('You were redirected here to log in securely.'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Clean the query string by removing redirect_message and reloading. |
|
|
|
|
|
|
|
$query = $request->query->all(); |
|
|
|
|
|
|
|
unset($query['redirect_message']); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$current_path = $request->getPathInfo(); |
|
|
|
|
|
|
|
$clean_url = Url::fromUri('internal:' . $current_path, ['query' => $query])->toString(); |
|
|
|
|
|
|
|
$event->setResponse(new RedirectResponse($clean_url, 302)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|