AccountProxy.php in Drupal 8
File
core/lib/Drupal/Core/Session/AccountProxy.php
View source
<?php
namespace Drupal\Core\Session;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class AccountProxy implements AccountProxyInterface {
use DependencySerializationTrait;
protected $account;
protected $id = 0;
protected $initialAccountId;
protected $eventDispatcher;
public function __construct(EventDispatcherInterface $eventDispatcher = NULL) {
if (!$eventDispatcher) {
@trigger_error('Calling AccountProxy::__construct() without the $eventDispatcher argument is deprecated in drupal:8.8.0. The $eventDispatcher argument will be required in drupal:9.0.0. See https://www.drupal.org/node/3009387', E_USER_DEPRECATED);
$eventDispatcher = \Drupal::service('event_dispatcher');
}
$this->eventDispatcher = $eventDispatcher;
}
public function setAccount(AccountInterface $account) {
if ($account instanceof static) {
$account = $account
->getAccount();
}
$this->account = $account;
$this->id = $account
->id();
$this->eventDispatcher
->dispatch(AccountEvents::SET_USER, new AccountSetEvent($account));
}
public function getAccount() {
if (!isset($this->account)) {
if ($this->id) {
$this
->setAccount($this
->loadUserEntity($this->id));
}
else {
$this->account = new AnonymousUserSession();
}
}
return $this->account;
}
public function id() {
return $this->id;
}
public function getRoles($exclude_locked_roles = FALSE) {
return $this
->getAccount()
->getRoles($exclude_locked_roles);
}
public function hasPermission($permission) {
return $this
->getAccount()
->hasPermission($permission);
}
public function isAuthenticated() {
return $this
->getAccount()
->isAuthenticated();
}
public function isAnonymous() {
return $this
->getAccount()
->isAnonymous();
}
public function getPreferredLangcode($fallback_to_default = TRUE) {
return $this
->getAccount()
->getPreferredLangcode($fallback_to_default);
}
public function getPreferredAdminLangcode($fallback_to_default = TRUE) {
return $this
->getAccount()
->getPreferredAdminLangcode($fallback_to_default);
}
public function getUsername() {
@trigger_error('\\Drupal\\Core\\Session\\AccountInterface::getUsername() is deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. Use \\Drupal\\Core\\Session\\AccountInterface::getAccountName() or \\Drupal\\user\\UserInterface::getDisplayName() instead. See https://www.drupal.org/node/2572493', E_USER_DEPRECATED);
return $this
->getAccountName();
}
public function getAccountName() {
return $this
->getAccount()
->getAccountName();
}
public function getDisplayName() {
return $this
->getAccount()
->getDisplayName();
}
public function getEmail() {
return $this
->getAccount()
->getEmail();
}
public function getTimeZone() {
return $this
->getAccount()
->getTimeZone();
}
public function getLastAccessedTime() {
return $this
->getAccount()
->getLastAccessedTime();
}
public function setInitialAccountId($account_id) {
if (isset($this->account)) {
throw new \LogicException('AccountProxyInterface::setInitialAccountId() cannot be called after an account was set on the AccountProxy');
}
$this->id = $this->initialAccountId = $account_id;
}
protected function loadUserEntity($account_id) {
return \Drupal::entityTypeManager()
->getStorage('user')
->load($account_id);
}
}