0

I am using hook_node_grants to provide access to a realm based on whether someone is an 'owner' of a node. The issue is that menu_get_object() may call hook_node_grants recursively.

function hottopics_node_grants($account, $op) {
  $grants = array();
  ...
   try {
      $node = menu_get_object();
    } catch (Exception $e) {
      // URL mustn't be the correct format.
      return $grants;
    }
   ...
    // Is the person a member of the administrator group, or the owner of the parent node? 
    if (module_invoke('hottopicsresearch', 'display_moderated_research_access_callback', 'update', $node)) {
      $grants['view_hottopics_manager'] = array(1);
    }

    return $grants

The code recurses at $node = menu_get_object(). Drupal-7.

0

Load the node directly:

  // Do not use $node = menu_get_object();
  // This calls hook_access which results in a recursive loop
  $node_url = $_GET['q'];
  $nid = array_pop(explode('/', $node_url));
  if (!is_integer($nid)) {
    return $grants;
  }
  $node = node_load($nid);
0

The infinite loop you are noticing is caused from the fact that menu_get_object() causes Drupal to verify the currently logged-in user has access to the node, which causes your implementation of hook_node_grants() to be invoked again, which calls menu_get_object(), which causes your implementation of hook_node_grants() to be invoked again, which….

It is hook_node_access_records() that has access to the node object; the code you are using in hook_node_grants() should be used in hook_node_access_records().

See Node access rights for more information. In short, hook_node_grants() returns a realm => GID array, while hook_node_access_records() return an array containing realm, GID, priority, and three 0/1 values for the view/update/delete permissions the current user has.

  • I had hook_node_access_records as the first try - yes it does take a node parameter. However, because the rule is 'you have access if you are in a group or created the parent' I had a realm for every node and still had to load the node to work out whether the current user was the creator of the parent. – Interlated Apr 9 '13 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.