You are here

public function SwitchController::switchTo in Masquerade 8.2

Masquerades the current user as a given user.

Access to masquerade as the target user account has to checked by all callers via masquerade_target_user_access() already.

Parameters

\Drupal\user\UserInterface $user: The user account object to masquerade as.

\Symfony\Component\HttpFoundation\Request $request: The request object.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse Redirect to previous page.

See also

this::getRedirectResponse()

1 string reference to 'SwitchController::switchTo'
masquerade.routing.yml in ./masquerade.routing.yml
masquerade.routing.yml

File

src/Controller/SwitchController.php, line 77

Class

SwitchController
Controller for switch and back to masquerade as user.

Namespace

Drupal\masquerade\Controller

Code

public function switchTo(UserInterface $user, Request $request) {

  // Store current user for messages.
  $account = $this->currentUser;
  $error = masquerade_switch_user_validate($user);
  if (empty($error)) {
    if ($this->masquerade
      ->switchTo($user)) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('You are now masquerading as @user.', [
        '@user' => $account
          ->getDisplayName(),
      ]));
    }
  }
  else {
    $this
      ->messenger()
      ->addError($error);
  }
  return $this
    ->getRedirectResponse($request);
}