Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve user's chosen locale in Editor #2037

Merged
merged 2 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Controller/Backend/UserEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ public function save(?User $user, ValidatorInterface $validator): Response
$this->em->persist($user);
$this->em->flush();

$event = new UserEvent($user);
$this->dispatcher->dispatch($event, UserEvent::ON_POST_SAVE);

$this->addFlash('success', 'user.updated_profile');

return $this->redirectToRoute('bolt_users');
Expand Down
4 changes: 4 additions & 0 deletions src/Event/Subscriber/LocaleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class LocaleSubscriber implements EventSubscriberInterface
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();

if (! $request->hasPreviousSession()) {
return;
}
Expand All @@ -25,7 +26,10 @@ public function onKernelRequest(RequestEvent $event): void
$locale = $request->query->get('_locale');
$request->getSession()->set('_locale', $locale);
$request->setLocale($locale);
} elseif ($request->attributes->get('zone', false) === 'backend' && $request->getSession()->has('_backend_locale')) {
$request->setLocale($request->getSession()->get('_backend_locale'));
} elseif ($request->getSession()->has('_locale')) {
// @todo: This is probably never reached. Remove if you're brave enough ;-)
// if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale'));
}
Expand Down
20 changes: 16 additions & 4 deletions src/Event/Subscriber/UserLocaleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Bolt\Event\Subscriber;

use Bolt\Entity\User;
use Bolt\Event\UserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
Expand All @@ -18,19 +19,30 @@ class UserLocaleSubscriber implements EventSubscriberInterface
{
private $session;

public function __construct(SessionInterface $session)
/** @var string */
private $defaultLocale;

public function __construct(SessionInterface $session, string $defaultLocale)
{
$this->session = $session;
$this->defaultLocale = $defaultLocale;
}

public function onInteractiveLogin(InteractiveLoginEvent $event): void
{
/** @var User $user */
$user = $event->getAuthenticationToken()->getUser();
$this->updateBackendLocale($user);
}

public function onUserEdit(UserEvent $event): void
{
$this->updateBackendLocale($event->getUser());
}

if ($user->getLocale() !== null) {
$this->session->set('_locale', $user->getLocale());
}
private function updateBackendLocale(User $user): void
{
$this->session->set('_backend_locale', $user->getLocale() ?? $this->defaultLocale);
}

public static function getSubscribedEvents()
Expand Down
9 changes: 9 additions & 0 deletions src/Event/UserEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class UserEvent
{
public const ON_EDIT = 'bolt.users_pre_edit';
public const ON_POST_SAVE = 'bolt.users_post_save';

/** @var User */
private $user;
Expand All @@ -28,11 +29,19 @@ public function __construct(User $user, ?Collection $roleOptions = null)
}
}

/**
* @deprecated
*/
public function getContent(): User
{
return $this->user;
}

public function getUser(): User
{
return $this->user;
}

public function getRoleOptions(): Collection
{
return $this->rolesOptions;
Expand Down