"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Session/AccountProxy.php" (26 Nov 2020, 5992 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AccountProxy.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.0.8_vs_9.1.0-rc1.

    1 <?php
    2 
    3 namespace Drupal\Core\Session;
    4 
    5 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
    6 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
    7 
    8 /**
    9  * A proxied implementation of AccountInterface.
   10  *
   11  * The reason why we need an account proxy is that we don't want to have global
   12  * state directly stored in the container.
   13  *
   14  * This proxy object avoids multiple invocations of the authentication manager
   15  * which can happen if the current user is accessed in constructors. It also
   16  * allows legacy code to change the current user where the user cannot be
   17  * directly injected into dependent code.
   18  */
   19 class AccountProxy implements AccountProxyInterface {
   20 
   21   use DependencySerializationTrait;
   22 
   23   /**
   24    * The instantiated account.
   25    *
   26    * @var \Drupal\Core\Session\AccountInterface
   27    */
   28   protected $account;
   29 
   30   /**
   31    * Account id.
   32    *
   33    * @var int
   34    */
   35   protected $id = 0;
   36 
   37   /**
   38    * Initial account id.
   39    *
   40    * @var int
   41    *
   42    * @deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use
   43    *   $this->id instead.
   44    */
   45   protected $initialAccountId;
   46 
   47   /**
   48    * Event dispatcher.
   49    *
   50    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   51    */
   52   protected $eventDispatcher;
   53 
   54   /**
   55    * AccountProxy constructor.
   56    *
   57    * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
   58    *   Event dispatcher.
   59    */
   60   public function __construct(EventDispatcherInterface $eventDispatcher = NULL) {
   61     if (!$eventDispatcher) {
   62       @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);
   63       $eventDispatcher = \Drupal::service('event_dispatcher');
   64     }
   65     $this->eventDispatcher = $eventDispatcher;
   66   }
   67 
   68   /**
   69    * {@inheritdoc}
   70    */
   71   public function setAccount(AccountInterface $account) {
   72     // If the passed account is already proxied, use the actual account instead
   73     // to prevent loops.
   74     if ($account instanceof static) {
   75       $account = $account->getAccount();
   76     }
   77     $this->account = $account;
   78     $this->id = $account->id();
   79     $this->eventDispatcher->dispatch(AccountEvents::SET_USER, new AccountSetEvent($account));
   80   }
   81 
   82   /**
   83    * {@inheritdoc}
   84    */
   85   public function getAccount() {
   86     if (!isset($this->account)) {
   87       if ($this->id) {
   88         // After the container is rebuilt, DrupalKernel sets the initial
   89         // account to the id of the logged in user. This is necessary in order
   90         // to refresh the user account reference here.
   91         $this->setAccount($this->loadUserEntity($this->id));
   92       }
   93       else {
   94         $this->account = new AnonymousUserSession();
   95       }
   96     }
   97 
   98     return $this->account;
   99   }
  100 
  101   /**
  102    * {@inheritdoc}
  103    */
  104   public function id() {
  105     return $this->id;
  106   }
  107 
  108   /**
  109    * {@inheritdoc}
  110    */
  111   public function getRoles($exclude_locked_roles = FALSE) {
  112     return $this->getAccount()->getRoles($exclude_locked_roles);
  113   }
  114 
  115   /**
  116    * {@inheritdoc}
  117    */
  118   public function hasPermission($permission) {
  119     return $this->getAccount()->hasPermission($permission);
  120   }
  121 
  122   /**
  123    * {@inheritdoc}
  124    */
  125   public function isAuthenticated() {
  126     return $this->getAccount()->isAuthenticated();
  127   }
  128 
  129   /**
  130    * {@inheritdoc}
  131    */
  132   public function isAnonymous() {
  133     return $this->getAccount()->isAnonymous();
  134   }
  135 
  136   /**
  137    * {@inheritdoc}
  138    */
  139   public function getPreferredLangcode($fallback_to_default = TRUE) {
  140     return $this->getAccount()->getPreferredLangcode($fallback_to_default);
  141   }
  142 
  143   /**
  144    * {@inheritdoc}
  145    */
  146   public function getPreferredAdminLangcode($fallback_to_default = TRUE) {
  147     return $this->getAccount()->getPreferredAdminLangcode($fallback_to_default);
  148   }
  149 
  150   /**
  151    * {@inheritdoc}
  152    */
  153   public function getUsername() {
  154     @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);
  155     return $this->getAccountName();
  156   }
  157 
  158   /**
  159    * {@inheritdoc}
  160    */
  161   public function getAccountName() {
  162     return $this->getAccount()->getAccountName();
  163   }
  164 
  165   /**
  166    * {@inheritdoc}
  167    */
  168   public function getDisplayName() {
  169     return $this->getAccount()->getDisplayName();
  170   }
  171 
  172   /**
  173    * {@inheritdoc}
  174    */
  175   public function getEmail() {
  176     return $this->getAccount()->getEmail();
  177   }
  178 
  179   /**
  180    * {@inheritdoc}
  181    */
  182   public function getTimeZone() {
  183     return $this->getAccount()->getTimeZone();
  184   }
  185 
  186   /**
  187    * {@inheritdoc}
  188    */
  189   public function getLastAccessedTime() {
  190     return $this->getAccount()->getLastAccessedTime();
  191   }
  192 
  193   /**
  194    * {@inheritdoc}
  195    */
  196   public function setInitialAccountId($account_id) {
  197     if (isset($this->account)) {
  198       throw new \LogicException('AccountProxyInterface::setInitialAccountId() cannot be called after an account was set on the AccountProxy');
  199     }
  200 
  201     $this->id = $this->initialAccountId = $account_id;
  202   }
  203 
  204   /**
  205    * Load a user entity.
  206    *
  207    * The entity manager requires additional initialization code and cache
  208    * clearing after the list of modules is changed. Therefore it is necessary to
  209    * retrieve it as late as possible.
  210    *
  211    * Because of serialization issues it is currently not possible to inject the
  212    * container into the AccountProxy. Thus it is necessary to retrieve the
  213    * entity manager statically.
  214    *
  215    * @see https://www.drupal.org/node/2430447
  216    *
  217    * @param int $account_id
  218    *   The id of an account to load.
  219    *
  220    * @return \Drupal\Core\Session\AccountInterface|null
  221    *   An account or NULL if none is found.
  222    */
  223   protected function loadUserEntity($account_id) {
  224     return \Drupal::entityTypeManager()->getStorage('user')->load($account_id);
  225   }
  226 
  227 }