Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / entity / src / Access / EntityRevisionRouteAccessChecker.php
1 <?php
2
3 namespace Drupal\entity\Access;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\ContentEntityInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Routing\Access\AccessInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\Routing\Route;
13
14 /**
15  * Checks access to a entity revision.
16  */
17 class EntityRevisionRouteAccessChecker implements AccessInterface {
18
19   /**
20    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21    */
22   protected $entityTypeManager;
23
24   /**
25    * Stores calculated access check results.
26    *
27    * @var array
28    */
29   protected $accessCache = array();
30
31   /**
32    * The currently active route match object.
33    *
34    * @var \Drupal\Core\Routing\RouteMatchInterface
35    */
36   protected $routeMatch;
37
38   /**
39    * Creates a new EntityRevisionRouteAccessChecker instance.
40    *
41    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
42    *   The entity manager.
43    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
44    *   The currently active route match object.
45    */
46   public function __construct(EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match) {
47     $this->entityTypeManager = $entity_type_manager;
48     $this->routeMatch = $route_match;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function access(Route $route, AccountInterface $account, RouteMatchInterface $route_match = NULL) {
55     if (empty($route_match)) {
56       $route_match = $this->routeMatch;
57     }
58
59     $operation = $route->getRequirement('_entity_access_revision');
60     list($entity_type_id, $operation) = explode('.', $operation, 2);
61
62     if ($operation === 'list') {
63       $_entity = $route_match->getParameter($entity_type_id);
64       return AccessResult::allowedIf($this->checkAccess($_entity, $account, $operation))->cachePerPermissions();
65     }
66     else {
67       $_entity_revision = $route_match->getParameter($entity_type_id . '_revision');
68       return AccessResult::allowedIf($_entity_revision && $this->checkAccess($_entity_revision, $account, $operation))->cachePerPermissions();
69     }
70   }
71
72   protected function checkAccess(ContentEntityInterface $entity, AccountInterface $account, $operation = 'view') {
73     $entity_type = $entity->getEntityType();
74     $entity_type_id = $entity->getEntityTypeId();
75     $entity_access = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
76
77     /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
78     $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
79
80     $map = [
81       'view' => "view all $entity_type_id revisions",
82       'list' => "view all $entity_type_id revisions",
83       'update' => "revert all $entity_type_id revisions",
84       'delete' => "delete all $entity_type_id revisions",
85     ];
86     $bundle = $entity->bundle();
87     $type_map = [
88       'view' => "view $entity_type_id $bundle revisions",
89       'list' => "view $entity_type_id $bundle revisions",
90       'update' => "revert $entity_type_id $bundle revisions",
91       'delete' => "delete $entity_type_id $bundle revisions",
92     ];
93
94     if (!$entity || !isset($map[$operation]) || !isset($type_map[$operation])) {
95       // If there was no node to check against, or the $op was not one of the
96       // supported ones, we return access denied.
97       return FALSE;
98     }
99
100     // Statically cache access by revision ID, language code, user account ID,
101     // and operation.
102     $langcode = $entity->language()->getId();
103     $cid = $entity->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $operation;
104
105     if (!isset($this->accessCache[$cid])) {
106       $admin_permission = $entity_type->getAdminPermission();
107
108       // Perform basic permission checks first.
109       if (!$account->hasPermission($map[$operation]) && !$account->hasPermission($type_map[$operation]) && ($admin_permission && !$account->hasPermission($admin_permission))) {
110         $this->accessCache[$cid] = FALSE;
111         return FALSE;
112       }
113
114       if (($admin_permission = $entity_type->getAdminPermission()) && $account->hasPermission($admin_permission)) {
115         $this->accessCache[$cid] = TRUE;
116       }
117       else {
118         // Entity access handlers are generally not aware of the "list" operation.
119         $operation = $operation == 'list' ? 'view' : $operation;
120         // First check the access to the default revision and finally, if the
121         // node passed in is not the default revision then access to that, too.
122         $this->accessCache[$cid] = $entity_access->access($entity_storage->load($entity->id()), $operation, $account) && ($entity->isDefaultRevision() || $entity_access->access($entity, $operation, $account));
123       }
124     }
125
126     return $this->accessCache[$cid];
127   }
128
129
130   /**
131    * Counts the number of revisions in the default language.
132    *
133    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
134    *   The entity.
135    * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
136    *   The entity storage.
137    *
138    * @return int
139    *   The number of revisions in the default language.
140    */
141   protected function countDefaultLanguageRevisions(ContentEntityInterface $entity, EntityStorageInterface $entity_storage) {
142     $entity_type = $entity->getEntityType();
143     $count = $entity_storage->getQuery()
144       ->allRevisions()
145       ->condition($entity_type->getKey('id'), $entity->id())
146       ->condition($entity_type->getKey('default_langcode'), 1)
147       ->count()
148       ->execute();
149     return $count;
150   }
151
152   /**
153    * Resets the access cache.
154    *
155    * @return $this
156    */
157   public function resetAccessCache() {
158     $this->accessCache = [];
159     return $this;
160   }
161
162 }