Update LocaleSubscriber.php

pull/3768/head
Julio Montoya 4 years ago
parent 50dcc20fc7
commit 4359c2159a
  1. 31
      src/CoreBundle/EventSubscriber/LocaleSubscriber.php
  2. 22
      src/CoreBundle/EventSubscriber/UserLocaleSubscriber.php
  3. 10
      src/CoreBundle/Resources/config/services.yml

@ -2,7 +2,7 @@
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\EventListener; namespace Chamilo\CoreBundle\EventSubscriber;
use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Manager\SettingsManager; use Chamilo\CoreBundle\Manager\SettingsManager;
@ -12,26 +12,20 @@ use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
/** /**
* Class LocaleListener
* Checks the portal listener depending of different settings: * Checks the portal listener depending of different settings:
* platform, user, course. * platform, user, course.
*/ */
class LocaleListener implements EventSubscriberInterface class LocaleSubscriber implements EventSubscriberInterface
{ {
protected $settingsManager;
protected $defaultLocale; protected $defaultLocale;
protected $paremeterBag; protected $parameterBag;
protected $settingsManager;
/**
* LocaleListener constructor. public function __construct(string $defaultLocale, SettingsManager $settingsManager, ParameterBagInterface $parameterBag)
*
* @param string $defaultLocale
*/
public function __construct($defaultLocale, SettingsManager $settingsManager, ParameterBagInterface $paremeterBag)
{ {
$this->defaultLocale = $defaultLocale; $this->defaultLocale = $defaultLocale;
$this->settingsManager = $settingsManager; $this->settingsManager = $settingsManager;
$this->paremeterBag = $paremeterBag; $this->parameterBag = $parameterBag;
} }
public function onKernelRequest(RequestEvent $event) public function onKernelRequest(RequestEvent $event)
@ -40,12 +34,16 @@ class LocaleListener implements EventSubscriberInterface
if (!$request->hasPreviousSession()) { if (!$request->hasPreviousSession()) {
return; return;
} }
$installed = 1 === (int) $this->paremeterBag->get('installed'); $installed = 1 === (int) $this->parameterBag->get('installed');
if (!$installed) { if (!$installed) {
return; return;
} }
if (false === $request->hasSession()) {
return;
}
$loadFromDb = $request->getSession()->get('check_locale_from_db', true); $loadFromDb = $request->getSession()->get('check_locale_from_db', true);
if (false === $loadFromDb && if (false === $loadFromDb &&
@ -142,14 +140,11 @@ class LocaleListener implements EventSubscriberInterface
} }
} }
/**
* @return array
*/
public static function getSubscribedEvents() public static function getSubscribedEvents()
{ {
return [ return [
// must be registered before the default Locale listener // must be registered before the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 15]], KernelEvents::REQUEST => [['onKernelRequest', 20]],
]; ];
} }
} }

@ -2,28 +2,24 @@
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\EventListener; namespace Chamilo\CoreBundle\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
/** /**
* Class UserLocaleListener.
*
* Stores the locale of the user in the session after the * Stores the locale of the user in the session after the
* login. This can be used by the LocaleListener afterwards. * login. This can be used by the LocaleSubscriber afterwards.
* *
* Priority order: platform -> user * Priority order: platform -> user
* Priority order: platform -> user -> course * Priority order: platform -> user -> course
*/ */
class UserLocaleListener class UserLocaleSubscriber implements EventSubscriberInterface
{ {
/** @var SessionInterface */
private $session; private $session;
/**
* UserLocaleListener constructor.
*/
public function __construct(SessionInterface $session) public function __construct(SessionInterface $session)
{ {
$this->session = $session; $this->session = $session;
@ -35,9 +31,17 @@ class UserLocaleListener
public function onInteractiveLogin(InteractiveLoginEvent $event) public function onInteractiveLogin(InteractiveLoginEvent $event)
{ {
$user = $event->getAuthenticationToken()->getUser(); $user = $event->getAuthenticationToken()->getUser();
if (null !== $user->getLocale()) { if (null !== $user->getLocale()) {
$this->session->set('_locale', $user->getLocale()); $this->session->set('_locale', $user->getLocale());
$this->session->set('_locale_user', $user->getLocale()); $this->session->set('_locale_user', $user->getLocale());
} }
} }
public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
];
}
} }

@ -152,15 +152,13 @@ services:
- {name: kernel.event_listener, event: kernel.controller, method: onKernelController} - {name: kernel.event_listener, event: kernel.controller, method: onKernelController}
# Locale listener # Locale listener
Chamilo\CoreBundle\EventListener\LocaleListener: Chamilo\CoreBundle\EventSubscriber\LocaleSubscriber:
arguments: ['%kernel.default_locale%', '@chamilo.settings.manager', '@parameter_bag'] arguments: ['%kernel.default_locale%', '@chamilo.settings.manager', '@parameter_bag']
tags:
- {name: kernel.event_subscriber, priority: 15}
# User locale listener # User locale listener
Chamilo\CoreBundle\EventListener\UserLocaleListener: # Chamilo\CoreBundle\EventListener\UserLocaleListener:
tags: # tags:
- {name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin, priority: 15} # - {name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin, priority: 15}
# Settings listener # Settings listener
Chamilo\CoreBundle\EventListener\SettingListener: Chamilo\CoreBundle\EventListener\SettingListener:

Loading…
Cancel
Save