You are here

function node_access_test_node_grants in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/tests/modules/node_access_test/node_access_test.module \node_access_test_node_grants()
  2. 7 modules/node/tests/node_access_test.module \node_access_test_node_grants()
  3. 9 core/modules/node/tests/modules/node_access_test/node_access_test.module \node_access_test_node_grants()

Implements hook_node_grants().

Provides three grant realms:

  • node_access_test_author: Grants users view, update, and delete privileges on nodes they have authored. Users receive a group ID matching their user ID on this realm.
  • node_access_test: Grants users view privileges when they have the 'node test view' permission. Users with this permission receive two group IDs for the realm, 8888 and 8889. Access for both realms is identical; the second group is added so that the interaction of multiple groups on a given grant realm can be tested in NodeAccessPagerTest.
  • node_access_all: Provides grants for the user whose user ID matches the 'node_access_test.no_access_uid' state variable. Access control on this realm is not provided in this module; instead, NodeQueryAlterTest::testNodeQueryAlterOverride() manually writes a node access record defining the access control for this realm.

See also

\Drupal\node\Tests\NodeQueryAlterTest::testNodeQueryAlterOverride()

\Drupal\node\Tests\NodeAccessPagerTest

node_access_test.permissions.yml

node_access_test_node_access_records()

File

core/modules/node/tests/modules/node_access_test/node_access_test.module, line 52
Test module for testing the node access system.

Code

function node_access_test_node_grants($account, $op) {
  $grants = [];
  $grants['node_access_test_author'] = [
    $account
      ->id(),
  ];
  if ($op == 'view' && $account
    ->hasPermission('node test view')) {
    $grants['node_access_test'] = [
      8888,
      8889,
    ];
  }
  $no_access_uid = \Drupal::state()
    ->get('node_access_test.no_access_uid', 0);
  if ($op == 'view' && $account
    ->id() == $no_access_uid) {
    $grants['node_access_all'] = [
      0,
    ];
  }
  return $grants;
}