Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Impersonate a user when I'm already impersonating another user?

Tags:

php

symfony

I want to be able to impersonate a user in my app, so I followed the tutorial from the website

If I try to change my user from user "A" to user "B", it works, but if I try to change to user "C" while I'm already impersonating user "B", the attemptSwitchUser method throws this excepcion:

You are already switched to "B" user.
500 Internal Server Error - LogicException

This is because this method checks wether the current user name (B) equals the user name we want to impersonate (C)

$token = $this->securityContext->getToken();
$originalToken = $this->getOriginalToken($token);

if (false !== $originalToken) {
    if ($token->getUsername() === $request->get($this->usernameParameter)) {
        return $token;
    } else {
        throw new \LogicException(sprintf('You are already switched to "%s" user.', $token->getUsername()));
    }
}

But obviously this can't never be the case, so I don't know if this is a bug in Symfony's code or if there's something else I'm missing.

Is this really a bug? Can I achieve this functionality without commenting out this block of code?

like image 844
zootropo Avatar asked Dec 05 '25 04:12

zootropo


1 Answers

Switched user cannot switch to other. Before you can do that you need to switch back to the original user ( ?_switch_user=_exit )

like image 80
Federkun Avatar answered Dec 07 '25 20:12

Federkun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!