You are here

public function SecureLoginManager::secureRedirect in Secure Login 8

Redirects a request to the same path on the secure base URL.

1 call to SecureLoginManager::secureRedirect()
SecureLoginManager::secureForm in src/SecureLoginManager.php
Rewrites the form action to use the secure base URL.

File

src/SecureLoginManager.php, line 81

Class

SecureLoginManager
Defines the secure login service.

Namespace

Drupal\securelogin

Code

public function secureRedirect() {

  // Do not redirect from HTTPS requests.
  if ($this->request
    ->isSecure()) {
    return;
  }
  $status = $this
    ->getRedirectStatus();

  // Build the redirect URL from the master request.
  $request = $this->requestStack
    ->getMasterRequest();

  // Request may be a 404 so handle as unrouted URI.
  $url = Url::fromUri("internal:{$request->getPathInfo()}");
  $url
    ->setOption('absolute', TRUE)
    ->setOption('external', FALSE)
    ->setOption('https', TRUE)
    ->setOption('query', $request->query
    ->all());

  // Create listener to set the redirect response.
  $listener = function ($event) use ($url, $status) {
    $response = new TrustedRedirectResponse($url
      ->toString(), $status);

    // Page cache has a fatal error if cached response has no Expires header.
    $response
      ->setExpires(\DateTime::createFromFormat('j-M-Y H:i:s T', '19-Nov-1978 05:00:00 UTC'));

    // Add cache context for this redirect.
    $response
      ->addCacheableDependency(new SecureLoginCacheableDependency());
    $event
      ->setResponse($response);

    // Redirect URL has destination so consider this the final destination.
    $event
      ->getRequest()->query
      ->set('destination', '');
  };

  // Add listener to response event at high priority.
  $this->eventDispatcher
    ->addListener(KernelEvents::RESPONSE, $listener, 222);
}