class RefinableCalculatedPermissions

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Session/RefinableCalculatedPermissions.php \Drupal\Core\Session\RefinableCalculatedPermissions

Represents a calculated set of permissions with cacheable metadata.

Hierarchy

Expanded class hierarchy of RefinableCalculatedPermissions

See also

\Drupal\Core\Session\AccessPolicyProcessor

6 files declare their use of RefinableCalculatedPermissions
AccessPolicyProcessorTest.php in core/tests/Drupal/Tests/Core/Session/AccessPolicyProcessorTest.php
PermissionCheckerTest.php in core/tests/Drupal/Tests/Core/Session/PermissionCheckerTest.php
PermissionsHashGeneratorTest.php in core/tests/Drupal/Tests/Core/Session/PermissionsHashGeneratorTest.php
RefinableCalculatedPermissionsTest.php in core/tests/Drupal/Tests/Core/Session/RefinableCalculatedPermissionsTest.php
SuperUserAccessPolicyTest.php in core/tests/Drupal/Tests/Core/Session/SuperUserAccessPolicyTest.php

... See full list

File

core/lib/Drupal/Core/Session/RefinableCalculatedPermissions.php, line 12

Namespace

Drupal\Core\Session
View source
class RefinableCalculatedPermissions implements RefinableCalculatedPermissionsInterface {
    use CalculatedPermissionsTrait;
    use RefinableCacheableDependencyTrait;
    
    /**
     * {@inheritdoc}
     */
    public function addItem(CalculatedPermissionsItemInterface $item, bool $overwrite = FALSE) : self {
        if (!$overwrite && ($existing = $this->getItem($item->getScope(), $item->getIdentifier()))) {
            $item = static::mergeItems($existing, $item);
        }
        $this->items[$item->getScope()][$item->getIdentifier()] = $item;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function removeItem(string $scope = AccessPolicyInterface::SCOPE_DRUPAL, string|int $identifier = AccessPolicyInterface::SCOPE_DRUPAL) : self {
        unset($this->items[$scope][$identifier]);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function removeItems() : self {
        $this->items = [];
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function removeItemsByScope(string $scope) : self {
        unset($this->items[$scope]);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function merge(CalculatedPermissionsInterface $calculated_permissions) : self {
        foreach ($calculated_permissions->getItems() as $item) {
            $this->addItem($item);
        }
        $this->addCacheableDependency($calculated_permissions);
        return $this;
    }
    
    /**
     * Merges two items of identical scope and identifier.
     *
     * @param \Drupal\Core\Session\CalculatedPermissionsItemInterface $a
     *   The first item to merge.
     * @param \Drupal\Core\Session\CalculatedPermissionsItemInterface $b
     *   The second item to merge.
     *
     * @return \Drupal\Core\Session\CalculatedPermissionsItemInterface
     *   A new item representing the merger of both items.
     */
    protected static function mergeItems(CalculatedPermissionsItemInterface $a, CalculatedPermissionsItemInterface $b) : CalculatedPermissionsItemInterface {
        // If either of the items is admin, the new one is too.
        $is_admin = $a->isAdmin() || $b->isAdmin();
        // Admin items don't need to have any permissions.
        $permissions = [];
        if (!$is_admin) {
            $permissions = array_unique(array_merge($a->getPermissions(), $b->getPermissions()));
        }
        return new CalculatedPermissionsItem($permissions, $is_admin, $a->getScope(), $a->getIdentifier());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::getCacheContexts public function 4
CacheableDependencyTrait::getCacheMaxAge public function 4
CacheableDependencyTrait::getCacheTags public function 4
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
CalculatedPermissionsTrait::$items protected property A list of calculated permission items, keyed by scope and identifier.
CalculatedPermissionsTrait::getItem public function
CalculatedPermissionsTrait::getItems public function
CalculatedPermissionsTrait::getItemsByScope public function
CalculatedPermissionsTrait::getScopes public function
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
RefinableCalculatedPermissions::addItem public function Adds a calculated permission item. Overrides RefinableCalculatedPermissionsInterface::addItem
RefinableCalculatedPermissions::merge public function Merge another calculated permissions object into this one. Overrides RefinableCalculatedPermissionsInterface::merge
RefinableCalculatedPermissions::mergeItems protected static function Merges two items of identical scope and identifier.
RefinableCalculatedPermissions::removeItem public function Removes a single calculated permission item from a given scope. Overrides RefinableCalculatedPermissionsInterface::removeItem
RefinableCalculatedPermissions::removeItems public function Removes all of the calculated permission items, regardless of scope. Overrides RefinableCalculatedPermissionsInterface::removeItems
RefinableCalculatedPermissions::removeItemsByScope public function Removes all of the calculated permission items for the given scope. Overrides RefinableCalculatedPermissionsInterface::removeItemsByScope

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.