Skip to content

Impersonation events #19446

@DavidPetrasek

Description

@DavidPetrasek

Here it says:
"The firewall dispatches the security.switch_user event right after the impersonation is completed."

In the file vendor/symfony/security-http/SecurityEvents.php you can find this:

"The SWITCH_USER event occurs before switch to another user..."

If you look at the code below it seems to me like the user is not switched, because in the service it returns PostAuthenticationToken and I expected it to return SwitchUserToken.

public function onSwitchUser(SwitchUserEvent $event): void
    {
        $token = $event->getToken();   dump($token); // This returns SwitchUserToken
        ...
        .... later ....
        $this->someService->someMethod();
    }

The called method:

public function someMethod()
    {
        $token = $this->security->getToken();   dump($token);   // This returns PostAuthenticationToken
        if ($token instanceof SwitchUserToken) 
        {
            $impersonatorUser = $token->getOriginalToken()->getUser();
             ......
        }
    }

And, then when I visit another page which also uses someMethod, it returns SwitchUserToken as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SecurityhasPRA Pull Request has already been submitted for this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions