You are here

function _nodeaccess_get_grants in Nodeaccess 8

Same name and namespace in other branches
  1. 8.2 nodeaccess.module \_nodeaccess_get_grants()
  2. 6.2 nodeaccess.module \_nodeaccess_get_grants()
  3. 6 nodeaccess.module \_nodeaccess_get_grants()

Return the grants applied to a node object used for Grant form.

Return value

array An array of grants with keys 'rid' for roles and 'uid' for users.

1 call to _nodeaccess_get_grants()
nodeaccess_node_access in ./nodeaccess.module
Implements hook_node_access().

File

./nodeaccess.module, line 290
Control access to site content based on the users and roles.

Code

function _nodeaccess_get_grants($node) {
  $grants = [];

  // Load all roles.
  $db = \Drupal::database();
  $result = $db
    ->select('nodeaccess', 'na')
    ->fields('na', [
    'gid',
    'grant_view',
    'grant_update',
    'grant_delete',
  ])
    ->condition('nid', $node->id, '=')
    ->condition('realm', 'nodeaccess_rid', '=')
    ->execute()
    ->fetchAll();
  foreach ($result as $grant) {
    $grants['rid'][$grant->gid] = [
      'grant_view' => $grant->grant_view,
      'grant_update' => $grant->grant_update,
      'grant_delete' => $grant->grant_delete,
    ];
  }

  // Load users from node_access.
  $db = \Drupal::database();
  $entries = $db
    ->select('nodeaccess', 'n');
  $entries
    ->join('users_field_data', 'ufd', 'ufd.uid = n.gid');
  $entries
    ->fields('n', [
    'grant_view',
    'grant_update',
    'grant_delete',
  ])
    ->condition('n.nid', $node->id, '=')
    ->condition('n.realm', 'nodeaccess_uid', '=')
    ->orderBy('ufd.name', 'ASC');
  $result = $entries
    ->execute();
  $results = [];
  while ($data = $result
    ->fetchObject()) {
    $results[] = $data;
  }
  foreach ($result as $account) {
    $grants['uid'][$account->uid] = [
      'name' => $account->name,
      'keep' => 1,
      'grant_view' => $account->grant_view,
      'grant_update' => $account->grant_update,
      'grant_delete' => $account->grant_delete,
    ];
  }
  return $grants;
}