class TempstoreAccess

Same name in other branches
  1. 4.0.x src/Access/TempstoreAccess.php \Drupal\ctools\Access\TempstoreAccess

Tempstore Access for ctools.

Hierarchy

Expanded class hierarchy of TempstoreAccess

1 string reference to 'TempstoreAccess'
ctools.services.yml in ./ctools.services.yml
ctools.services.yml
1 service uses TempstoreAccess
ctools.access in ./ctools.services.yml
Drupal\ctools\Access\TempstoreAccess

File

src/Access/TempstoreAccess.php, line 16

Namespace

Drupal\ctools\Access
View source
class TempstoreAccess implements CoreAccessInterface {
    
    /**
     * The shared tempstore factory.
     *
     * @var \Drupal\Core\TempStore\SharedTempStoreFactory
     */
    protected $tempstore;
    
    /**
     * Constructor for access to shared tempstore.
     */
    public function __construct(SharedTempStoreFactory $tempstore) {
        $this->tempstore = $tempstore;
    }
    
    /**
     * Retreive the tempstore factory.
     */
    protected function getTempstore() {
        return $this->tempstore;
    }
    
    /**
     * Access method to find if user has access to a particular tempstore.
     *
     * @param \Symfony\Component\Routing\Route $route
     * @param \Drupal\Core\Routing\RouteMatchInterface $match
     * @param \Drupal\Core\Session\AccountInterface $account
     *
     * @return \Drupal\Core\Access\AccessResultAllowed|\Drupal\Core\Access\AccessResultForbidden
     */
    public function access(Route $route, RouteMatchInterface $match, AccountInterface $account) {
        $tempstore_id = $match->getParameter('tempstore_id') ? $match->getParameter('tempstore_id') : $route->getDefault('tempstore_id');
        $id = $match->getParameter($route->getRequirement('_ctools_access'));
        if ($tempstore_id && $id) {
            $cached_values = $this->getTempstore()
                ->get($tempstore_id)
                ->get($id);
            if (!empty($cached_values['access']) && $cached_values['access'] instanceof CToolsAccessInterface) {
                $access = $cached_values['access']->access($account);
            }
            else {
                $access = AccessResult::allowed();
            }
        }
        else {
            $access = AccessResult::forbidden();
        }
        // The different wizards will have different tempstore ids and adding this
        // cache context allows us to nuance the access per wizard.
        $access->addCacheContexts([
            'url.query_args:tempstore_id',
        ]);
        return $access;
    }

}

Members

Title Sort descending Modifiers Object type Summary
TempstoreAccess::$tempstore protected property The shared tempstore factory.
TempstoreAccess::access public function Access method to find if user has access to a particular tempstore.
TempstoreAccess::getTempstore protected function Retreive the tempstore factory.
TempstoreAccess::__construct public function Constructor for access to shared tempstore.