Drupal 10 compatibility | Drupal\Core\Entity\Query\QueryException: Entity queries must explicitly set whether the query should be access checked or not.

Created on 29 October 2023, about 1 year ago
Updated 28 August 2024, 3 months ago

Problem/Motivation

In Drupal 10 accessCheck() method should be explicitly called on all entity queries. Otherwise exception will be thrown.
See https://www.drupal.org/node/3201242

Steps to reproduce

Error can be reproduced in different places. One of them:

  • Update to Drupal 10.
  • Go to modules page admin/structure/opigno-modules and try to delete any module.

Proposed resolution

Add explicit call of accessCheck() on every entity query.

Remaining tasks

Review.

User interface changes

No.

API changes

No.

Data model changes

No.

🐛 Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

🇺🇦Ukraine oleksandr.s

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @oleksandr.s
  • 🇺🇦Ukraine oleksandr.s

    Here is patch to fix issue.

  • Issue was unassigned.
  • Status changed to Needs review about 1 year ago
  • Status changed to Needs work about 1 year ago
  • 🇬🇧United Kingdom catch

    Most of these should be accessCheck(FALSE) - anything that's not a listing query for showing information to the user. In practice there won't be query access on most of these entity types, but better to be explicit.

  • 🇺🇦Ukraine oleksandr.s

    When I was developing this patch I was trying to not break current functionality and keep it the same as it was before.
    So If before we had:
    BEFORE:

    // This gets all answers the current user can view.
    $query = $answer_storage->getQuery();
    $aids = $query->condition('user_id', $this->getOwnerId())
      ->condition('user_module_status', $this->id())
      ->execute();
    

    then, to make result the same we need to change like this:
    AFTER:

    Unchanged: This gets all answers the current user can view.
    $query = $answer_storage->getQuery();
    $aids = $query->condition('user_id', $this->getOwnerId())
      ->condition('user_module_status', $this->id())
      ->accessCheck()
      ->execute();
    

    Please, check this example BEFORE and AFTER https://www.drupal.org/node/3201242 . It shows the similar query examples but with articles content type and how to achieve the same query results after explicitly calling accessCheck().

  • Status changed to Needs review about 1 year ago
  • 🇧🇪Belgium kristiaanvandeneynde Antwerp, Belgium

    I would also argue for keeping functionality as is, so adding accessCheck() to all queries that didn't call said method before seems fair. Having said that, when I updated Group and its ecosystem to D10 I did break this rule for config entity types.

    Config entity types never ran query access to begin with, so adding accessCheck() to them does nothing. I chose to add all of these access checks with accessCheck(FALSE) except for the list builder. If core then ever decides to actually add access checking to config entities, then all queries but the list will keep behaving like they did before (not checking access).

    Some nitpicks:

    1. +++ b/opigno_module.module
      @@ -1009,6 +1010,7 @@ function opigno_module_views_query_alter(ViewExecutable $view, QueryPluginBase $
               $query_a = $activities_storage->getQuery();
      +        $query_a->accessCheck();
      

      Can keep this as a oneliner as accessCheck() returns the query object.

    2. +++ b/src/Controller/OpignoModuleManagerController.php
      @@ -501,6 +501,7 @@ class OpignoModuleManagerController extends ControllerBase {
           $query = $activities_storage->getQuery();
      +    $query->accessCheck();
      

      Same as above

    Long story short: Keep the behavior unchanged by adding accessCheck() rather than accessCheck(FALSE). Then perhaps in a follow-up we could argue whether these actually make sense by anaylizing all accessCheck() calls and re-evaluating them (also keeping my note about config entities in mind).

    But this issue is merely about adding D10 support and for that purpose, the current patch is sufficient.

    Looks good to me and as far as I am concerned this is RTBC but waiting a bit to see if @catch has anything else to add.

  • 🇬🇧United Kingdom catch

    Yeah adding them as-is then re-evaluating in a follow-up seems OK. The worst situation is when sites use accessCheck(TRUE) in updates, which can mean skipping entities in the update.

  • Status changed to Needs work about 1 year ago
  • 🇧🇪Belgium kristiaanvandeneynde Antwerp, Belgium

    I'd say fix the nitpicks and create a follow-up "Task" issue "Evaluate the accessCheck() calls on entity queries" and then this looks good to me.

  • Status changed to Needs review 9 months ago
  • 🇮🇳India sarwan_verma

    Hi @oleksandr.s,

    I have fixed this issue "Drupal 10 compatibility | Drupal\Core\Entity\Query\QueryException: Entity queries must explicitly set whether the query should be access checked or not." and also attached patch ,
    please review and verify.

  • 🇦🇷Argentina leonel.umerez

    Hi all!

    Here is a patch for the 3.1.2 module version

    Please review and verify, regards!

  • 🇦🇷Argentina leonel.umerez

    Hi all!

    This is a patch for module version 3.1.2,

    Please check and verify, regards!

  • 🇦🇷Argentina leonel.umerez

    Hi all!

    This is a patch for module version 3.1.2, please verify, regards!

Production build 0.71.5 2024