Skip to content

Commit

Permalink
Document security.switch_user event
Browse files Browse the repository at this point in the history
... in the cookbook article about How to Impersonate a User.

Added code sample about how to change the locale in case of a sticky locale:
http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
  • Loading branch information
rvanlaak committed Jun 16, 2015
1 parent 4fc429e commit 11ec6e4
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cookbook/security/impersonating_user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,49 @@ setting:
),
),
));
Events
------

The firewall dispatches the ``security.switch_user`` event right after the impersonation
completed. The ``SwitchUserEvent`` is passed to the listener, based on which you are able
to get the target user you impersonate.

The cookbook article about
:doc:`Making the Locale "Sticky" during a User's Session </cookbook/session/locale_sticky_session>`
does not take any changing locale into account. The following code sample will show how to change the sticky locale:

.. configuration-block::

.. code-block:: yaml
# app/config/services.yml
services:
app.switch_user_listener:
class: AppBundle\EventListener\SwitchUserListener
tags:
- { name: kernel.event_listener, event: security.switch_user, method: onSwitchUser }
.. caution::

The listener implementation assumes your ``User`` entity has ``getLocale()``.

.. code-block:: php
// src/AppBundle/EventListener/SwitchUserListener.pnp
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
class SwitchUserListener
{
/**
* @param SwitchUserEvent $event
*/
public function onSwitchUser(SwitchUserEvent $event)
{
$event->getRequest()->getSession()->set(
'_locale',
$event->getTargetUser()->getLocale()
);
}
}

0 comments on commit 11ec6e4

Please sign in to comment.