SecureLoginManager.php in Secure Login 8
File
src/SecureLoginManager.php
View source
<?php
namespace Drupal\securelogin;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Url;
use Drupal\user\Plugin\Block\UserLoginBlock;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
class SecureLoginManager implements TrustedCallbackInterface {
protected $config;
protected $eventDispatcher;
protected $request;
protected $requestStack;
public function __construct(ConfigFactoryInterface $config_factory, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack) {
$this->config = $config_factory
->get('securelogin.settings');
$this->eventDispatcher = $event_dispatcher;
$this->requestStack = $request_stack;
$this->request = $this->requestStack
->getCurrentRequest();
}
public function secureForm(&$form) {
$form['#cache']['contexts'][] = 'securelogin';
$form['#https'] = TRUE;
if ($this->request
->isSecure()) {
return;
}
if ($this->config
->get('secure_forms')) {
$form['#cache']['max-age'] = 0;
$this
->secureRedirect();
}
$form['#action'] = $this
->secureUrl($form['#action']);
}
public function secureRedirect() {
if ($this->request
->isSecure()) {
return;
}
$status = $this
->getRedirectStatus();
$request = $this->requestStack
->getMasterRequest();
$url = Url::fromUri("internal:{$request->getPathInfo()}");
$url
->setOption('absolute', TRUE)
->setOption('external', FALSE)
->setOption('https', TRUE)
->setOption('query', $request->query
->all());
$listener = function ($event) use ($url, $status) {
$response = new TrustedRedirectResponse($url
->toString(), $status);
$response
->setExpires(\DateTime::createFromFormat('j-M-Y H:i:s T', '19-Nov-1978 05:00:00 UTC'));
$response
->addCacheableDependency(new SecureLoginCacheableDependency());
$event
->setResponse($response);
$event
->getRequest()->query
->set('destination', '');
};
$this->eventDispatcher
->addListener(KernelEvents::RESPONSE, $listener, 222);
}
public function secureUrl($url) {
global $base_path, $base_secure_url;
if (strpos($url, $base_path) === 0) {
$base_url = $this->config
->get('base_url') ?: $base_secure_url;
return substr_replace($url, $base_url, 0, strlen($base_path) - 1);
}
return str_replace('http://', 'https://', $url);
}
public function renderPlaceholderFormAction() {
$action = UserLoginBlock::renderPlaceholderFormAction();
$action['#markup'] = $this
->secureUrl($action['#markup']);
return $action;
}
public function getRedirectStatus() {
return $this->request
->isMethodCacheable() ? RedirectResponse::HTTP_MOVED_PERMANENTLY : RedirectResponse::HTTP_PERMANENTLY_REDIRECT;
}
public static function trustedCallbacks() {
return [
'renderPlaceholderFormAction',
'userLoginBlockPreRender',
];
}
public function userLoginBlockPreRender($build) {
if (!empty($build['content']['user_login_form']['#https'])) {
$this
->secureForm($build['content']['user_login_form']);
$placeholder = 'form_action_p_4r8ITd22yaUvXM6SzwrSe9rnQWe48hz9k1Sxto3pBvE';
if (isset($build['content']['user_login_form']['#attached']['placeholders'][$placeholder])) {
$build['content']['user_login_form']['#attached']['placeholders'][$placeholder] = [
'#lazy_builder' => [
'securelogin.manager:renderPlaceholderFormAction',
[],
],
];
}
}
return $build;
}
}