3

I searched everywhere but it seems impossible thing to do.

I was able to catch SwitchEvent coming from SwitchEventListener and get both impersonator and target user objects (one being impersonated as) but I would really like, based on target user roles to redirect request to some other URL.

Example:

I have two restricted areas: "/basic" and "/elevated" where first one requires ROLE_BASIC and the other one ROLE_ELEVATED role. Now, if I am currently logged in as ROLE_ELEVATED user and want to switch to some ROLE_BASIC user using URL:

/basic/?_switch_user=some_basic_user

that would result in AccessDeniedException, and I'm forced to navigate first to:

/?_switch_user=some_basic_user. 

Only after that I can navigate to /basic as token has been written to session.

Now, the question, as subject suggests: Is there any way to redirect user using SwitchEvent (or any other) after doing user switching?

hakre
  • 193,403
  • 52
  • 435
  • 836
Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
  • Did you ever solve this problem? I'm having the same issue. It seems that `AccessListener` is called before `SwitchUserListener` so you're locked out before the `SwitchUser` can be invoked? – Squazic Aug 30 '12 at 00:16
  • Actually, I did :) Currently, I don't have access to my dev computer but as I do (tomorrow or day after that) I going to write here how I did it. BTW, thanks for reminding me about posted question. I totally forgot I asked it so I definitely need to "close" it with an appropriate answer... – Jovan Perovic Aug 31 '12 at 19:31
  • I'm very interested in your solution too – David Barreto Sep 03 '12 at 14:33

2 Answers2

2

Ok, I finally got access to my dev computer and looked up the the solution to this problem.

And, I got to say, it is not as clean as I wanted it to be but delivery date was critical and this was the only way to achieve it back then.

So, what I basically did was to send AJAX (although, I set async to false) request to URL /?_switch_user=some_basic_user, wait for an answer and if it was successful I would navigate forward to /basic/?_switch_user=some_basic_user URL. I know, it's quick and dirty, and is pretty much unreliable...

Hope this helps....

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
0

You can just generate the right link to begin with:

<a href="/basic/?_switch_user=some_basic_user">...</a>

Of course, the URL can be generated by twig's path helper too:

<a href="{{ path('basic_homepage', { _switch_user: 'some_basic_user' }) }}">...</a>
Geoffrey Bachelet
  • 4,047
  • 2
  • 21
  • 17