You are here

private function RequireLoginSubscriber::loginRedirect in Require Login 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/RequireLoginSubscriber.php \Drupal\require_login\EventSubscriber\RequireLoginSubscriber::loginRedirect()

Prepare login redirect response.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseEvent $event: The event response.

Return value

string|null The redirect URL.

2 calls to RequireLoginSubscriber::loginRedirect()
RequireLoginSubscriber::exceptionRedirect in src/EventSubscriber/RequireLoginSubscriber.php
Login redirect on KernelEvents::EXCEPTION.
RequireLoginSubscriber::requestRedirect in src/EventSubscriber/RequireLoginSubscriber.php
Login redirect on KernelEvents::REQUEST.

File

src/EventSubscriber/RequireLoginSubscriber.php, line 218

Class

RequireLoginSubscriber
Login requirement.

Namespace

Drupal\require_login\EventSubscriber

Code

private function loginRedirect(GetResponseEvent $event) {
  if ($this->accountProxy
    ->getAccount()
    ->isAuthenticated()) {

    // Stop if user is authenticated.
    return NULL;
  }
  $config = $this->configFactory
    ->get('require_login.config');
  $request = $this->requestStack
    ->getCurrentRequest();
  if ($this
    ->checkLogin($event, $config, $request)) {

    // Show access denied warning message.
    if ($message = $config
      ->get('deny_message')) {
      $messenger = $this->messenger;
      $messenger
        ->addMessage($message, $messenger::TYPE_WARNING);
    }

    // Prepare login and destination paths.
    if ($auth_path = $config
      ->get('auth_path')) {
      $redirect_path = "internal:{$auth_path}";
    }
    else {
      $redirect_path = 'internal:/user/login';
    }
    if (empty($destination = $config
      ->get('destination_path'))) {
      $destination = $request
        ->getRequestUri();
    }
    return Url::fromUri($redirect_path, [
      'query' => [
        'destination' => $destination,
      ],
    ])
      ->toString();
  }
  return NULL;
}