Have you ever had a bug you couldn't reproduce? Nope, me either. Of course we have! The worst is when a user reports a bug in their account... which would be really easy to verify and debug... if only we could log and see what they're seeing.

Look, we've got too much work to do to debug this the hard way. We need to be able switch to that user's account: we need to impersonate them.

Activating switch_user

Setting up impersonation is super easy. In security.yml, under your firewall, add a new key called switch_user set to ~ to activate the system:

... lines 1 - 2
... lines 4 - 14
... lines 16 - 20
... lines 22 - 28
switch_user: ~
... lines 30 - 40

Back to the User Provider

Now, on any URL, you can add ?_switch_user= and then the username of whoever you want to log in as. In our case, we want to login as [email protected] Why is that the email address in our case? This actually goes back to your user provider.

We're using the built-in entity user provider, and we told it that we want to identify by the email. Before now, this setting was meaningless. If we changed that to some other field, then we would actually identify ourselves by that up in the URL.

Not Everyone Can Impersonate!

Hit enter to try switching users. OMG - access denied!

That makes sense - we can't just let anybody do this impersonation trick. Internally, this feature checks for a very specific role called ROLE_ALLOWED_TO_SWITCH. But, we don't have that.

Hey, no problem! Let's give this role to ROLE_ADMIN under role_hierarchy:

... lines 1 - 2
... lines 4 - 6
... lines 9 - 40

Cool! Try it out. It works! I mean, it doesn't work! Hmm, but it's not that security error anymore - it just can't find the user: weaverryan [email protected] You know what? This is the because of the + sign in the email addresses - which represents a space in URLs. Change that to the url-encoded plus sign: %2B.

Boom! Now we're surfing as [email protected]. Pretty awesome. Once you're done, go back by adding ?_switch_user=_exit to any URL. That's it! We're back as our original user.

Switching users... yea, it's my favorite.

Leave a comment!

  • 2018-06-06 Diego Aguiar

    Hey ali nizar

    Have you consider using the voter's system?
    or you can check our video about it:

  • 2018-06-06 ali nizar

    Hello Victor
    I associated acl with masks (MASK EDIT, MASK VIEW, ....) to an entity according to a role for example: ROLE_SUPER_ADMIN.
    I would like to retrieve them to modify them using a form which contains permissions ie masks like (MASK_VIEW, ..)
    Thanks for your help

  • 2018-03-16 Victor Bocharsky

    Hey Piter,

    Well, do not give an ADMIN the ROLE_ALLOWED_TO_SWITCH role? probably the simplest one :) But yeah, good question. I have never tried to do this, but looks like it's possible to do by overriding the default SwitchUserListener, see this answer in StackOverflow: . With this way you have a full control with any crazy custom logic.


  • 2018-03-15 Piter Huaman

    But what happens when, for instance, you have ROLE_ADMIN and ROLE_SUPER_ADMIN both with the ROLE_ALLOWED_TO_SWITCH. How can you prevent an ADMIN impersonating a SUPER ADMIN?

  • 2017-02-22 Victor Bocharsky

    Hey Daniel,

    Yes, the ROLE_PREVIOUS_ADMIN is the Symfony way to do it! What about shortcut - I don't know any shorter than that. But probably you can create special route which always will redirect back based on HTTP referer: $request->headers->get('referer').


  • 2017-02-21 Daniel Hauck

    Hey Ryan,

    awesome Tutorial!

    For example in Jira I also have the option to log in as different users, There I have the "switch back" option.

    I tried to implement the same here and found, that I got one extra role available, if I switch the user, which is the 'ROLE_PREVIOUS_ADMIN' role.

    Is it the right way, to determine, if I impersonated someone?

    Here is the code I used, to check in the twig template:

    {% if is_granted('ROLE_PREVIOUS_ADMIN') %}
    <li><a class="btn btn-danger" href="{{

    {% endif %}

    And maybe, there is a shortcut, to get the current path out of twig ;)

    thanks for the awesome tutorial!