Commit 2a321c4e authored by Emma's avatar Emma 🦉

retain locale when session expires, fixes #16

parent 13c5c8fa
Pipeline #21354594 passed with stage
in 4 minutes and 48 seconds
......@@ -56,9 +56,8 @@ services:
App\EventListener\LocaleListener:
tags:
- { name: kernel.event_listener, event: kernel.request, priority: 15 }
- { name: kernel.event_listener, event: kernel.request, priority: 20 }
- { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }
- { name: doctrine.event_listener, event: postPersist }
- { name: doctrine.event_listener, event: postUpdate }
App\EventListener\SubmissionImageListener:
......
......@@ -8,9 +8,10 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Translation\TranslatorInterface;
/**
* @see http://symfony.com/doc/current/session/locale_sticky_session.html
* @see https://symfony.com/doc/current/session/locale_sticky_session.html
*/
final class LocaleListener {
/**
......@@ -23,12 +24,19 @@ final class LocaleListener {
*/
private $tokenStorage;
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(
SessionInterface $session,
TokenStorageInterface $tokenStorage
TokenStorageInterface $tokenStorage,
TranslatorInterface $translator
) {
$this->session = $session;
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
}
public function onKernelRequest(GetResponseEvent $event) {
......@@ -38,8 +46,10 @@ final class LocaleListener {
return;
}
if ($request->getSession()->has('_locale')) {
$request->setLocale($request->getSession()->get('_locale'));
$locale = $request->getSession()->get('_locale');
if ($locale) {
$request->setLocale($locale);
}
}
......@@ -47,37 +57,28 @@ final class LocaleListener {
$user = $event->getAuthenticationToken()->getUser();
if ($user instanceof User) {
$this->configureLocale($user);
}
}
$locale = $user->getLocale();
public function postPersist(LifecycleEventArgs $args) {
$user = $args->getEntity();
$this->session->set('_locale', $locale);
if ($user instanceof User) {
$this->configureLocale($user);
$event->getRequest()->setLocale($locale);
// Because security.interactive_login runs after kernel.request,
// where the translator gets its locale, we must manually set the
// locale on the translator. There is no way around this.
$this->translator->setLocale($locale);
}
}
public function postUpdate(LifecycleEventArgs $args) {
$this->postPersist($args);
}
$user = $args->getEntity();
private function configureLocale(User $user) {
if (
!$this->session->isStarted() ||
!$this->tokenStorage->getToken() ||
$this->tokenStorage->getToken()->getUser() !== $user
) {
// The session is not started, or the logged in user is not the
// account being modified.
return;
}
if ($user instanceof User) {
$token = $this->tokenStorage->getToken();
if ($user->getLocale() !== null) {
$this->session->set('_locale', $user->getLocale());
} else {
$this->session->remove('_locale');
if ($token && $token->getUser() === $user) {
$this->session->set('_locale', $user->getLocale());
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment