You are here

public function AnonymousRedirectSubscriber::redirectAnonymous in Anonymous Redirect 8.2

Redirects anonymous users to the /user route.

Parameters

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

File

src/EventSubscriber/AnonymousRedirectSubscriber.php, line 72

Class

AnonymousRedirectSubscriber
Subscribes to kernel events and does the actual redirect.

Namespace

Drupal\anonymous_redirect\EventSubscriber

Code

public function redirectAnonymous(GetResponseEvent $event) {

  // Initialize the minimum amount of values since this runs on every request.
  $config = $this
    ->config('anonymous_redirect.settings');
  $redirectEnabled = $config
    ->get('enable_redirect');

  // Fail as early as possible.
  if (!$redirectEnabled || $this->account
    ->isAuthenticated() || $this
    ->state()
    ->get('system.maintenance_mode')) {
    return;
  }

  // Now we know we need to redirect -- build all other needed variables.
  $redirectUrl = $config
    ->get('redirect_url');
  $redirectUrlOverridesText = $config
    ->get('redirect_url_overrides');
  $redirectUrlOverrides = $redirectUrlOverridesText ? explode("\r\n", $redirectUrlOverridesText) : [];
  $currentPath = $event
    ->getRequest()
    ->getPathInfo();

  // Handle language prefix if present.
  $currentLanguagePrefix = $this
    ->languageManager()
    ->getCurrentLanguage()
    ->getId();

  // Check if the language prefix is present after the leading slash.
  if (substr($currentPath, 1, strlen($currentLanguagePrefix)) == $currentLanguagePrefix) {
    $currentPath = substr($currentPath, strlen($currentLanguagePrefix) + 1);
  }

  // Do nothing if the url is in the list of overrides.
  if (in_array($currentPath, $redirectUrlOverrides) || $this->pathMatcher
    ->matchPath($currentPath, $redirectUrlOverridesText)) {
    return;
  }

  // External URL must use TrustedRedirectResponse class.
  if (UrlHelper::isExternal($redirectUrl)) {
    $event
      ->setResponse(new TrustedRedirectResponse($redirectUrl));
    return;
  }

  // Redirect the user to the front page.
  if ($this
    ->isFrontPage($redirectUrl) && $currentPath !== Url::fromRoute("<front>")
    ->toString()) {
    $event
      ->setResponse(new RedirectResponse(Url::fromRoute("<front>")
      ->toString()));
  }

  // Redirect the user the configured route.
  if ($this
    ->isFrontPage($redirectUrl) == FALSE && strpos($currentPath, $redirectUrl) === FALSE) {

    // If redirecting to the login page, redirect the visitor back to the
    // requested path after a successful login.
    if ($redirectUrl === Url::fromRoute('user.login')
      ->toString() && $currentPath !== '/') {
      $options = [
        'query' => [
          'destination' => $currentPath,
        ],
      ];
      $redirectUrl = Url::fromUserInput($redirectUrl, $options)
        ->toString();
    }
    $event
      ->setResponse(new RedirectResponse(Url::fromUri('internal:' . $redirectUrl)
      ->toString()));
  }
}