Node author cannot view its own unpublished node according to entity query access

Created on 7 March 2023, over 1 year ago

Problem/Motivation

View access is not granted to the author of the unpublished node on the entity query level. This is an issue in cases when available entity ids are covered first by an entity query and only passed to loadMultiple() afterward.

(Like when a filter call is fired via JSONAPI calls https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi... )

If you would wonder why all test passes in \Drupal\Tests\node_view_permissions\Functional\NodeViewPermissionsTest then it is because it checks entity access and we know that even if entity query level access is not granted, entity access can grant access (because the user has "view own unpublished content' permission).
The /node/[nid] page also works for the very same reason.

Steps to reproduce

A failing test case that can be added to \Drupal\Tests\node_view_permissions\Functional\NodeViewPermissionsTest

/**
   * Test users with a "view own unpublished content" permission.
   *
   * Ensure that these users can view nodes of this type that they created.
   */
  public function testViewOwnUnpublished() {
    $user1 = $this->drupalCreateUser(['view own unpublished content']);
    $user2 = $this->drupalCreateUser(['view own unpublished content']);

    $node = $this->drupalCreateNode([
      'type' => 'article',
      'uid' => $user1->id(),
      'status' => NodeInterface::NOT_PUBLISHED,
    ]);

    $lookup = [
      [$user1, Response::HTTP_OK],
      [$user2, Response::HTTP_FORBIDDEN],
    ];

    foreach ($lookup as $i) {
      [$user, $expected] = $i;

      $this->drupalLogin($user);

      $this->drupalGet(Url::fromRoute('entity.node.canonical', [
        'node' => $node->id(),
      ]));

      $this->assertSession()->statusCodeEquals($expected);
    }

    $result = \Drupal::entityQuery('node')
      ->condition('nid', $node->id())
      ->addMetaData('account', $user1)
      ->accessCheck(TRUE)
      ->execute();

    self::assertNotEmpty($result, 'Access is granted to the author of an unpublished node in the query level.');
  }

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇭🇺Hungary mxr576 Hungary

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

Comments & Activities

Production build 0.71.5 2024