devel_node_access.module

You are here

  1. 7 devel_node_access.module
  2. 5 devel_node_access.module
  3. 6 devel_node_access.module
  4. 8 devel_node_access/devel_node_access.module

This module gives developers feedback as to what their node_access table contains, and which nodes are protected or visible to the public.

Functions

Constants

Namesort descending Description
DNA_ACCESS_VIEW

File

devel_node_access.module
View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This module gives developers feedback as to what their
  6. * node_access table contains, and which nodes are protected or
  7. * visible to the public.
  8. *
  9. */
  10. define('DNA_ACCESS_VIEW', 'view devel_node_access information');
  11. function devel_node_access_perm() {
  12. return array('view devel_node_access information');
  13. }
  14. /**
  15. * Implementation of hook_help().
  16. */
  17. function devel_node_access_help($path, $arg) {
  18. switch ($path) {
  19. case 'admin/settings/modules#description':
  20. return t('Development helper for node_access table');
  21. break;
  22. case 'admin/help#devel_node_access':
  23. $output = '<p>'. t('This module helps in site development. Specifically, when an access control module is used to limit access to some or all nodes, this module provides some feedback showing the node_access table in the database.') ."</p>\n";
  24. $output .= '<p>'. t('The node_access table is one method Drupal provides to hide content from some users while displaying it to others. By default, Drupal shows all nodes to all users. There are a number of optional modules which may be installed to hide content from some users.') ."</p>\n";
  25. $output .= '<p>'. t('If you have not installed any of these modules, you really have no need for the devel_node_access module. This module is intended for use during development, so that developers and admins can confirm that the node_access table is working as expected. You probably do not want this module enabled on a production site.') ."</p>\n";
  26. $output .= '<p>'. t('This module provides two blocks. One called Devel Node Access by User is visible when a single node is shown on a page. This block shows which users can view, update or delete the node shown. Note that this block uses an inefficient algorithm to produce its output. You should only enable this block on sites with very few user accounts.') ."</p>\n";
  27. $output .= '<p>'. t('The second block provided by this module shows the entries in the node_access table for any nodes shown on the current page. You can enable the debug mode on the <a href="@settings_page">settings page</a> to display much more information, but this can cause considerable overhead. Because the tables shown are wide, it is recommended to enable the blocks in the page footer rather than a sidebar.',
  28. array('@settings_page' => url('admin/settings/devel', array('fragment' => 'edit-devel-node-access-debug-mode')))
  29. ) ."</p>\n";
  30. $output .= '<p>'. t('This module also provides a <a href="@summary_page">summary page</a> which shows general information about your node_access table. If you have installed the Views module, you may browse node_access by realm.',
  31. array('@summary_page' => url('devel/node_access/summary'))
  32. ) ."</p>\n";
  33. return $output;
  34. }
  35. }
  36. function devel_node_access_menu() {
  37. $items = array();
  38. // add this to the custom menu 'devel' created by devel module.
  39. $items['devel/node_access/summary'] = array(
  40. 'title' => 'Node_access summary',
  41. 'page callback' => 'dna_summary',
  42. 'access arguments' => array(DNA_ACCESS_VIEW),
  43. 'menu_name' => 'devel',
  44. );
  45. if (!module_exists('devel')) {
  46. $items['admin/settings/devel'] = array(
  47. 'title' => 'Devel node access',
  48. 'description' => 'Helper pages and blocks to assist Drupal developers and admins with node_access. The devel blocks can be managed via the Blocks (admin/build/block) page.',
  49. 'page callback' => 'drupal_get_form',
  50. 'page arguments' => array('devel_node_access_admin_settings'),
  51. 'access arguments' => array('administer site configuration'),
  52. 'type' => MENU_NORMAL_ITEM
  53. );
  54. }
  55. return $items;
  56. }
  57. function devel_node_access_admin_settings() {
  58. $form = array();
  59. return system_settings_form($form);
  60. }
  61. function devel_node_access_form_alter(&$form, $form_state, $form_id) {
  62. $tr = 't';
  63. if ($form_id == 'devel_admin_settings' || $form_id == 'devel_node_access_admin_settings') {
  64. $form['devel_node_access_debug_mode'] = array(
  65. '#type' => 'checkbox',
  66. '#title' => t('Devel Node Access debug mode'),
  67. '#default_value' => variable_get('devel_node_access_debug_mode', FALSE),
  68. '#description' => t('Debug mode verifies the grant records in the node_access table against those that would be set by running !Rebuild_permissions, and displays them all; this can cause considerable overhead.<br />For even more information enable the <a href="@link">%DNAbU block</a>, too.', array(
  69. '!Rebuild_permissions' => l('[' . $tr('Rebuild permissions') . ']', 'admin/content/node-settings'),
  70. '%DNAbU' => t('Devel Node Access by User'),
  71. '@link' => url('admin/build/block/list'),
  72. )),
  73. );
  74. // push these down:
  75. $form['devel_error_handler']['#weight'] = 1;
  76. $form['smtp_library']['#weight'] = 1;
  77. $form['buttons']['#weight'] = 2;
  78. }
  79. }
  80. function dna_summary() {
  81. // Warn user if they have any entries that could grant access to all nodes
  82. $output = '';
  83. $result = db_query('SELECT DISTINCT realm FROM {node_access} WHERE nid=0 AND gid=0');
  84. $rows = array();
  85. while ($row = db_fetch_object($result)) {
  86. $rows[] = array($row->realm);
  87. }
  88. if (!empty($rows)) {
  89. $output .= '<h3>'. t('Access Granted to All Nodes (All Users)') ."</h3>\n";
  90. $output .= '<p>'. t('Your node_access table contains entries that may be granting all users access to all nodes. Depending on which access control module(s) you use, you may want to delete these entries. If you are not using an access control module, you should probably leave these entries as is.') ."</p>\n";
  91. $headers = array(t('realm'));
  92. $output .= theme('table', $headers, $rows);
  93. $access_granted_to_all_nodes = TRUE;
  94. }
  95. // how many nodes are not represented in the node_access table
  96. $result = db_fetch_object(db_query('SELECT COUNT(n.nid) AS num_nodes FROM {node} n LEFT JOIN {node_access} na ON n.nid = na.nid WHERE na.nid IS NULL'));
  97. if ($num = $result->num_nodes) {
  98. $output .= '<h3>'. t('Legacy Nodes') ."</h3>\n";
  99. $output .= '<p>'.
  100. t('You have !num nodes in your node table which are not represented in your node_access table. If you have an access control module installed, these nodes may be hidden from all users. This could be caused by publishing nodes before enabling the access control module. If this is the case, manually updating each node should add it to the node_access table and fix the problem.', array('!num' => l($num, 'devel/node_access/view/NULL')))
  101. ."</p>\n";
  102. if (!empty($access_granted_to_all_nodes)) {
  103. $output .= '<p>'.
  104. t('This issue may be masked by the one above, so look into the former first.')
  105. ."</p>\n";
  106. }
  107. }
  108. else {
  109. $output .= '<h3>'. t('All Nodes Represented') ."</h3>\n";
  110. $output .= '<p>'. t('All nodes are represented in the node_access table.') ."</p>\n";
  111. }
  112. // a similar warning to the one above, but slightly more specific
  113. $result = db_query('SELECT DISTINCT realm FROM {node_access} WHERE nid = 0 AND gid <> 0');
  114. $rows = array();
  115. while ($row = db_fetch_object($result)) {
  116. $rows[] = array($row->realm);
  117. }
  118. if (!empty($rows)) {
  119. $output .= '<h3>'. t('Access Granted to All Nodes (Some Users)') ."</h3>\n";
  120. $output .= '<p>'. t('Your node_access table contains entries that may be granting some users access to all nodes. This may be perfectly normal, depending on which access control module(s) you use.') ."</p>\n";
  121. $headers = array(t('realm'));
  122. $output .= theme('table', $headers, $rows);
  123. }
  124. // find specific nodes which may be visible to all users
  125. $result = db_query('SELECT DISTINCT realm, COUNT(DISTINCT nid) as node_count FROM {node_access} WHERE gid = 0 AND nid > 0 GROUP BY realm');
  126. $rows = array();
  127. while ($row = db_fetch_object($result)) {
  128. $rows[] = array($row->realm,
  129. array('data' => $row->node_count,
  130. 'align' => 'center'));
  131. }
  132. if (!empty($rows)) {
  133. $output .= '<h3>'. t('Access Granted to Some Nodes') ."</h3>\n";
  134. $output .= '<p>'.
  135. t('The following realms appear to grant all users access to some specific nodes. This may be perfectly normal, if some of your content is available to the public.')
  136. ."</p>\n";
  137. $headers = array(t('realm'), t('public nodes'));
  138. $output .= theme('table', $headers, $rows, array(), t('Public Nodes'));
  139. }
  140. // find specific nodes protected by node_access table
  141. $result = db_query('SELECT DISTINCT realm, COUNT(DISTINCT nid) as node_count FROM {node_access} WHERE gid <> 0 AND nid > 0 GROUP BY realm');
  142. $rows = array();
  143. while ($row = db_fetch_object($result)) {
  144. // no Views yet:
  145. //$rows[] = array(l($row->realm, "devel/node_access/view/$row->realm"),
  146. $rows[] = array($row->realm,
  147. array('data' => $row->node_count,
  148. 'align' => 'center'));
  149. }
  150. if (!empty($rows)) {
  151. $output .= '<h3>'. t('Summary by Realm') ."</h3>\n";
  152. $output .= '<p>'. t('The following realms grant limited access to some specific nodes.') ."</p>\n";
  153. $headers = array(t('realm'), t('private nodes'));
  154. $output .= theme('table', $headers, $rows, array(), t('Protected Nodes'));
  155. }
  156. return $output;
  157. }
  158. function dna_visible_nodes($nid = NULL) {
  159. static $nids = array();
  160. if ($nid) {
  161. $nids[$nid] = $nid;
  162. }
  163. return $nids;
  164. }
  165. function devel_node_access_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  166. if ($op == 'view') {
  167. // remember this node, for display in our block
  168. dna_visible_nodes($node->nid);
  169. }
  170. }
  171. function _devel_node_access_module_invoke_all() { // array and scalar returns only!
  172. $args = func_get_args();
  173. $hook = array_shift($args);
  174. $return = array();
  175. foreach (module_implements($hook) as $module) {
  176. $function = $module .'_'. $hook;
  177. $result = call_user_func_array($function, $args);
  178. if (isset($result)) {
  179. if (is_array($result)) {
  180. foreach ($result as $key => $value) {
  181. // add name of module that returned the value:
  182. $result[$key]['#module'] = $module;
  183. }
  184. }
  185. else {
  186. // build array with result keyed by $module:
  187. $result = array($module => $result);
  188. }
  189. $return = array_merge($return, $result);
  190. }
  191. }
  192. return $return;
  193. }
  194. function devel_node_access_block($op = 'list', $delta = 0) {
  195. global $user;
  196. global $theme_key;
  197. static $block1_visible, $hint = '';
  198. if (!isset($block1_visible)) {
  199. $block1_visible = db_result(db_query("SELECT status FROM {blocks} WHERE module = 'devel_node_access' AND delta = '1' AND theme = '%s'", $theme_key));
  200. if (!$block1_visible) {
  201. $hint = t('For per-user access permissions enable the second DNA <a href="@link">block</a>.', array('@link' => url('admin/build/block')));
  202. }
  203. }
  204. switch ($op) {
  205. case 'list':
  206. $blocks[0]['info'] = t('Devel Node Access');
  207. $blocks[0]['status'] = 1;
  208. $blocks[0]['region'] = 'footer';
  209. $blocks[1]['info'] = t('Devel Node Access by User');
  210. $blocks[1]['status'] = 0;
  211. $blocks[1]['region'] = 'footer';
  212. return $blocks;
  213. case 'view':
  214. if (!user_access(DNA_ACCESS_VIEW)) {
  215. return;
  216. }
  217. switch ($delta) {
  218. case 0:
  219. if (!count(dna_visible_nodes())) {
  220. return;
  221. }
  222. // include rows where nid == 0
  223. $nids = array_merge(array(0 => 0), dna_visible_nodes());
  224. $result = db_query('SELECT na.*, n.title FROM {node_access} na LEFT JOIN {node} n ON n.nid = na.nid WHERE na.nid IN ('. db_placeholders($nids) .') ORDER BY na.nid, na.realm, na.gid', $nids);
  225. if (!variable_get('devel_node_access_debug_mode', FALSE)) {
  226. $headers = array(t('node'), t('realm'), t('gid'), t('view'), t('update'), t('delete'), t('explained'));
  227. $rows = array();
  228. while ($row = db_fetch_object($result)) {
  229. $explained = module_invoke_all('node_access_explain', $row);
  230. $rows[] = array('<a href="#node-'. $row->nid .'">'. _devel_node_access_get_node_title($row, TRUE) .'</a>',
  231. $row->realm,
  232. $row->gid,
  233. $row->grant_view,
  234. $row->grant_update,
  235. $row->grant_delete,
  236. implode('<br />', $explained));
  237. }
  238. $output = theme('table', $headers, $rows, array('style' => 'text-align: left'));
  239. $hint = t('To see more details enable <a href="@debug_mode">debug mode</a>.', array('@debug_mode' => url('admin/settings/devel', array('fragment' => 'edit-devel-node-access-debug-mode')))) .' '. $hint;
  240. }
  241. else {
  242. $tr = 't';
  243. $variables = array('!na' => '{node_access}');
  244. $states = array(
  245. 'default' => array(t('default'), 'ok', t('Default grant supplied by core in the absence of any other non-empty grants, in !na.', $variables)),
  246. 'ok' => array(t('ok'), 'ok', t('Highest priority grant, in !na.', $variables)),
  247. 'static' => array(t('static'), 'ok', t('Non-standard grant in !na.', $variables)),
  248. 'unexpected' => array(t('unexpected'), 'warning', t('The 0/0/all/... grant applies to all nodes and all users -- usually it should not be present if any node access module is active!')),
  249. 'ignored' => array(t('ignored'), 'warning', t('Lower priority grant, not in !na and thus ignored.', $variables)),
  250. 'empty' => array(t('empty'), 'warning', t('Does not grant any access, but could block lower priority grants; not in !na.', $variables)),
  251. 'missing' => array(t('missing'), 'error', t("Should be in !na but isn't!", $variables)),
  252. 'illegitimate' => array(t('illegitimate'), 'error', t('Should NOT be in !na because of lower priority!', $variables)),
  253. 'alien' => array(t('alien'), 'error', t('Should NOT be in !na because of unknown origin!', $variables)),
  254. );
  255. $active_states = array('default', 'ok', 'static', 'unexpected', 'illegitimate', 'alien');
  256. $headers = array(t('node'), t('prio'), t('status'), t('realm'), t('gid'), t('view'), t('update'), t('delete'), t('explained'));
  257. $active_grants = array();
  258. while ($active_grant = db_fetch_object($result)) {
  259. $active_grants[$active_grant->nid][$active_grant->realm][$active_grant->gid] = $active_grant;
  260. }
  261. $all_grants = $checked_grants = $checked_status = array();
  262. foreach ($nids as $nid) {
  263. $acquired_grants_nid = array();
  264. if ($node = node_load($nid)) {
  265. // check node_access_acquire_grants()
  266. $grants = _devel_node_access_module_invoke_all('node_access_records', $node);
  267. if (!empty($grants)) {
  268. $top_priority = NULL;
  269. foreach ($grants as $grant) {
  270. $priority = intval($grant['priority']);
  271. $top_priority = (isset($top_priority) ? max($top_priority, $priority) : $priority);
  272. $grant['priority'] = (isset($grant['priority']) ? $priority : '&ndash;&nbsp;');
  273. $acquired_grants_nid[$priority][$grant['realm']][$grant['gid']] = $grant + array(
  274. '#title' => _devel_node_access_get_node_title($node),
  275. '#module' => (isset($grant['#module']) ? $grant['#module'] : ''),
  276. );
  277. }
  278. krsort($acquired_grants_nid);
  279. }
  280. // check node_access_grants()
  281. $checked_status[$nid] = $node->status;
  282. if ($node->nid) {
  283. foreach (array('view', 'update', 'delete') as $op) {
  284. $checked_grants[$nid][$op] = array_merge(
  285. array('all' => array(0)),
  286. _devel_node_access_module_invoke_all('node_grants', $user, $op)
  287. );
  288. }
  289. }
  290. }
  291. // check for grants in the node_access table that aren't returned by node_access_acquire_grants()
  292. if (isset($active_grants[$nid])) {
  293. foreach ($active_grants[$nid] as $realm => $active_grants_realm) {
  294. foreach ($active_grants_realm as $gid => $active_grant) {
  295. $found = FALSE;
  296. $count_nonempty_grants = 0;
  297. foreach ($acquired_grants_nid as $priority => $acquired_grants_nid_priority) {
  298. if (isset($acquired_grants_nid_priority[$realm][$gid])) {
  299. $found = TRUE;
  300. }
  301. }
  302. if ($acquired_grants_nid_priority = reset($acquired_grants_nid)) { // highest priority only
  303. foreach ($acquired_grants_nid_priority as $acquired_grants_nid_priority_realm) {
  304. foreach ($acquired_grants_nid_priority_realm as $acquired_grants_nid_priority_realm_gid) {
  305. $count_nonempty_grants += (!empty($acquired_grants_nid_priority_realm_gid['grant_view']) || !empty($acquired_grants_nid_priority_realm_gid['grant_update']) || !empty($acquired_grants_nid_priority_realm_gid['grant_delete']));
  306. }
  307. }
  308. }
  309. $fixed_grant = (array) $active_grant;
  310. if ($count_nonempty_grants == 0 && $realm == 'all' && $gid == 0 ) {
  311. $fixed_grant += array(
  312. 'priority' => '&ndash;',
  313. 'state' => 'default',
  314. );
  315. }
  316. elseif (!$found) {
  317. $acknowledged = _devel_node_access_module_invoke_all('node_access_acknowledge', $fixed_grant);
  318. if (empty($acknowledged)) {
  319. // no one acknowledged this record, mark it as alien:
  320. $fixed_grant += array(
  321. 'priority' => '?',
  322. 'state' => 'alien',
  323. );
  324. }
  325. else {
  326. // at least one module acknowledged the record, attribute it to the first one:
  327. $fixed_grant += array(
  328. 'priority' => '&ndash;',
  329. 'state' => 'static',
  330. '#module' => reset(array_keys($acknowledged)),
  331. );
  332. }
  333. }
  334. else {
  335. continue;
  336. }
  337. $fixed_grant += array(
  338. 'nid' => $nid,
  339. '#title' => _devel_node_access_get_node_title($node),
  340. );
  341. $all_grants[] = $fixed_grant;
  342. }
  343. }
  344. }
  345. // order grants and evaluate their status
  346. foreach ($acquired_grants_nid as $priority => $acquired_grants_priority) {
  347. ksort($acquired_grants_priority);
  348. foreach ($acquired_grants_priority as $realm => $acquired_grants_realm) {
  349. ksort($acquired_grants_realm);
  350. foreach ($acquired_grants_realm as $gid => $acquired_grant) {
  351. if ($priority == $top_priority) {
  352. if (empty($acquired_grant['grant_view']) && empty($acquired_grant['grant_update']) && empty($acquired_grant['grant_delete'])) {
  353. $acquired_grant['state'] = 'empty';
  354. }
  355. else {
  356. $acquired_grant['state'] = (isset($active_grants[$nid][$realm][$gid]) ? 'ok' : 'missing');
  357. if ($acquired_grant['state'] == 'ok') {
  358. foreach (array('view', 'update', 'delete') as $op) {
  359. $active_grant = (array) $active_grants[$nid][$realm][$gid];
  360. if (empty($acquired_grant["grant_$op"]) != empty($active_grant["grant_$op"]) ) {
  361. $acquired_grant["grant_$op!"] = $active_grant["grant_$op"];
  362. }
  363. }
  364. }
  365. }
  366. }
  367. else {
  368. $acquired_grant['state'] = (isset($active_grants[$nid][$realm][$gid]) ? 'illegitimate' : 'ignored');
  369. }
  370. $all_grants[] = $acquired_grant + array('nid' => $nid);
  371. }
  372. }
  373. }
  374. }
  375. // fill in the table rows
  376. $rows = array();
  377. $error_count = 0;
  378. foreach ($all_grants as $grant) {
  379. $row = new stdClass();
  380. $row->nid = $grant['nid'];
  381. $row->title = $grant['#title'];
  382. $row->priority = $grant['priority'];
  383. $row->state = array('data' => $states[$grant['state']][0], 'title' => $states[$grant['state']][2]);
  384. $row->realm = $grant['realm'];
  385. $row->gid = $grant['gid'];
  386. $row->grant_view = $grant['grant_view'];
  387. $row->grant_update = $grant['grant_update'];
  388. $row->grant_delete = $grant['grant_delete'];
  389. $row->explained = implode('<br />', module_invoke_all('node_access_explain', $row));
  390. unset($row->title); // possibly needed above
  391. if ($row->nid == 0 && $row->gid == 0 && $row->realm == 'all' && count($all_grants) > 1) {
  392. $row->state = array('data' => $states['unexpected'][0], 'title' => $states['unexpected'][2]);
  393. $class = $states['unexpected'][1];
  394. }
  395. else {
  396. $class = $states[$grant['state']][1];
  397. }
  398. $error_count += ($class == 'error');
  399. $row = (array) $row;
  400. foreach (array('view', 'update', 'delete') as $op) {
  401. $row["grant_$op"] = array('data' => $row["grant_$op"]);
  402. if ((isset($checked_grants[$grant['nid']][$op][$grant['realm']]) && in_array($grant['gid'], $checked_grants[$grant['nid']][$op][$grant['realm']]) || ($row['nid'] == 0 && $row['gid'] == 0 && $row['realm'] == 'all')) && !empty($row["grant_$op"]['data']) && in_array($grant['state'], $active_states)) {
  403. $row["grant_$op"]['data'] .= '&prime;';
  404. $row["grant_$op"]['title'] = t('This entry grants access to this node to this user.');
  405. }
  406. if (isset($grant["grant_$op!"])) {
  407. $row["grant_$op"]['data'] = $grant["grant_$op!"] .'&gt;'. (!$row["grant_$op"]['data'] ? 0 : $row["grant_$op"]['data']);
  408. $row["grant_$op"]['class'] = 'error';
  409. }
  410. }
  411. $row['nid'] = '<a href="#node-'. $grant['nid'] .'">'. $row['nid'] .'</a>';
  412. foreach (array('nid', 'priority', 'gid') as $key) {
  413. $row[$key] = array('data' => $row[$key], 'style' => 'text-align: right');
  414. }
  415. $row['nid']['title'] = $grant['#title'];
  416. $row['realm'] = (empty($grant['#module']) || strpos($grant['realm'], $grant['#module']) === 0 ? '' : $grant['#module'] .':<br />') . $grant['realm'];
  417. $rows[] = array('data' => array_values($row), 'class' => 'even '. $class);
  418. }
  419. $output = theme('table', $headers, $rows, array('class' => 'system-status-report', 'style' => 'text-align: left'));
  420. $output .= theme('item', array('#value' => '', '#description' => '(Some of the table elements provide additional information if you hover your mouse over them.)'));
  421. if ($error_count > 0) {
  422. $variables['!Rebuild_permissions'] = '<a href="'. url('admin/content/node-settings/rebuild') .'">'. $tr('Rebuild permissions') .'</a>';
  423. $output .= theme('item', array('#value' => '<div class="error">'. t("You have errors in your !na table! You may be able to fix these for now by running !Rebuild_permissions, but this is likely to destroy the evidence and make it impossible to identify the underlying issues. If you don't fix those, the errors will probably come back again. <br /> DON'T do this just yet if you intend to ask for help with this situation.", $variables) .'</div>'));
  424. }
  425. // Explain whether access is granted or denied, and why (using code from node_access()).
  426. $tr = 't';
  427. array_shift($nids); // remove the 0
  428. $accounts = array();
  429. if (!isset($user->name)) {
  430. $user->name = '';
  431. }
  432. $variables += array(
  433. '!username' => theme('username', $user),
  434. '%uid' => $user->uid,
  435. );
  436. if (user_access('administer nodes')) {
  437. $variables['%administer_nodes'] = $tr('administer nodes');
  438. $output .= t('!username has the %administer_nodes permission and thus full access to all nodes.', $variables) .'<br />&nbsp;';
  439. }
  440. else {
  441. $variables['!list'] = '<div style="margin-left: 2em">'. _devel_node_access_get_grant_list($nid, $checked_status, $checked_grants) .'</div>';
  442. $variables['%access'] = 'view';
  443. $output .= "\n<div style='text-align: left' title='". t('These are the grants returned by hook_node_grants() for this user.') ."'>". t('!username (user %uid) can use these grants for %access access (if they are present above): !list', $variables) ."</div>\n";
  444. $accounts[] = $user;
  445. }
  446. if (arg(0) == 'node' && is_numeric(arg(1)) && !$block1_visible) { // only for single nodes
  447. if (user_is_logged_in()) {
  448. $accounts[] = user_load(0); // Anonymous, too
  449. }
  450. foreach ($accounts as $account) {
  451. $variables['!username'] = theme('username', $account);
  452. $output .= "\n<div style='text-align: left'>". t("!username has the following access", $variables) .' ';
  453. $nid_items = array();
  454. foreach ($nids as $nid) {
  455. $op_items = array();
  456. foreach (array('create', 'view', 'update', 'delete') as $op) {
  457. $explain = _devel_node_access_explain_access($op, $nid, $account);
  458. $op_items[] = "<div style='width: 5em; display: inline-block'>". t('%op:', array('%op' => $op)) .' </div>'. $explain[2];
  459. }
  460. $nid_items[] = t('to node !nid:', array('!nid' => l($nid, 'node/'. $nid)))
  461. ."\n<div style='margin-left: 2em'>". theme('item_list', $op_items, NULL, 'ul') .'</div>';
  462. }
  463. if (count($nid_items) == 1) {
  464. $output .= $nid_items[0];
  465. }
  466. else {
  467. $output .= "\n<div style='margin-left: 2em'>". theme('item_list', $nid_items, NULL, 'ul') .'</div>';
  468. }
  469. $output .= "\n</div>\n";
  470. }
  471. }
  472. }
  473. if (!empty($hint)) {
  474. $output .= theme('item', array('#value' => '', '#description' => '('. $hint .')'));
  475. }
  476. $subject = t('node_access entries for nodes shown on this page');
  477. return array('subject' => $subject, 'content' => $output .'<br /><br />');
  478. case 1:
  479. // show which users can access this node
  480. if (arg(0) == 'node' && is_numeric($nid = arg(1)) && arg(2) == null && $node = node_load($nid)) {
  481. $headers = array(t('username'), '<span title="'. t("Create nodes of the '@Node_type' type.", array('@Node_type' => node_get_types('name', $node))) .'">'. t('create') .'</span>', t('view'), t('update'), t('delete'));
  482. $rows = array();
  483. // Find all users. The following operations are very inefficient, so we
  484. // limit the number of users returned. It would be better to make a
  485. // pager query, or at least make the number of users configurable. If
  486. // anyone is up for that please submit a patch.
  487. $result = db_query_range('SELECT DISTINCT u.uid, u.*, (u.uid <> 0) AS authorized FROM {users} u ORDER BY authorized ASC, u.access DESC', 0, 10);
  488. while ($data = db_fetch_object($result)) {
  489. $account = user_load($data->uid);
  490. $username = theme('username', $data);
  491. if ($account->uid == $user->uid) {
  492. $username = '<strong>'. $username .'</strong>';
  493. }
  494. $rows[] = array($username,
  495. theme('dna_permission', _devel_node_access_explain_access('create', $nid, $account)),
  496. theme('dna_permission', _devel_node_access_explain_access('view', $nid, $account)),
  497. theme('dna_permission', _devel_node_access_explain_access('update', $nid, $account)),
  498. theme('dna_permission', _devel_node_access_explain_access('delete', $nid, $account)),
  499. );
  500. }
  501. if (count($rows)) {
  502. $output = theme('table', $headers, $rows, array('style' => 'text-align: left'));
  503. $output .= theme('item', array('#value' => '', '#description' => t('(This table lists the most-recently active users. Hover your mouse over each result for more details.)')));
  504. return array('subject' => t('Access permissions by user'),
  505. 'content' => $output);
  506. }
  507. }
  508. break;
  509. }
  510. break;
  511. }
  512. }
  513. /**
  514. * Helper function that mimicks node.module's node_access() function.
  515. *
  516. * Unfortunately, this needs to be updated manually whenever node.module changes!
  517. *
  518. * @return
  519. * An array suitable for theming with theme_dna_permission().
  520. */
  521. function _devel_node_access_explain_access($op, $node, $account = NULL) {
  522. global $user;
  523. static $filter_formats;
  524. if (is_numeric($node) && !($node = node_load($node))) {
  525. return array( FALSE, '???',
  526. t('Unable to load the node &ndash; this should never happen!'),
  527. );
  528. }
  529. if ($op == 'create' && is_object($node)) {
  530. $node = $node->type;
  531. }
  532. if (!empty($account)) {
  533. $filter_formats = filter_formats(); // use real current user first!
  534. // To try to get the most authentic result we impersonate the given user!
  535. // This may reveal bugs in other modules, leading to contradictory results.
  536. $saved_user = $user;
  537. session_save_session(FALSE);
  538. $user = $account;
  539. $result = _devel_node_access_explain_access($op, $node, NULL);
  540. $user = $saved_user;
  541. session_save_session(TRUE);
  542. $second_opinion = node_access($op, $node, $account);
  543. if ($second_opinion != $result[0]) {
  544. $result[1] .= '<span class="'. ($second_opinion ? 'ok' : 'error') .'" title="Core seems to disagree on this item. This is a bug in either DNA or Core and should be fixed! Try to look at this node as this user and check whether there is still disagreement.">*</span>';
  545. }
  546. return $result;
  547. }
  548. $variables = array(
  549. '!NO' => t('NO'),
  550. '!YES' => t('YES'),
  551. );
  552. if ($op == 'update' && !_devel_node_access_filter_access($node->format)) {
  553. return array( FALSE,
  554. t('!NO: input format', $variables),
  555. t("!NO: This user is not allowed to use the input format '!format' (!fid).", $variables += array(
  556. '!fid' => $node->format,
  557. '!format' => (isset($filter_formats[$node->format]) ? $filter_formats[$node->format]->name : '***'),
  558. )),
  559. );
  560. }
  561. if (user_access('administer nodes')) {
  562. return array( TRUE,
  563. t('!YES: administer nodes', $variables),
  564. t("!YES: This user has the '!administer_nodes' permission and may do everything with nodes.", $variables += array(
  565. '!administer_nodes' => t('administer nodes'),
  566. )),
  567. );
  568. }
  569. elseif (!user_access('access content')) {
  570. return array( FALSE,
  571. t('!NO: access content', $variables),
  572. t("!NO: This user does not have the '!access_content' permission and is denied doing anything with content.", $variables += array(
  573. '!access_content' => t('access content'),
  574. )),
  575. );
  576. }
  577. $module = node_get_types('module', $node);
  578. $access = module_invoke(($module == 'node' ? 'node_content' : $module), 'access', $op, $node, $user);
  579. if (!is_null($access)) {
  580. $variables += array(
  581. '@module' => $module,
  582. '@content_type' => (is_object($node) ? $node->type : $node),
  583. );
  584. if ($access) {
  585. return array( TRUE,
  586. t('!YES: by the module', $variables),
  587. t("!YES: The '@module' module (which defines the '@content_type' content type) allows this, probably based on some permission.", $variables),
  588. );
  589. }
  590. else {
  591. return array( FALSE,
  592. t('!NO: by the module', $variables),
  593. t("!NO: The '@module' module (which defines the '@content_type' content type) denies this.", $variables),
  594. );
  595. }
  596. }
  597. if ($op != 'create' && $node->nid && $node->status) {
  598. if (node_access($op, $node, $user)) { // delegate this part
  599. $variables['@node_access_table'] = '{node_access}';
  600. return array( TRUE,
  601. t('!YES: @node_access_table', $variables),
  602. t('!YES: Node access allows this based on one or more records in the @node_access_table table (see the other DNA block!).', $variables),
  603. );
  604. }
  605. else {
  606. return array( FALSE,
  607. t('!NO: node access', $variables),
  608. t('!NO: Node access denies this.', $variables),
  609. );
  610. }
  611. }
  612. if ($op == 'view' && $user->uid == $node->uid && $user->uid != 0) {
  613. return array( TRUE,
  614. t('!YES: own node', $variables),
  615. t('!YES: The user may view his/her own node.', $variables),
  616. );
  617. }
  618. return array( FALSE,
  619. t('!NO: no reason', $variables),
  620. t("!NO: None of the checks resulted in allowing this, so it's denied.", $variables) .
  621. ($op != 'create' && !$node->status ? ' '. t('Node access was not checked because the node is not published.') : '') .
  622. ($op == 'create' ? ' '. t('This is most likely due to a withheld permission.') : ''),
  623. );
  624. }
  625. /*
  626. * Helper function that mimicks filter.modules' filter_access(), but with the
  627. * help of the user-aware _devel_node_access_filter_formats() function.
  628. */
  629. function _devel_node_access_filter_access($format) {
  630. $format = filter_resolve_format($format);
  631. if (user_access('administer filters') || ($format == variable_get('filter_default_format', 1))) {
  632. return TRUE;
  633. }
  634. else {
  635. return (bool) _devel_node_access_filter_formats($format);
  636. }
  637. }
  638. /*
  639. * Helper function that mimicks filter.module's filter_formats(), but for a
  640. * specific user. If #470840 gets committed, we can remove this here.
  641. */
  642. function _devel_node_access_filter_formats($index, $account = NULL) {
  643. global $user;
  644. static $formats = array();
  645. if (!isset($account)) {
  646. $account = $user;
  647. }
  648. // Administrators can always use all text formats.
  649. $all = user_access('administer filters', $account);
  650. if (!isset($formats[$account->uid])) {
  651. $formats[$account->uid] = array();
  652. $query = 'SELECT * FROM {filter_formats}';
  653. // Build query for selecting the format(s) based on the user's roles.
  654. $args = array();
  655. if (!$all) {
  656. $where = array();
  657. foreach ($account->roles as $rid => $role) {
  658. $where[] = "roles LIKE '%%,%d,%%'";
  659. $args[] = $rid;
  660. }
  661. $query .= ' WHERE '. implode(' OR ', $where) .' OR format = %d';
  662. $args[] = variable_get('filter_default_format', 1);
  663. }
  664. $result = db_query($query, $args);
  665. while ($format = db_fetch_object($result)) {
  666. $formats[$account->uid][$format->format] = $format;
  667. }
  668. }
  669. if (isset($index)) {
  670. return isset($formats[$account->uid][$index]) ? $formats[$account->uid][$index] : FALSE;
  671. }
  672. return $formats[$account->uid];
  673. }
  674. /**
  675. * Helper function to create a list of the grants returned by hook_node_grants().
  676. */
  677. function _devel_node_access_get_grant_list($nid, $checked_status, $checked_grants) {
  678. if (!empty($checked_status[$nid])) {
  679. $cgs_by_realm = array();
  680. foreach ($checked_grants[$nid]['view'] as $realm => $cg) {
  681. if (isset($cg['#module'])) {
  682. $module = $cg['#module'];
  683. unset($cg['#module']);
  684. if (!empty($module) && (strpos($realm, $module) !== 0)) {
  685. $realm = $module .':'. $realm;
  686. }
  687. }
  688. $cgs_by_realm[$realm] = $realm .': '. implode(', ', $cg);
  689. }
  690. if (!empty($cgs_by_realm)) {
  691. return theme('item_list', array_values($cgs_by_realm), NULL, 'ul');
  692. }
  693. }
  694. }
  695. /**
  696. * Implementation of hook_node_access_explain().
  697. *
  698. * Provide a (short!) textual explanation of this module's grant records.
  699. *
  700. * @param $row
  701. * Array with a row from the {node_access} table.
  702. * The keys are: nid, gid, realm, grant_view, grant_update, grant_delete.
  703. *
  704. * @return
  705. * A (short!) HTML explanation of the given {node_access} row, to be
  706. * displayed in DNA's 'Devel Node Access' block.
  707. */
  708. function devel_node_access_node_access_explain($row) {
  709. if ($row->gid == 0 && $row->realm == 'all') {
  710. foreach (array('view', 'update', 'delete') as $op) {
  711. $gop = 'grant_'. $op;
  712. if (!empty($row->$gop)) {
  713. $ops[] = $op;
  714. }
  715. }
  716. if (empty($ops)) {
  717. return '(No access granted to '. ($row->nid == 0 ? 'any nodes.)' : 'this node.)');
  718. }
  719. else {
  720. return 'All users may '. implode('/', $ops) . ($row->nid == 0 ? ' all nodes.' : ' this node.');
  721. }
  722. }
  723. }
  724. /**
  725. * Helper function to return a sanitized node title.
  726. */
  727. function _devel_node_access_get_node_title($node, $clip_and_decorate = FALSE) {
  728. if (isset($node)) {
  729. if (isset($node->title)) {
  730. $node_title = check_plain($node->title);
  731. if ($clip_and_decorate) {
  732. if (drupal_strlen($node_title) > 20) {
  733. $node_title = "<span title='node/$node->nid: $node_title'>". drupal_substr($node_title, 0, 15) .'...</span>';
  734. }
  735. $node_title = '<span title="node/'. $node->nid .'">'. $node_title .'</span>';
  736. }
  737. return $node_title;
  738. }
  739. elseif (isset($node->nid)) {
  740. return $node->nid;
  741. }
  742. }
  743. return '&mdash;';
  744. }
  745. /**
  746. * Implementation of hook_theme().
  747. */
  748. function devel_node_access_theme() {
  749. return array(
  750. 'dna_permission' => array(
  751. 'arguments' => array('permission' => NULL),
  752. ),
  753. );
  754. }
  755. /**
  756. * Indicate whether user has a permission or not.
  757. */
  758. function theme_dna_permission($permission) {
  759. return '<span class="'. ($permission[0] ? 'ok' : 'error') .'" title="'. $permission[2] .'">'. $permission[1] .'</span>';
  760. }