Skip to content

Commit d235c18

Browse files
Support for entity query access check (#790)
Fix api_product entity query access issue by adding support for entity query access check
1 parent c446600 commit d235c18

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/Entity/Query/Query.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace Drupal\apigee_edge\Entity\Query;
2121

22+
use Drupal\Core\Cache\CacheableMetadata;
2223
use Drupal\Core\Entity\EntityInterface;
2324
use Drupal\Core\Entity\EntityTypeInterface;
2425
use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -84,9 +85,40 @@ public function execute() {
8485
// Basically, DeveloperAppQuery already applies a condition on the returned
8586
// result because this function gets called.
8687
$all_records = $this->getFromStorage();
87-
$filter = $this->condition->compile($this);
8888

89+
// @todo Proper entity query support that is aligned with the implementation
90+
// in \Drupal\Core\Entity\Query\Sql\Query::prepare() can be only added
91+
// if the following Entity API module issue is solved.
92+
// https://www.drupal.org/project/entity/issues/3332956
93+
// (Having a fix for a similar Group module issue is a nice to have,
94+
// https://www.drupal.org/project/group/issues/3332963.)
95+
if ($this->accessCheck) {
96+
// Read meta-data from query, if provided.
97+
if (!$account = $this->getMetaData('account')) {
98+
// @todo DI dependency.
99+
$account = \Drupal::currentUser();
100+
}
101+
$cacheability = CacheableMetadata::createFromRenderArray([]);
102+
$all_records = array_filter($all_records, static function (EntityInterface $entity) use ($cacheability, $account) {
103+
// Bubble up cacheability information even from a revoked access result.
104+
$result = $entity->access('view', $account, TRUE);
105+
$cacheability->addCacheableDependency($result);
106+
return $result->isAllowed();
107+
});
108+
// @todo DI dependencies.
109+
/** @var \Symfony\Component\HttpFoundation\Request $request */
110+
$request = \Drupal::requestStack()->getCurrentRequest();
111+
$renderer = \Drupal::service('renderer');
112+
if ($request->isMethodCacheable() && $renderer->hasRenderContext()) {
113+
$build = [];
114+
$cacheability->applyTo($build);
115+
$renderer->render($build);
116+
}
117+
}
118+
119+
$filter = $this->condition->compile($this);
89120
$result = array_filter($all_records, $filter);
121+
90122
if ($this->count) {
91123
return count($result);
92124
}

0 commit comments

Comments
 (0)