You are here

public function AccountProxy::setAccount in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Session/AccountProxy.php \Drupal\Core\Session\AccountProxy::setAccount()

Sets the currently wrapped account.

Setting the current account is highly discouraged! Instead, make sure to inject the desired user object into the dependent code directly.

A preferable method of account impersonation is to use \Drupal\Core\Session\AccountSwitcherInterface::switchTo() and \Drupal\Core\Session\AccountSwitcherInterface::switchBack().

Parameters

\Drupal\Core\Session\AccountInterface $account: The current account.

Overrides AccountProxyInterface::setAccount

1 call to AccountProxy::setAccount()
AccountProxy::getAccount in core/lib/Drupal/Core/Session/AccountProxy.php
Gets the currently wrapped account.

File

core/lib/Drupal/Core/Session/AccountProxy.php, line 57

Class

AccountProxy
A proxied implementation of AccountInterface.

Namespace

Drupal\Core\Session

Code

public function setAccount(AccountInterface $account) {

  // If the passed account is already proxied, use the actual account instead
  // to prevent loops.
  if ($account instanceof static) {
    $account = $account
      ->getAccount();
  }
  $this->account = $account;
  $this->id = $account
    ->id();
  $this->eventDispatcher
    ->dispatch(new AccountSetEvent($account), AccountEvents::SET_USER);
}