Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow switching/impersonating only to particular users in Symfony2

EXPLANATION

Allowing user to switch to any other user is easy in Symfony2. My question is, how to allow certain user to switch only to certain other users? I'm using FOSUserBundle as my users provider.

EXAMPLE

There is 5 users, UserA, UserB, UserC, UserD, UserE, but only 3 of them are linked together.

UserA can switch to UserB and UserC

UserB can switch to UserA and UserC

UserC can switch to UserA and UserB

Thanks for your help!

like image 1000
Žan Kusterle Avatar asked Nov 14 '12 00:11

Žan Kusterle


People also ask

What is selected to impersonate another user?

To impersonate another user, the impersonator selects the Impersonate icon on the far right of the Tab Bar and selects the user from the Impersonate drop-down list. To stop impersonating a user, the impersonator clicks the Impersonate icon and selects Stop Impersonate from the Impersonate drop-down list.

How do you impersonate a user?

Impersonate a user Users or Contacts. Click the name of an individual user to view the detail page. Click the Impersonate link next to the user's name.


2 Answers

You could possibly implement this type of thing by overriding the default SwitchUserListener.

The parameter you would override is: security.authentication.switchuser_listener.class

For example, in parameters.yml:

parameters:
  security.authentication.switchuser_listener.class: My\Project\Security\Listener\SwitchUserListener

Where, in your custom listener class, you'd implement Symfony\Component\Security\Http\Firewall\ListenerInterface and use the existing Symfony\Component\Security\Http\Firewall\SwitchUserListener class as a basis for your custom implementation.

like image 154
Tyler Sommer Avatar answered Oct 13 '22 23:10

Tyler Sommer


If you want to impersonate Admin Users in to Regular user there are some examples :)

https://gist.github.com/1589120

http://symfony.com/doc/current/book/security.html#impersonating-a-user

like image 25
Max Małecki Avatar answered Oct 13 '22 23:10

Max Małecki