if (false ===
$this->accessDecisionManager->
decide($token,
[$this->role
],
$user)) { $exception =
new AccessDeniedException();
$exception->
setAttributes($this->role
);
throw $exception;
} $this->logger?->
info('Attempting to switch to user.',
['username' =>
$username]);
$this->userChecker->
checkPostAuth($user);
$roles =
$user->
getRoles();
$roles[] = 'ROLE_PREVIOUS_ADMIN';
$originatedFromUri =
str_replace('/&', '/?',
preg_replace('#[&?]'.
$this->usernameParameter.'=[^&]*#', '',
$request->
getRequestUri()));
$token =
new SwitchUserToken($user,
$this->firewallName,
$roles,
$token,
$originatedFromUri);
if (null !==
$this->dispatcher
) { $switchEvent =
new SwitchUserEvent($request,
$token->
getUser(),
$token);
$this->dispatcher->
dispatch($switchEvent, SecurityEvents::SWITCH_USER
);
// use the token from the event in case any listeners have replaced it.
$token =
$switchEvent->
getToken();
}