summaryrefslogtreecommitdiffstats
path: root/core/modules/node/src/Cache
diff options
context:
space:
mode:
authorNathaniel Catchpole2015-03-23 14:57:21 (GMT)
committerNathaniel Catchpole2015-03-23 14:57:21 (GMT)
commit11f554993702b64202a2682af92f1a1b52113e97 (patch)
treeb01714610483eedeab2d76469b925912dc1da4d7 /core/modules/node/src/Cache
parent8dbdbd76efb57e6f245ecfa744225e5ee856ab89 (diff)
Issue #2432837 by Wim Leers, Fabianx: Make cache contexts hierarchical (e.g. 'user' is more specific than 'user.roles')
Diffstat (limited to 'core/modules/node/src/Cache')
-rw-r--r--core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php80
-rw-r--r--core/modules/node/src/Cache/NodeAccessViewGrantsCacheContext.php69
2 files changed, 80 insertions, 69 deletions
diff --git a/core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php b/core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php
new file mode 100644
index 0000000..e213c40
--- /dev/null
+++ b/core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php
@@ -0,0 +1,80 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Cache\NodeAccessGrantsCacheContext.
+ */
+
+namespace Drupal\node\Cache;
+
+use Drupal\Core\Cache\CalculatedCacheContextInterface;
+use Drupal\Core\Cache\UserCacheContext;
+
+/**
+ * Defines the node access view cache context service.
+ *
+ * This allows for node access grants-sensitive caching when listing nodes.
+ *
+ * @see node_query_node_access_alter()
+ */
+class NodeAccessGrantsCacheContext extends UserCacheContext implements CalculatedCacheContextInterface {
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function getLabel() {
+ return t("Content access view grants");
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getContext($operation = NULL) {
+ // If the current user either can bypass node access then we don't need to
+ // determine the exact node grants for the current user.
+ if ($this->user->hasPermission('bypass node access')) {
+ return 'all';
+ }
+
+ // When no specific operation is specified, check the grants for all three
+ // possible operations.
+ if ($operation === NULL) {
+ $result = [];
+ foreach (['view', 'update', 'delete'] as $op) {
+ $result[] = $this->checkNodeGrants($op);
+ }
+ return implode('-', $result);
+ }
+ else {
+ return $this->checkNodeGrants($operation);
+ }
+ }
+
+ /**
+ * Checks the node grants for the given operation.
+ *
+ * @param string $operation
+ * The operation to check the node grants for.
+ *
+ * @return string
+ * The string representation of the cache context.
+ */
+ protected function checkNodeGrants($operation) {
+ // When checking the grants for the 'view' operation and the current user
+ // has a global view grant (i.e. a view grant for node ID 0) — note that
+ // this is automatically the case if no node access modules exist (no
+ // hook_node_grants() implementations) then we don't need to determine the
+ // exact node view grants for the current user.
+ if ($operation === 'view' && node_access_view_all_nodes($this->user)) {
+ return 'view.all';
+ }
+
+ $grants = node_access_grants($operation, $this->user);
+ $grants_context_parts = [];
+ foreach ($grants as $realm => $gids) {
+ $grants_context_parts[] = $realm . ':' . implode(',', $gids);
+ }
+ return $operation . '.' . implode(';', $grants_context_parts);
+ }
+
+}
diff --git a/core/modules/node/src/Cache/NodeAccessViewGrantsCacheContext.php b/core/modules/node/src/Cache/NodeAccessViewGrantsCacheContext.php
deleted file mode 100644
index 24323b9..0000000
--- a/core/modules/node/src/Cache/NodeAccessViewGrantsCacheContext.php
+++ /dev/null
@@ -1,69 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\Core\Cache\NodeAccessViewGrantsCacheContext.
- */
-
-namespace Drupal\node\Cache;
-
-use Drupal\Core\Cache\CacheContextInterface;
-use Drupal\Core\Session\AccountInterface;
-
-/**
- * Defines the node access view grants cache context service.
- *
- * This allows for node access grants-sensitive caching when viewing nodes.
- *
- * @see node_query_node_access_alter()
- */
-class NodeAccessViewGrantsCacheContext implements CacheContextInterface {
-
- /**
- * The current user.
- *
- * @var \Drupal\Core\Session\AccountInterface
- */
- protected $user;
-
- /**
- * Constructs a new NodeAccessViewGrantsCacheContext service.
- *
- * @param \Drupal\Core\Session\AccountInterface $user
- * The current user.
- */
- public function __construct(AccountInterface $user) {
- $this->user = $user;
- }
-
- /**
- * {@inheritdoc}
- */
- public static function getLabel() {
- return t("Content access view grants");
- }
-
- /**
- * {@inheritdoc}
- */
- public function getContext() {
- // If the current user either:
- // - can bypass node access
- // - has a global view grant (such as a view grant for node ID 0) — note
- // that this is automatically the case if no node access modules exist (no
- // hook_node_grants() implementations)
- // then we don't need to determine the exact node view grants for the
- // current user.
- if ($this->user->hasPermission('bypass node access') || node_access_view_all_nodes($this->user)) {
- return 'all';
- }
-
- $grants = node_access_grants('view', $this->user);
- $grants_context_parts = [];
- foreach ($grants as $realm => $gids) {
- $grants_context_parts[] = $realm . ':' . implode(',', $gids);
- }
- return implode(';', $grants_context_parts);
- }
-
-}