W3cubDocs

/Drupal 8

public function AccountProxy::setAccount

public AccountProxy::setAccount(AccountInterface $account)

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

File

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

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;
  date_default_timezone_set(drupal_get_user_timezone());
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Session!AccountProxy.php/function/AccountProxy::setAccount/8.1.x