Score:1

Entity access handler causing "The controller result claims to be providing relevant cache metadata, but leaked metadata was detected."

ph flag

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?

4uk4 avatar
cn flag
accessCheck(TRUE) leaks metadata, see https://drupal.stackexchange.com/questions/251864/logicexception-the-controller-result-claims-to-be-providing-relevant-cache-meta. Why not use $entity->access('view')?
Lambic avatar
ph flag
Looks like if I load the node and then do $node->access('view') it works, will post that as an answer
Score:2
ph flag

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();
  }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.