Last active
April 2, 2023 19:16
Revisions
-
jorislucius revised this gist
Jun 22, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ <?php namespace Drupal\<yourmodulename>\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\Event\GetResponseEvent; -
jorislucius created this gist
Jun 22, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ <?php namespace Drupal\openlucius_core\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; /** * Event subscriber subscribing to KernelEvents::REQUEST. */ class RedirectAnonymousSubscriber implements EventSubscriberInterface { public function checkAuthStatus(GetResponseEvent $event) { global $base_url; if ( \Drupal::currentUser()->isAnonymous() && \Drupal::routeMatch()->getRouteName() != 'user.login' && \Drupal::routeMatch()->getRouteName() != 'user.reset' && \Drupal::routeMatch()->getRouteName() != 'user.reset.form' && \Drupal::routeMatch()->getRouteName() != 'user.reset.login' && \Drupal::routeMatch()->getRouteName() != 'user.pass' ) { // add logic to check other routes you want available to anonymous users, // otherwise, redirect to login page. $route_name = \Drupal::routeMatch()->getRouteName(); if (strpos($route_name, 'view') === 0 && strpos($route_name, 'rest_') !== FALSE) { return; } $response = new RedirectResponse($base_url . '/user/login', 301); $event->setResponse($response); $event->stopPropagation(); return; } } public static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = array('checkAuthStatus'); return $events; } }