You are here

public function Masquerade::switchBack in Masquerade 8.2

Switching back to previous user.

Return value

bool TRUE when switched back, FALSE otherwise.

File

src/Masquerade.php, line 172

Class

Masquerade
Defines a masquerade service to switch user account.

Namespace

Drupal\masquerade

Code

public function switchBack() {
  if (!$this->session
    ->isStarted() && !$this->session
    ->has('masquerading')) {
    return FALSE;
  }

  // Load previous user account.
  $user = $this->userStorage
    ->load($this->session
    ->get('masquerading'));
  if (!$user) {

    // Ensure the flag is cleared.
    $this->session
      ->remove('masquerading');

    // User could be canceled while masquerading.
    return FALSE;
  }
  $account = $this
    ->switchUser($user);

  // Clear the masquerading flag after switching the user so that hook
  // implementations can differentiate this from a real logout/login.
  $this->session
    ->remove('masquerading');
  $this->logger
    ->info('User %username stopped masquerading as %old_username.', [
    '%username' => $user
      ->getDisplayName(),
    '%old_username' => $account
      ->getDisplayName(),
    'link' => $user
      ->toLink($this
      ->t('view'))
      ->toString(),
  ]);
  return TRUE;
}