Skip to content

Commit 0fdee57

Browse files
authored
fix(entity_access): Add access result as cacheable dependency (#1372)
1 parent 0666a1f commit 0fdee57

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/Plugin/GraphQL/DataProducer/Entity/EntityAccess.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Core\Entity\EntityInterface;
66
use Drupal\Core\Session\AccountInterface;
7+
use Drupal\graphql\GraphQL\Execution\FieldContext;
78
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
89

910
/**
@@ -40,10 +41,12 @@ class EntityAccess extends DataProducerPluginBase {
4041
* @param string $operation
4142
* @param \Drupal\Core\Session\AccountInterface $user
4243
*
43-
* @return bool|\Drupal\Core\Access\AccessResultInterface
44+
* @return bool
4445
*/
45-
public function resolve(EntityInterface $entity, $operation = NULL, AccountInterface $user = NULL) {
46-
return $entity->access($operation ?? 'view', $user);
46+
public function resolve(EntityInterface $entity, ?string $operation, ?AccountInterface $user, FieldContext $context) {
47+
$result = $entity->access($operation ?? 'view', $user, TRUE);
48+
$context->addCacheableDependency($result);
49+
return $result->isAllowed();
4750
}
4851

4952
}

tests/src/Kernel/DataProducer/EntityTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drupal\Tests\graphql\Kernel\DataProducer;
44

55
use Drupal\Core\Access\AccessResult;
6+
use Drupal\Core\Access\AccessResultForbidden;
67
use Drupal\Core\Entity\EntityInterface;
78
use Drupal\Core\Language\LanguageInterface;
89
use Drupal\Core\Url;
@@ -298,7 +299,7 @@ public function testResolvePublished(): void {
298299
public function testResolveAccess(): void {
299300
$this->entity->expects($this->any())
300301
->method('access')
301-
->willReturn(FALSE);
302+
->willReturn(new AccessResultForbidden());
302303

303304
$this->assertFalse($this->executeDataProducer('entity_access', [
304305
'entity' => $this->entity,

0 commit comments

Comments
 (0)