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.

Database definition:

  DELETE FROM node_access;
  INSERT INTO node_access (nid, gid, realm, grant_view, grant_update,
     grant_delete) VALUES (0, 1, 'example', 1, 0, 0)

This SQL needs to be run before the module will work properly. The installer system will probably perform this work in the future. The first line removes any existing grants, including (most importantly) the universal grant installed by default that gives read access to every node for everyone. The second line grants read access to every node for users with the "access private content" permission; in the scheme we're defining here, each node will either be private (in which case it can always be read by anyone with that permission) or public (in which case it can be read by everyone). We'll take care of public nodes in node_access_example_nodeapi().

Functions & methods

NameDescription
node_access_example_form_alterImplementation of hook_form_alter().
node_access_example_helpImplementation of hook_help().
node_access_example_nodeapiImplementation of hook_nodeapi().
node_access_example_node_grantsImplementation of hook_node_grants().
node_access_example_permImplementation of hook_perm().

File

developer/examples/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 some
  5. * criterion associated with the user.
  6. *
  7. * Database definition:
  8. * @code
  9. * DELETE FROM node_access;
  10. * INSERT INTO node_access (nid, gid, realm, grant_view, grant_update,
  11. * grant_delete) VALUES (0, 1, 'example', 1, 0, 0)
  12. * @endcode
  13. *
  14. * This SQL needs to be run before the module will work properly. The installer
  15. * system will probably perform this work in the future. The first line removes
  16. * any existing grants, including (most importantly) the universal grant
  17. * installed by default that gives read access to every node for everyone. The
  18. * second line grants read access to every node for users with the "access
  19. * private content" permission; in the scheme we're defining here, each node
  20. * will either be private (in which case it can always be read by anyone with
  21. * that permission) or public (in which case it can be read by everyone). We'll
  22. * take care of public nodes in node_access_example_nodeapi().
  23. */
  24. /**
  25. * Implementation of hook_help().
  26. */
  27. function node_access_example_help($section) {
  28. switch ($section) {
  29. case 'admin/modules#description':
  30. return t('An example illustrating how to restrict access to nodes based on some criterion associated with the user.');
  31. }
  32. }
  33. /**
  34. * Implementation of hook_perm().
  35. *
  36. * In this example, we will use a simple permission to determine whether a user
  37. * has access to "private" content. This permission is defined here.
  38. */
  39. function node_access_example_perm() {
  40. return array('access private content');
  41. }
  42. /**
  43. * Implementation of hook_node_grants().
  44. *
  45. * Since we are restricting access based on a permission, we need to check that
  46. * permission and return the appropriate result.
  47. *
  48. */
  49. function node_access_example_node_grants($account, $op) {
  50. $grants = array();
  51. if (user_access('access content', $account)) {
  52. $grants[] = 0;
  53. }
  54. if (user_access('access private content', $account)) {
  55. $grants[] = 1;
  56. }
  57. return array('example' => $grants);
  58. }
  59. /**
  60. * Implementation of hook_form_alter().
  61. *
  62. * We use this to alter the node editing form and insert a check box so the
  63. * admins can manage the node's access rights.
  64. *
  65. * Modules may wish to provide default grants per node type using this hook.
  66. */
  67. function node_access_example_form_alter($form_id, &$form) {
  68. // This hook is called for all forms. We only want to work with node settings
  69. // and edit forms.
  70. if (isset($form['type'])) {
  71. // Node settings form.
  72. if ($form['type']['#value'] .'_node_settings' == $form_id) {
  73. // If the module needed it, this would be where you would insert controls
  74. // for the node's settings form.
  75. }
  76. // Node edit form for users with "administer nodes" permission.
  77. if ($form['type']['#value'] .'_node_form' == $form_id && user_access('administer nodes')) {
  78. $node = $form['#node'];
  79. if (!isset($node->access_example)) {
  80. // Load the grants from the database.
  81. $result = db_query('SELECT na.gid FROM {node_access} na WHERE na.nid = %d AND na.realm = \'example\' AND na.grant_view = 1', $node->nid);
  82. $grant = db_fetch_object($result);
  83. if ($grant && $grant->gid == 0) {
  84. // The "public" grant was set.
  85. $node->access_example = 0;
  86. }
  87. else {
  88. $node->access_example = 1;
  89. }
  90. }
  91. $form['access_example'] = array(
  92. '#type' => 'checkbox',
  93. '#title' => t('Private Node Access'),
  94. '#default_value' => $node->access_example,
  95. '#weight' => -10,
  96. '#description' => t('Make this node private.'),
  97. );
  98. }
  99. }
  100. }
  101. /**
  102. * Implementation of hook_nodeapi().
  103. *
  104. * Most of a node access module's work will be done via this hook. Several
  105. * values of $op will require responses:
  106. *
  107. * - "delete", "insert", and "update":
  108. * The module must take care of updating the node_access table appropriately
  109. * when nodes are modified, probably using the form element mentioned above.
  110. * Only the realm(s) handled by the module should be affected, so that multiple
  111. * node access modules can peacefully coexist.
  112. * - "validate":
  113. * Depending on the user interface provided in the node form, the selection
  114. * may need to be verified and validated here.
  115. */
  116. function node_access_example_nodeapi(&$node, $op, $arg = 0) {
  117. switch ($op) {
  118. case 'delete':
  119. // When a node is deleted, delete any relevant grants.
  120. db_query('DELETE FROM {node_access} WHERE nid = %d AND realm = \'example\'', $node->nid);
  121. break;
  122. case 'insert':
  123. case 'update':
  124. // Clear out any existing grants for the node, and set new ones.
  125. db_query('DELETE FROM {node_access} WHERE nid = %d AND realm = \'example\'', $node->nid);
  126. $node->access_example = isset($node->access_example) ? $node->access_example : 0;
  127. if ($node->access_example == 0) {
  128. // If the node is public, we need to grant access to everyone.
  129. db_query('INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, \'example\', %d, %d, %d)', $node->nid, 0, 1, 0, 0);
  130. }
  131. break;
  132. }
  133. }
Login or register to post comments