Symfony\Component\HttpKernel\Security\Firewall\SwitchUserListener::attemptSwitchUser PHP Method

attemptSwitchUser() protected method

Attempts to switch to another user.
protected attemptSwitchUser ( Request $request ) : Symfony\Component\Security\Authentication\Token\TokenInterface | null
$request Symfony\Component\HttpFoundation\Request A Request instance
return Symfony\Component\Security\Authentication\Token\TokenInterface | null The new TokenInterface if successfully switched, null otherwise
    protected function attemptSwitchUser(Request $request)
    {
        $token = $this->securityContext->getToken();
        if (false !== $this->getOriginalToken($token)) {
            throw new \LogicException(sprintf('You are already switched to "%s" user.', (string) $token));
        }

        $this->accessDecisionManager->decide($token, null, array($this->role));

        $username = $request->get($this->usernameParameter);

        if (null !== $this->logger) {
            $this->logger->debug(sprintf('Attempt to switch to user "%s"', $username));
        }

        $user = $this->provider->loadUserByUsername($username);
        $this->accountChecker->checkPostAuth($user);

        $roles = $user->getRoles();
        $roles[] = new SwitchUserRole('ROLE_PREVIOUS_ADMIN', $this->securityContext->getToken());

        $token = new UsernamePasswordToken($user, $user->getPassword(), $roles);
        $token->setImmutable(true);

        return $token;
    }