506ade6b482b3399c8e12e8ac3fbcdc6b37a7ce7
[yaffs-website] / web / modules / contrib / ctools / src / Access / TempstoreAccess.php
1 <?php
2
3 namespace Drupal\ctools\Access;
4
5
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\Routing\Access\AccessInterface as CoreAccessInterface;
8 use Drupal\Core\Routing\RouteMatch;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\ctools\Access\AccessInterface as CToolsAccessInterface;
11 use Drupal\user\SharedTempStoreFactory;
12 use Symfony\Component\Routing\Route;
13
14 class TempstoreAccess implements CoreAccessInterface {
15
16   /**
17    * The shared tempstore factory.
18    *
19    * @var \Drupal\user\SharedTempStoreFactory
20    */
21   protected $tempstore;
22
23   public function __construct(SharedTempStoreFactory $tempstore) {
24     $this->tempstore = $tempstore;
25   }
26
27   protected function getTempstore() {
28     return $this->tempstore;
29   }
30
31   public function access(Route $route, RouteMatch $match, AccountInterface $account) {
32     $tempstore_id = $match->getParameter('tempstore_id') ? $match->getParameter('tempstore_id') : $route->getDefault('tempstore_id');
33     $id = $match->getParameter($route->getRequirement('_ctools_access'));
34     if ($tempstore_id && $id) {
35       $cached_values = $this->getTempstore()->get($tempstore_id)->get($id);
36       if (!empty($cached_values['access']) && ($cached_values['access'] instanceof CToolsAccessInterface)) {
37         $access = $cached_values['access']->access($account);
38       }
39       else {
40         $access = AccessResult::allowed();
41       }
42     }
43     else {
44       $access = AccessResult::forbidden();
45     }
46     // The different wizards will have different tempstore ids and adding this
47     // cache context allows us to nuance the access per wizard.
48     $access->addCacheContexts(['url.query_args:tempstore_id']);
49     return $access;
50   }
51 }