node_access_example.module

  1. examples
    1. 6 node_access_example/node_access_example.module
    2. 7 node_access_example/node_access_example.module
    3. 8 node_access_example/node_access_example.module
  2. drupal
    1. 4.6 developer/examples/node_access_example.module
    2. 4.7 developer/examples/node_access_example.module
    3. 5 developer/examples/node_access_example.module

This is an example illustrating how to restrict access to nodes based on some criterion associated with the user.

This example module will simply set a single flag on a node: 'private'. If the flag is set, only users with the 'view private content' flag can see the node, and all users with 'edit private content' can edit (but not delete) the node.

Additionally we will ensure that the node author can always view, edit, and delete the node by providing an additional access realm that grants privileges to the node's author.

Database definition:

  CREATE TABLE node_access_example (
    nid int(10) unsigned NOT NULL default '0' PRIMARY KEY,
    private int,
    KEY `node_example_nid` (nid)
  )

Functions & methods

NameDescription
node_access_example_form_alterImplementation of hook_form_alter()
node_access_example_nodeapiImplementation of hook_nodeapi().
node_access_example_node_access_recordsImplementation of hook_node_access_records().
node_access_example_node_grantsImplementation of hook_node_grants().
node_access_example_permImplementation of hook_perm().

File

node_access_example/node_access_example.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * This is an example illustrating how to restrict access to nodes based on
  5. * some criterion associated with the user.
  6. *
  7. * This example module will simply set a single flag on a node: 'private'. If
  8. * the flag is set, only users with the 'view private content' flag can see
  9. * the node, and all users with 'edit private content' can edit (but not
  10. * delete) the node.
  11. *
  12. * Additionally we will ensure that the node author can always view, edit,
  13. * and delete the node by providing an additional access realm that grants
  14. * privileges to the node's author.
  15. *
  16. * Database definition:
  17. * @code
  18. * CREATE TABLE node_access_example (
  19. * nid int(10) unsigned NOT NULL default '0' PRIMARY KEY,
  20. * private int,
  21. * KEY `node_example_nid` (nid)
  22. * )
  23. * @endcode
  24. */
  25. /**
  26. * @defgroup node_access_example Example: Node Access
  27. * @ingroup examples
  28. * @{
  29. * Examples of node access restriction. (drupal 6)
  30. *
  31. * This is an example illustrating how to restrict access to nodes based on
  32. * some criterion associated with the user.
  33. *
  34. * This example module will simply set a single flag on a node: 'private'. If
  35. * the flag is set, only users with the 'view private content' flag can see
  36. * the node, and all users with 'edit private content' can edit (but not
  37. * delete) the node.
  38. *
  39. * Additionally we will ensure that the node author can always view, edit,
  40. * and delete the node by providing an additional access realm that grants
  41. * privileges to the node's author.
  42. *
  43. * Database definition:
  44. * @code
  45. * CREATE TABLE node_access_example (
  46. * nid int(10) unsigned NOT NULL default '0' PRIMARY KEY,
  47. * private int,
  48. * KEY `node_example_nid` (nid)
  49. * )
  50. * @endcode
  51. *
  52. * This example is part of the Examples for Developers Project which you can download
  53. * and experiment with here: http://drupal.org/project/examples
  54. */
  55. /**
  56. * Implementation of hook_perm().
  57. *
  58. * In this example, we will use a simple permission to determine whether a user
  59. * has access to "private" content. This permission is defined here.
  60. */
  61. function node_access_example_perm() {
  62. return array('access private content', 'edit private content');
  63. }
  64. /**
  65. * Implementation of hook_node_grants().
  66. *
  67. * Tell the node access system what GIDs the user belongs to for each realm.
  68. * In this example, we are providing two realms: the example realm, which
  69. * has just one group id (1) and the user is either a member or not depending
  70. * upon the operation and the access permission set.
  71. *
  72. * We are also setting up a realm for the node author, though, to give it
  73. * special privileges. That has 1 GID for every UID, and each user is
  74. * automatically a member of the group where GID == UID.
  75. *
  76. */
  77. function node_access_example_node_grants($account, $op) {
  78. if ($op == 'view' && user_access('access private content', $account)) {
  79. $grants['example'] = array(1);
  80. }
  81. if (($op == 'update' || $op == 'delete') && user_access('edit private content', $account)) {
  82. $grants['example'] = array(1);
  83. }
  84. $grants['example_author'] = array($account->uid);
  85. return $grants;
  86. }
  87. /**
  88. * Implementation of hook_node_access_records().
  89. *
  90. * All node access modules must implement this hook. If the module is
  91. * interested in the privacy of the node passed in, return a list
  92. * of node access values for each grant ID we offer. Since this
  93. * example module only offers 1 grant ID, we will only ever be
  94. * returning one record.
  95. */
  96. function node_access_example_node_access_records($node) {
  97. // We only care about the node if it's been marked private. If not, it is
  98. // treated just like any other node and we completely ignore it.
  99. if (!empty($node->private)) {
  100. $grants = array();
  101. $grants[] = array(
  102. 'realm' => 'example',
  103. 'gid' => TRUE,
  104. 'grant_view' => TRUE,
  105. 'grant_update' => FALSE,
  106. 'grant_delete' => FALSE,
  107. 'priority' => 0,
  108. );
  109. // For the example_author array, the GID is equivalent to a UID, which
  110. // means there are many many groups of just 1 user.
  111. $grants[] = array(
  112. 'realm' => 'example_author',
  113. 'gid' => $node->uid,
  114. 'grant_view' => TRUE,
  115. 'grant_update' => TRUE,
  116. 'grant_delete' => TRUE,
  117. 'priority' => 0,
  118. );
  119. return $grants;
  120. }
  121. }
  122. /**
  123. * Implementation of hook_form_alter()
  124. *
  125. * This module adds a simple checkbox to the node form labeled private. If the
  126. * checkbox is labelled, only the node author and users with 'access private content'
  127. * privileges may see it.
  128. */
  129. function node_access_example_form_alter(&$form, $form_state) {
  130. if ($form['#id'] == 'node-form') {
  131. $form['private'] = array(
  132. '#type' => 'checkbox',
  133. '#title' => t('Private'),
  134. '#description' => t('Check here if this content should be set private and only shown to privileged users.'),
  135. '#default_value' => isset($form['#node']->private) ? $form['#node']->private : FALSE,
  136. );
  137. }
  138. }
  139. /**
  140. * Implementation of hook_nodeapi().
  141. *
  142. * The module must track the access status of the node.
  143. */
  144. function node_access_example_nodeapi(&$node, $op, $arg = 0) {
  145. switch ($op) {
  146. case 'load':
  147. $node->private = db_result(db_query('SELECT private FROM {node_access_example} WHERE nid = %d', $node->nid));
  148. break;
  149. case 'insert':
  150. db_query('INSERT INTO {node_access_example} (nid, private) VALUES (%d, %d)', $node->nid, $node->private);
  151. break;
  152. case 'update':
  153. db_query('UPDATE {node_access_example} SET private = %d WHERE nid = %d', $node->private, $node->nid);
  154. break;
  155. case 'delete':
  156. db_query('DELETE FROM {node_access_example} WHERE nid = %d', $node->nid);
  157. break;
  158. }
  159. }
  160. /**
  161. * @} End of "defgroup node_access_example".
  162. */
Login or register to post comments