5

I have Drupal 8.5.1 and I am trying to force a redirect for anonymous users to the CAS login page. First I thought that the issue was related with the firing of the checkAuthStatus function below, however, the issue seems related to an endless redirect:

namespace Drupal\my_custom_module\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event subscriber subscribing to KernelEvents::REQUEST.
 */
class RedirectAnonymousSubscriber implements EventSubscriberInterface {

  public function __construct() {
    $this->account = \Drupal::currentUser();
  }

  public function checkAuthStatus(GetResponseEvent $event) {
    \Drupal::logger('my_custom_module')->error('THIS IS A TEST');
    if ($this->account->isAnonymous()
        && \Drupal::routeMatch()->getRouteName() != 'cas.legacy_login'
        && \Drupal::routeMatch()->getRouteName() != 'cas.service') {
      $response = new RedirectResponse('/cas', 301);
      $response->send();
    }
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['checkAuthStatus', 27];
    return $events;
  }

}

What could be the issue?

18
  • 1
    It should be fired on every page, are you sure the index page isn't just cached? Maybe try returning a CacheableRedirectResponse instead
    – Clive
    Mar 29, 2018 at 10:49
  • 2
    The problem is, you should never send a response inside of a StackPHP application. Drupal continues to build its own response (the real frontpage) and stores this in the cache. Set the response in the event instead: $event->setResponse($response);
    – 4uk4
    Mar 29, 2018 at 11:35
  • 1
    @Clive I have added logger calls and they are only executed when the visited URL is an internal page. On the front page is not being executed. So that means the RedirectResponse object is only created on internal pages, so using the CacheableRedirectResponse will not make any difference.
    – MmMnMr
    Mar 29, 2018 at 11:41
  • 1
    The front page is an internal page, I'm not sure what you mean
    – Clive
    Mar 29, 2018 at 11:42
  • 1
    I have executed drush cr multiple times to clear the cache, but the issue continues.
    – MmMnMr
    Mar 29, 2018 at 11:42

1 Answer 1

7

Finally I have solved the issue thanks to the comments I received on my question. Here is the solution I found:

namespace Drupal\my_custom_module\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event subscriber subscribing to KernelEvents::REQUEST.
 */
class RedirectAnonymousSubscriber implements EventSubscriberInterface {

  public function __construct() {
    $this->account = \Drupal::currentUser();
  }

  public function checkAuthStatus(GetResponseEvent $event) {
    if ($this->account->isAnonymous()
      && \Drupal::service('path.current')->getPath() != '/cas'
      && \Drupal::service('path.current')->getPath() != '/casservice') {
      $response = new RedirectResponse('/cas', 301);
      $event->setResponse($response);
    }
    return;
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['checkAuthStatus', 100];
    return $events;
  }

}

In short, the issue was related with the path checking and the RedirectResponse object management.

1
  • 1
    You needed an explicit return() at the end of the function?
    – user1359
    Jan 29, 2019 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.