1

I'm writing a custom entity access handler for a custom entity that checks if a user has access to the underlying node. The checkAccess() code looks like this:

$uuid = $entity->uuid->getString();
if (isset($uuid)) {
  $nodes = $this->node->getQuery()->accessCheck(TRUE)->condition('uuid', $uuid)->execute();
  if (!empty($nodes)) {
    return AccessResult::allowed();
  }
}
return AccessResult::forbidden("Access denied.");

The problem is when I go to the jsonapi endpoint for the entity, I'm getting the error "The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\\jsonapi\\CacheableResourceResponse."

If I return accessResult::allowed() before the $nodes query it works. What's happening here?

2
  • 1
    accessCheck(TRUE) leaks metadata, see drupal.stackexchange.com/questions/251864/…. Why not use $entity->access('view')?
    – 4uk4
    Apr 7, 2022 at 15:15
  • Looks like if I load the node and then do $node->access('view') it works, will post that as an answer
    – Lambic
    Apr 7, 2022 at 15:20

1 Answer 1

2

It looks like using accessCheck(TRUE) causes this, so the workaround is:

if (isset($uuid)) {
  $node = $this->node->loadByProperties(['uuid' => $uuid]);
  $node = reset($action);
  if ($node->access('view')) {
    return AccessResult::allowed();
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.