You are here

public function SecureLoginResponseSubscriber::onRespond in Secure Login 8

Redirects login attempts on already-logged-in session to the destination.

@todo Should accept a ResponseEvent.

File

src/SecureLoginResponseSubscriber.php, line 81

Class

SecureLoginResponseSubscriber
Listens for login to the 403 page and redirects to destination.

Namespace

Drupal\securelogin

Code

public function onRespond($event) {

  // Return early in most cases.
  if ($event
    ->getRequest()
    ->getMethod() !== 'POST') {
    return;
  }
  if (!$this->currentUser
    ->isAuthenticated()) {
    return;
  }
  if (!$event
    ->isMasterRequest()) {
    return;
  }
  if (!$event
    ->getRequest()->query
    ->has('destination')) {
    return;
  }
  if ($event
    ->getResponse() instanceof RedirectResponse) {
    return;
  }

  // @todo Find a better way to figure out if we landed on the 403/404 page.
  $page_403 = $this->configFactory
    ->get('system.site')
    ->get('page.403');
  $page_404 = $this->configFactory
    ->get('system.site')
    ->get('page.404');
  $path = $this->currentPath
    ->getPath();
  $route = $this->routeMatch
    ->getRouteName();
  if ($route == 'system.403' || $page_403 && $path == $page_403 || $route == 'system.404' || $page_404 && $path == $page_404) {

    // RedirectResponseSubscriber will convert to absolute URL for us.
    $event
      ->setResponse(new RedirectResponse($this->redirectDestination
      ->get(), RedirectResponse::HTTP_SEE_OTHER));
  }
}