"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php" (26 Nov 2020, 1808 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 "AccountPermissionsCacheContext.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\Core\Cache\Context;
    4 
    5 use Drupal\Core\Cache\CacheableMetadata;
    6 use Drupal\Core\Session\AccountInterface;
    7 use Drupal\Core\Session\PermissionsHashGeneratorInterface;
    8 
    9 /**
   10  * Defines the AccountPermissionsCacheContext service, for "per permission" caching.
   11  *
   12  * Cache context ID: 'user.permissions'.
   13  */
   14 class AccountPermissionsCacheContext extends UserCacheContextBase implements CacheContextInterface {
   15 
   16   /**
   17    * The permissions hash generator.
   18    *
   19    * @var \Drupal\Core\Session\PermissionsHashGeneratorInterface
   20    */
   21   protected $permissionsHashGenerator;
   22 
   23   /**
   24    * Constructs a new UserCacheContext service.
   25    *
   26    * @param \Drupal\Core\Session\AccountInterface $user
   27    *   The current user.
   28    * @param \Drupal\Core\Session\PermissionsHashGeneratorInterface $permissions_hash_generator
   29    *   The permissions hash generator.
   30    */
   31   public function __construct(AccountInterface $user, PermissionsHashGeneratorInterface $permissions_hash_generator) {
   32     $this->user = $user;
   33     $this->permissionsHashGenerator = $permissions_hash_generator;
   34   }
   35 
   36   /**
   37    * {@inheritdoc}
   38    */
   39   public static function getLabel() {
   40     return t("Account's permissions");
   41   }
   42 
   43   /**
   44    * {@inheritdoc}
   45    */
   46   public function getContext() {
   47     return $this->permissionsHashGenerator->generate($this->user);
   48   }
   49 
   50   /**
   51    * {@inheritdoc}
   52    */
   53   public function getCacheableMetadata() {
   54     $cacheable_metadata = new CacheableMetadata();
   55 
   56     // The permissions hash changes when:
   57     // - a user is updated to have different roles;
   58     $tags = ['user:' . $this->user->id()];
   59     // - a role is updated to have different permissions.
   60     foreach ($this->user->getRoles() as $rid) {
   61       $tags[] = "config:user.role.$rid";
   62     }
   63 
   64     return $cacheable_metadata->setCacheTags($tags);
   65   }
   66 
   67 }