3 namespace Drupal\Core\Session;
6 * A proxied implementation of AccountInterface.
8 * The reason why we need an account proxy is that we don't want to have global
9 * state directly stored in the container.
11 * This proxy object avoids multiple invocations of the authentication manager
12 * which can happen if the current user is accessed in constructors. It also
13 * allows legacy code to change the current user where the user cannot be
14 * directly injected into dependent code.
16 class AccountProxy implements AccountProxyInterface {
19 * The instantiated account.
21 * @var \Drupal\Core\Session\AccountInterface
37 * @deprecated Scheduled for removal in Drupal 8.4.x. Use $this->id instead.
39 protected $initialAccountId;
44 public function setAccount(AccountInterface $account) {
45 // If the passed account is already proxied, use the actual account instead
47 if ($account instanceof static) {
48 $account = $account->getAccount();
50 $this->account = $account;
51 $this->id = $account->id();
52 date_default_timezone_set(drupal_get_user_timezone());
58 public function getAccount() {
59 if (!isset($this->account)) {
61 // After the container is rebuilt, DrupalKernel sets the initial
62 // account to the id of the logged in user. This is necessary in order
63 // to refresh the user account reference here.
64 $this->setAccount($this->loadUserEntity($this->id));
67 $this->account = new AnonymousUserSession();
71 return $this->account;
77 public function id() {
84 public function getRoles($exclude_locked_roles = FALSE) {
85 return $this->getAccount()->getRoles($exclude_locked_roles);
91 public function hasPermission($permission) {
92 return $this->getAccount()->hasPermission($permission);
98 public function isAuthenticated() {
99 return $this->getAccount()->isAuthenticated();
105 public function isAnonymous() {
106 return $this->getAccount()->isAnonymous();
112 public function getPreferredLangcode($fallback_to_default = TRUE) {
113 return $this->getAccount()->getPreferredLangcode($fallback_to_default);
119 public function getPreferredAdminLangcode($fallback_to_default = TRUE) {
120 return $this->getAccount()->getPreferredAdminLangcode($fallback_to_default);
126 public function getUsername() {
127 return $this->getAccountName();
133 public function getAccountName() {
134 return $this->getAccount()->getAccountName();
140 public function getDisplayName() {
141 return $this->getAccount()->getDisplayName();
147 public function getEmail() {
148 return $this->getAccount()->getEmail();
154 public function getTimeZone() {
155 return $this->getAccount()->getTimeZone();
161 public function getLastAccessedTime() {
162 return $this->getAccount()->getLastAccessedTime();
168 public function setInitialAccountId($account_id) {
169 if (isset($this->account)) {
170 throw new \LogicException('AccountProxyInterface::setInitialAccountId() cannot be called after an account was set on the AccountProxy');
173 $this->id = $this->initialAccountId = $account_id;
177 * Load a user entity.
179 * The entity manager requires additional initialization code and cache
180 * clearing after the list of modules is changed. Therefore it is necessary to
181 * retrieve it as late as possible.
183 * Because of serialization issues it is currently not possible to inject the
184 * container into the AccountProxy. Thus it is necessary to retrieve the
185 * entity manager statically.
187 * @see https://www.drupal.org/node/2430447
189 * @param int $account_id
190 * The id of an account to load.
192 * @return \Drupal\Core\Session\AccountInterface|null
193 * An account or NULL if none is found.
195 protected function loadUserEntity($account_id) {
196 return \Drupal::entityManager()->getStorage('user')->load($account_id);