node_access_test.module 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * @file
  4. * Dummy module implementing node access related hooks to test API interaction
  5. * with the Node module. This module restricts view permission to those with
  6. * a special 'node test view' permission.
  7. */
  8. /**
  9. * Implements hook_node_grants().
  10. */
  11. function node_access_test_node_grants($account, $op) {
  12. $grants = array();
  13. // First grant a grant to the author for own content.
  14. $grants['node_access_test_author'] = array($account->uid);
  15. if ($op == 'view' && user_access('node test view', $account)) {
  16. $grants['node_access_test'] = array(8888, 8889);
  17. }
  18. if ($op == 'view' && $account->uid == variable_get('node_test_node_access_all_uid', 0)) {
  19. $grants['node_access_all'] = array(0);
  20. }
  21. return $grants;
  22. }
  23. /**
  24. * Implements hook_node_access_records().
  25. */
  26. function node_access_test_node_access_records($node) {
  27. $grants = array();
  28. // For NodeAccessBaseTableTestCase, only set records for private nodes.
  29. if (!variable_get('node_access_test_private') || $node->private) {
  30. $grants[] = array(
  31. 'realm' => 'node_access_test',
  32. 'gid' => 8888,
  33. 'grant_view' => 1,
  34. 'grant_update' => 0,
  35. 'grant_delete' => 0,
  36. 'priority' => 0,
  37. );
  38. $grants[] = array(
  39. 'realm' => 'node_access_test',
  40. 'gid' => 8889,
  41. 'grant_view' => 1,
  42. 'grant_update' => 0,
  43. 'grant_delete' => 0,
  44. 'priority' => 0,
  45. );
  46. // For the author realm, the GID is equivalent to a UID, which
  47. // means there are many many groups of just 1 user.
  48. $grants[] = array(
  49. 'realm' => 'node_access_test_author',
  50. 'gid' => $node->uid,
  51. 'grant_view' => 1,
  52. 'grant_update' => 1,
  53. 'grant_delete' => 1,
  54. 'priority' => 0,
  55. );
  56. }
  57. return $grants;
  58. }
  59. /**
  60. * Implements hook_permission().
  61. *
  62. * Sets up permissions for this module.
  63. */
  64. function node_access_test_permission() {
  65. return array('node test view' => array('title' => 'View content'));
  66. }
  67. /**
  68. * Implements hook_menu().
  69. *
  70. * Sets up a page that lists nodes.
  71. */
  72. function node_access_test_menu() {
  73. $items = array();
  74. $items['node_access_test_page'] = array(
  75. 'title' => 'Node access test',
  76. 'page callback' => 'node_access_test_page',
  77. 'access arguments' => array('access content'),
  78. 'type' => MENU_SUGGESTED_ITEM,
  79. );
  80. $items['node_access_entity_test_page'] = array(
  81. 'title' => 'Node access test',
  82. 'page callback' => 'node_access_entity_test_page',
  83. 'access arguments' => array('access content'),
  84. 'type' => MENU_SUGGESTED_ITEM,
  85. );
  86. return $items;
  87. }
  88. /**
  89. * Page callback for node access test page.
  90. *
  91. * Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
  92. * the number filled in) if there were nodes the user could access. Also, the
  93. * database query is shown, and a list of the node IDs, for debugging purposes.
  94. * And if there is a query exception, the page says "Exception" and gives the
  95. * error.
  96. */
  97. function node_access_test_page() {
  98. $output = '';
  99. try {
  100. $query = db_select('node', 'mytab')
  101. ->fields('mytab');
  102. $query->addTag('node_access');
  103. $result = $query->execute()->fetchAll();
  104. if (count($result)) {
  105. $output .= '<p>Yes, ' . count($result) . ' nodes</p>';
  106. $output .= '<ul>';
  107. foreach ($result as $item) {
  108. $output .= '<li>' . $item->nid . '</li>';
  109. }
  110. $output .= '</ul>';
  111. }
  112. else {
  113. $output .= '<p>No nodes</p>';
  114. }
  115. $output .= '<p>' . ((string) $query ) . '</p>';
  116. }
  117. catch (Exception $e) {
  118. $output = '<p>Exception</p>';
  119. $output .= '<p>' . $e->getMessage() . '</p>';
  120. }
  121. return $output;
  122. }
  123. /**
  124. * Page callback for node access entity test page.
  125. *
  126. * Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
  127. * the number filled in) if there were nodes the user could access. Also, the
  128. * database query is shown, and a list of the node IDs, for debugging purposes.
  129. * And if there is a query exception, the page says "Exception" and gives the
  130. * error.
  131. */
  132. function node_access_entity_test_page() {
  133. $output = '';
  134. try {
  135. $query = new EntityFieldQuery;
  136. $result = $query->fieldCondition('body', 'value', 'A', 'STARTS_WITH')->execute();
  137. if (!empty($result['node'])) {
  138. $output .= '<p>Yes, ' . count($result['node']) . ' nodes</p>';
  139. $output .= '<ul>';
  140. foreach ($result['node'] as $nid => $v) {
  141. $output .= '<li>' . $nid . '</li>';
  142. }
  143. $output .= '</ul>';
  144. }
  145. else {
  146. $output .= '<p>No nodes</p>';
  147. }
  148. }
  149. catch (Exception $e) {
  150. $output = '<p>Exception</p>';
  151. $output .= '<p>' . $e->getMessage() . '</p>';
  152. }
  153. return $output;
  154. }
  155. /**
  156. * Implements hook_form_BASE_FORM_ID_alter().
  157. */
  158. function node_access_test_form_node_form_alter(&$form, $form_state) {
  159. // Only show this checkbox for NodeAccessBaseTableTestCase.
  160. if (variable_get('node_access_test_private')) {
  161. $form['private'] = array(
  162. '#type' => 'checkbox',
  163. '#title' => t('Private'),
  164. '#description' => t('Check here if this content should be set private and only shown to privileged users.'),
  165. '#default_value' => isset($form['#node']->private) ? $form['#node']->private : FALSE,
  166. );
  167. }
  168. }
  169. /**
  170. * Implements hook_node_load().
  171. */
  172. function node_access_test_node_load($nodes, $types) {
  173. $result = db_query('SELECT nid, private FROM {node_access_test} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
  174. foreach ($result as $record) {
  175. $nodes[$record->nid]->private = $record->private;
  176. }
  177. }
  178. /**
  179. * Implements hook_node_delete().
  180. */
  181. function node_access_test_node_delete($node) {
  182. db_delete('node_access_test')->condition('nid', $node->nid)->execute();
  183. }
  184. /**
  185. * Implements hook_node_insert().
  186. */
  187. function node_access_test_node_insert($node) {
  188. _node_access_test_node_write($node);
  189. }
  190. /**
  191. * Implements hook_nodeapi_update().
  192. */
  193. function node_access_test_node_update($node) {
  194. _node_access_test_node_write($node);
  195. }
  196. /**
  197. * Helper for node insert/update.
  198. */
  199. function _node_access_test_node_write($node) {
  200. if (isset($node->private)) {
  201. db_merge('node_access_test')
  202. ->key(array('nid' => $node->nid))
  203. ->fields(array('private' => (int) $node->private))
  204. ->execute();
  205. }
  206. }