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