Error message

Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /home1/tylerfra/public_html/includes/common.inc).

Drupal - Custom Node Access Example

Category: 

To setup custom access control in Drupal 6, try something like this...

This example restricts access to everyone on a particular node type, unless the user belongs to a certain role or is the node author. Here is how I did it using hook_node_access_records and hook_node_grants in Drupal 6:

/*
 * Implements hook_node_access_records().
 */
function my_module_node_access_records($node) {
  $grants = array();
  if ($node->type == "my_custom_node_type") {
    $grants[] = array(
      'realm' => 'my-module-example',
      'gid' => $node->nid,
      'grant_view' => TRUE,
      'grant_update' => TRUE,
      'grant_delete' => TRUE,
      'priority' => 0,
    );
  }
  return $grants;
}
/*
 * Implements hook_node_grants().
 */
function my_module_node_grants($account, $op) {
  $grants = array();
  $grants['my-module-example'] = array();
  $sql = " SELECT nid, uid FROM {node} WHERE type = 'my_custom_node_type' ";
  $result = db_query($sql);
  while ($data = db_fetch_object($result)) {
    if ($account->uid == $data->uid || in_array("My Custom User Role", $account->roles)) {
      $grants['my-module-example'][] = $data->nid;
    }
  }
  return $grants;
}

Note, you may have to increase your 'priority' to have your node access records grant wweigh in properly, otherwise it may be ignored. You'll also most likely need to rebuild content permissions under 'Post settings' (admin/content/node-settings) anytime you make changes .

Note, this is now much easier in Drupal 7, check out hook_node_access. ["Also note that this function isn't called for node listings (e.g., RSS feeds, the default home page at path 'node', a recent content block, etc.)"]