Exemplo n.º 1
0
 /**
  * Attempts to switch to another user.
  *
  * @param Request $request A Request instance
  *
  * @return 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;
 }