You are here

public function Masquerade::switchTo in Masquerade 8.2

Masquerades the current user as a given user.

Parameters

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

Return value

bool TRUE when masqueraded, FALSE otherwise.

File

src/Masquerade.php, line 149

Class

Masquerade
Defines a masquerade service to switch user account.

Namespace

Drupal\masquerade

Code

public function switchTo(UserInterface $target_account) {

  // Save previous account ID to session storage, set this before
  // switching so that other modules can react to it, e.g. during
  // hook_user_logout().
  $this->session
    ->set('masquerading', $this->currentUser
    ->id());
  $account = $this
    ->switchUser($target_account);
  $this->logger
    ->info('User %username masqueraded as %target_username.', [
    '%username' => $account
      ->getDisplayName(),
    '%target_username' => $target_account
      ->getDisplayName(),
    'link' => $target_account
      ->toLink($this
      ->t('view'))
      ->toString(),
  ]);
  return TRUE;
}