TokenHandler.php in Persistent Login 8
File
src/EventSubscriber/TokenHandler.php
View source
<?php
namespace Drupal\persistent_login\EventSubscriber;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\SessionConfigurationInterface;
use Drupal\persistent_login\CookieHelperInterface;
use Drupal\persistent_login\PersistentToken;
use Drupal\persistent_login\TokenException;
use Drupal\persistent_login\TokenManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class TokenHandler implements EventSubscriberInterface {
protected $tokenManager;
protected $cookieHelper;
protected $sessionConfiguration;
protected $entityTypeManager;
protected $token;
public function __construct(TokenManager $token_manager, CookieHelperInterface $cookie_helper, SessionConfigurationInterface $session_configuration, EntityTypeManagerInterface $entity_type_manager) {
$this->tokenManager = $token_manager;
$this->cookieHelper = $cookie_helper;
$this->sessionConfiguration = $session_configuration;
$this->entityTypeManager = $entity_type_manager;
}
public static function getSubscribedEvents() {
$events = [];
$events[KernelEvents::REQUEST][] = [
'loadTokenOnRequestEvent',
310,
];
$events[KernelEvents::RESPONSE][] = [
'setTokenOnResponseEvent',
];
return $events;
}
public function loadTokenOnRequestEvent(GetResponseEvent $event) {
if (!$event
->isMasterRequest()) {
return;
}
$request = $event
->getRequest();
if ($this->cookieHelper
->hasCookie($request)) {
$this->token = $this
->getTokenFromCookie($request);
if (!$this->sessionConfiguration
->hasSession($request)) {
$this->token = $this->tokenManager
->validateToken($this->token);
if ($this->token
->getStatus() === PersistentToken::STATUS_VALID) {
try {
$user = $this->entityTypeManager
->getStorage('user')
->load($this->token
->getUid());
user_login_finalize($user);
} catch (PluginException $e) {
}
}
}
}
}
public function setTokenOnResponseEvent(FilterResponseEvent $event) {
if (!$event
->isMasterRequest()) {
return;
}
if ($this->token) {
$request = $event
->getRequest();
$response = $event
->getResponse();
$sessionOptions = $this->sessionConfiguration
->getOptions($request);
if ($this->token
->getStatus() === PersistentToken::STATUS_VALID) {
$this->token = $this->tokenManager
->updateToken($this->token);
$response->headers
->setCookie(new Cookie($this->cookieHelper
->getCookieName($request), $this->token, $this->token
->getExpiry(), '/', $sessionOptions['cookie_domain'], $sessionOptions['cookie_secure']));
$response
->setPrivate();
}
elseif ($this->token
->getStatus() === PersistentToken::STATUS_INVALID) {
$this->tokenManager
->deleteToken($this->token);
$response->headers
->clearCookie($this->cookieHelper
->getCookieName($request), '/', $sessionOptions['cookie_domain'], $sessionOptions['cookie_secure']);
$response
->setPrivate();
}
else {
}
}
}
public function getTokenFromCookie(Request $request) {
return PersistentToken::createFromString($this->cookieHelper
->getCookieValue($request));
}
public function setNewSessionToken($uid) {
try {
$this->token = $this->tokenManager
->createNewTokenForUser($uid);
} catch (TokenException $e) {
}
}
public function clearSessionToken() {
if ($this->token) {
$this->token = $this->token
->setInvalid();
}
}
}