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

Namesort descending Description
devel_node_access_admin_settings
devel_node_access_block_info Implements hook_block_info().
devel_node_access_block_view Implements hook_block_view().
devel_node_access_form_alter
devel_node_access_help Implements hook_help().
devel_node_access_menu Implements hook_menu().
devel_node_access_node_access_explain Implements hook_node_access_explain().
devel_node_access_node_view
devel_node_access_permission
devel_node_access_theme Implements hook_theme().
devel_node_access_user_ajax Callback function for node access by user block ajax.
dna_summary
dna_visible_nodes
theme_dna_permission Indicate whether user has a permission or not.
_devel_node_access_build_nar_data Helper function to build an associative array of grant records and their history. If there are duplicate records, display an error message.
_devel_node_access_explain_access Helper function that mimicks node.module's node_access() function.
_devel_node_access_format_row Helper function to apply common formatting to a debug-mode table row.
_devel_node_access_get_grant_list Helper function to create a list of the grants returned by hook_node_grants().
_devel_node_access_get_node_title Helper function to return a sanitized node title.
_devel_node_access_module_invoke_all
_devel_node_access_nar_alter Helper function to mimic hook_node_access_records_alter() and trace what each module does with it.
_devel_node_access_ng_alter Helper function to mimic hook_node_grants_alter() and trace what each module does with it.

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_permission() {
  12. return array(
  13. 'view devel_node_access information' => array(
  14. 'description' => t('View the node access information blocks on node pages and the summary page.'),
  15. 'title' => t('Access DNA information'),
  16. 'restrict access' => TRUE,
  17. ),
  18. );
  19. }
  20. /**
  21. * Implements hook_help().
  22. */
  23. function devel_node_access_help($path, $arg) {
  24. switch ($path) {
  25. case 'admin/settings/modules#description':
  26. return t('Development helper for node_access table');
  27. break;
  28. case 'admin/help#devel_node_access':
  29. $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";
  30. $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";
  31. $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";
  32. $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";
  33. $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.',
  34. array('@settings_page' => url('admin/config/development/devel', array('fragment' => 'edit-devel-node-access')))
  35. ) . "</p>\n";
  36. $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.',
  37. array('@summary_page' => url('devel/node_access/summary'))
  38. ) . "</p>\n";
  39. return $output;
  40. }
  41. }
  42. /**
  43. * Implements hook_menu().
  44. */
  45. function devel_node_access_menu() {
  46. $items = array();
  47. if (!module_exists('devel')) {
  48. if (!menu_load('devel')) {
  49. // we have to create the 'devel' menu ourselves
  50. $menu = array(
  51. 'menu_name' => 'devel',
  52. 'title' => 'Development',
  53. 'description' => 'Development link',
  54. );
  55. menu_save($menu);
  56. }
  57. // we have to create the 'Devel settings' menu item ourselves
  58. $items['admin/config/development/devel'] = array(
  59. 'title' => 'Devel settings',
  60. 'description' => 'Helper pages and blocks to assist Drupal developers and admins with node_access. The devel blocks can be managed via the <a href="' . url('admin/structure/block') . '">block administration</a> page.',
  61. 'page callback' => 'drupal_get_form',
  62. 'page arguments' => array('devel_node_access_admin_settings'),
  63. 'access arguments' => array('administer site configuration'),
  64. );
  65. $items['devel/settings'] = $items['admin/config/development/devel'] + array(
  66. 'menu_name' => 'devel',
  67. );
  68. }
  69. // create a callback for use by devel_node_access_user_ajax().
  70. $items['devel/node_access/by_user/%/%'] = array(
  71. 'page callback' => 'devel_node_access_user_ajax',
  72. 'page arguments' => array(3, 4),
  73. 'access arguments' => array(DNA_ACCESS_VIEW),
  74. 'type' => MENU_CALLBACK,
  75. );
  76. // add this to the custom menu 'devel' created by the devel module.
  77. $items['devel/node_access/summary'] = array(
  78. 'title' => 'Node_access summary',
  79. 'page callback' => 'dna_summary',
  80. 'access arguments' => array(DNA_ACCESS_VIEW),
  81. 'menu_name' => 'devel',
  82. );
  83. return $items;
  84. }
  85. function devel_node_access_admin_settings() {
  86. $form = array();
  87. return system_settings_form($form);
  88. }
  89. function devel_node_access_form_alter(&$form, $form_state, $form_id) {
  90. $tr = 't';
  91. if ($form_id == 'devel_admin_settings' || $form_id == 'devel_node_access_admin_settings') {
  92. $form['devel_node_access'] = array(
  93. '#type' => 'fieldset',
  94. '#title' => t('Devel Node Access'),
  95. '#collapsible' => TRUE,
  96. );
  97. $form['devel_node_access']['devel_node_access_debug_mode'] = array(
  98. '#type' => 'checkbox',
  99. '#title' => t('Debug mode'),
  100. '#default_value' => variable_get('devel_node_access_debug_mode', FALSE),
  101. '#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(
  102. '!Rebuild_permissions' => l('[' . $tr('Rebuild permissions') . ']', 'admin/reports/status/rebuild'),
  103. '%DNAbU' => t('Devel Node Access by User'),
  104. '@link' => url('admin/structure/block/list'),
  105. )),
  106. );
  107. $form['devel_node_access']['devel_node_access_user_ajax'] = array(
  108. '#type' => 'checkbox',
  109. '#title' => t('Asynchronously populate the <em>Devel Node Access by User</em> block'),
  110. '#default_value' => variable_get('devel_node_access_user_ajax', FALSE),
  111. '#description' => t("Use Ajax to populate the second DNA block. This loads the initial page faster and uses dynamic calls to build the data in the table, one request at a time. It's especially useful during development, if some of the calls are failing.") . '<br />' . t('JavaScript must be enabled in your browser.'),
  112. );
  113. // push the Save button down
  114. $form['actions']['#weight'] = 10;
  115. }
  116. }
  117. function dna_summary() {
  118. // warn user if they have any entries that could grant access to all nodes
  119. $output = '';
  120. $result = db_query('SELECT DISTINCT realm FROM {node_access} WHERE nid = 0 AND gid = 0');
  121. $rows = array();
  122. foreach ($result as $row) {
  123. $rows[] = array($row->realm);
  124. }
  125. if (!empty($rows)) {
  126. $output .= '<h3>' . t('Access Granted to All Nodes (All Users)') . "</h3>\n";
  127. $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";
  128. $headers = array(t('realm'));
  129. $output .= theme('table', array('header' => $headers, 'rows' => $rows));
  130. $access_granted_to_all_nodes = TRUE;
  131. }
  132. // how many nodes are not represented in the node_access table
  133. $num = 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')->fetchField();
  134. if ($num) {
  135. $output .= '<h3>' . t('Legacy Nodes') . "</h3>\n";
  136. $output .= '<p>' .
  137. 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')))
  138. . "</p>\n";
  139. if (!empty($access_granted_to_all_nodes)) {
  140. $output .= '<p>' .
  141. t('This issue may be masked by the one above, so look into the former first.')
  142. . "</p>\n";
  143. }
  144. }
  145. else {
  146. $output .= '<h3>' . t('All Nodes Represented') . "</h3>\n";
  147. $output .= '<p>' . t('All nodes are represented in the node_access table.') . "</p>\n";
  148. }
  149. // a similar warning to the one above, but slightly more specific
  150. $result = db_query('SELECT DISTINCT realm FROM {node_access} WHERE nid = 0 AND gid <> 0');
  151. $rows = array();
  152. foreach ($result as $row) {
  153. $rows[] = array($row->realm);
  154. }
  155. if (!empty($rows)) {
  156. $output .= '<h3>' . t('Access Granted to All Nodes (Some Users)') . "</h3>\n";
  157. $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";
  158. $headers = array(t('realm'));
  159. $output .= theme('table', array('header' => $headers, 'rows' => $rows));
  160. }
  161. // find specific nodes which may be visible to all users
  162. $result = db_query('SELECT DISTINCT realm, COUNT(DISTINCT nid) as node_count FROM {node_access} WHERE gid = 0 AND nid > 0 GROUP BY realm');
  163. $rows = array();
  164. foreach ($result as $row) {
  165. $rows[] = array(
  166. $row->realm,
  167. array(
  168. 'data' => $row->node_count,
  169. 'align' => 'center',
  170. ),
  171. );
  172. }
  173. if (!empty($rows)) {
  174. $output .= '<h3>' . t('Access Granted to Some Nodes') . "</h3>\n";
  175. $output .= '<p>' .
  176. 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.')
  177. . "</p>\n";
  178. $headers = array(t('realm'), t('public nodes'));
  179. $output .= theme('table', array('header' => $headers, 'rows' => $rows, 'caption' => t('Public Nodes')));
  180. }
  181. // find specific nodes protected by node_access table
  182. $result = db_query('SELECT DISTINCT realm, COUNT(DISTINCT nid) as node_count FROM {node_access} WHERE gid <> 0 AND nid > 0 GROUP BY realm');
  183. $rows = array();
  184. foreach ($result as $row) {
  185. // no Views yet:
  186. //$rows[] = array(l($row->realm, "devel/node_access/view/$row->realm"),
  187. $rows[] = array(
  188. $row->realm,
  189. array(
  190. 'data' => $row->node_count,
  191. 'align' => 'center',
  192. ),
  193. );
  194. }
  195. if (!empty($rows)) {
  196. $output .= '<h3>' . t('Summary by Realm') . "</h3>\n";
  197. $output .= '<p>' . t('The following realms grant limited access to some specific nodes.') . "</p>\n";
  198. $headers = array(t('realm'), t('private nodes'));
  199. $output .= theme('table', array('header' => $headers, 'rows' => $rows, 'caption' => t('Protected Nodes')));
  200. }
  201. return $output;
  202. }
  203. function dna_visible_nodes($nid = NULL) {
  204. static $nids = array();
  205. if ($nid) {
  206. $nids[$nid] = $nid;
  207. }
  208. elseif (empty($nids)) {
  209. $menu_item = menu_get_item();
  210. $map = $menu_item['original_map'];
  211. if ($map[0] == 'node' && isset($map[1]) && is_numeric($map[1]) && !isset($map[2])) {
  212. // show DNA information on node/NID even if access is denied (IF the user has the 'view devel_node_access information' permission)!
  213. return array($map[1]);
  214. }
  215. }
  216. return $nids;
  217. }
  218. function devel_node_access_node_view($node, $build_mode) {
  219. // remember this node, for display in our block
  220. dna_visible_nodes($node->nid);
  221. }
  222. function _devel_node_access_module_invoke_all() { // array and scalar returns
  223. $args = func_get_args();
  224. $hook = $args[0];
  225. unset($args[0]);
  226. $return = array();
  227. foreach (module_implements($hook) as $module) {
  228. $function = $module . '_' . $hook;
  229. if (function_exists($function)) {
  230. $result = call_user_func_array($function, $args);
  231. if (isset($result)) {
  232. if (is_array($result)) {
  233. foreach ($result as $key => $value) {
  234. // add name of module that returned the value:
  235. $result[$key]['#module'] = $module;
  236. }
  237. }
  238. else {
  239. // build array with result keyed by $module:
  240. $result = array($module => $result);
  241. }
  242. $return = array_merge_recursive($return, $result);
  243. }
  244. }
  245. }
  246. return $return;
  247. }
  248. /**
  249. * Helper function to build an associative array of grant records and their
  250. * history. If there are duplicate records, display an error message.
  251. *
  252. * @param $grants
  253. * An indexed array of grant records, augmented by the '#module' key,
  254. * as created by _devel_node_access_module_invoke_all('node_access_records').
  255. *
  256. * @param $node
  257. * The node that the grant records belong to.
  258. *
  259. * @param $function
  260. * The name of the hook that produced the grants array, in case we need to
  261. * display an error message.
  262. *
  263. * @return
  264. * See _devel_node_access_nar_alter() for the description of the result.
  265. */
  266. function _devel_node_access_build_nar_data($grants, $node, $function) {
  267. $data = array();
  268. $duplicates = array();
  269. foreach ($grants as $grant) {
  270. if (empty($data[$grant['realm']][$grant['gid']])) {
  271. $data[$grant['realm']][$grant['gid']] = array('original' => $grant, 'current' => $grant, 'changes' => array());
  272. }
  273. else {
  274. if (empty($duplicates[$grant['realm']][$grant['gid']])) {
  275. $duplicates[$grant['realm']][$grant['gid']][] = $data[$grant['realm']][$grant['gid']]['original'];
  276. }
  277. $duplicates[$grant['realm']][$grant['gid']][] = $grant;
  278. }
  279. }
  280. if (!empty($duplicates)) {
  281. // generate an error message
  282. $msg = t('Devel Node Access has detected duplicate records returned from %function:', array('%function' => $function));
  283. $msg .= '<ul>';
  284. foreach ($duplicates as $realm => $data_by_realm) {
  285. foreach ($data_by_realm as $gid => $data_by_realm_gid) {
  286. $msg .= '<li><ul>';
  287. foreach ($data_by_realm_gid as $grant) {
  288. $msg .= "<li>$node->nid/$realm/$gid/" . ($grant['grant_view'] ? 1 : 0) . ($grant['grant_update'] ? 1 : 0) . ($grant['grant_delete'] ? 1 : 0) . ' by ' . $grant['#module'] . '</li>';
  289. }
  290. $msg .= '</ul></li>';
  291. }
  292. }
  293. $msg .= '</ul>';
  294. drupal_set_message($msg, 'error', FALSE);
  295. }
  296. return $data;
  297. }
  298. /**
  299. * Helper function to mimic hook_node_access_records_alter() and trace what
  300. * each module does with it.
  301. *
  302. * @param object $grants
  303. * An indexed array of grant records, augmented by the '#module' key,
  304. * as created by _devel_node_access_module_invoke_all('node_access_records').
  305. * This array is updated by the hook_node_access_records_alter()
  306. * implementations.
  307. *
  308. * @param $node
  309. * The node that the grant records belong to.
  310. *
  311. * @return
  312. * A tree representation of the grant records in $grants including their
  313. * history:
  314. * $data[$realm][$gid]
  315. * ['original'] - grant record before processing
  316. * ['current'] - grant record after processing (if still present)
  317. * ['changes'][]['op'] - change message (add/change/delete by $module)
  318. * ['grant'] - grant record after change (unless deleted)
  319. */
  320. function _devel_node_access_nar_alter(&$grants, $node) {
  321. //dpm($grants, '_devel_node_access_nar_alter(): grants IN');
  322. $dummy = array();
  323. drupal_alter('node_access_records', $dummy, $node);
  324. static $drupal_static = array();
  325. isset($drupal_static['drupal_alter']) || ($drupal_static['drupal_alter'] = &drupal_static('drupal_alter'));
  326. $functions = $drupal_static['drupal_alter'];
  327. // build the initial tree (and check for duplicates)
  328. $data = _devel_node_access_build_nar_data($grants, $node, 'hook_node_access_records()');
  329. // simulate drupal_alter('node_access_records', $grants, $node);
  330. foreach ($functions['node_access_records'] as $function) {
  331. // call hook_node_access_records_alter() for one module at a time and analyze
  332. $function($grants, $node); // <==
  333. $module = substr($function, 0, strlen($function) - 26);
  334. foreach ($grants as $i => $grant) {
  335. if (empty($data[$grant['realm']][$grant['gid']]['current'])) {
  336. // it's an added grant
  337. $data[$grant['realm']][$grant['gid']]['current'] = $grant;
  338. $data[$grant['realm']][$grant['gid']]['current']['#module'] = $module;
  339. $data[$grant['realm']][$grant['gid']]['changes'][] = array(
  340. 'op' => 'added by ' . $module,
  341. 'grant' => $grant,
  342. );
  343. $grants[$i]['#module'] = $module;
  344. }
  345. else {
  346. // it's an existing grant, check for changes
  347. $view = $update = $delete = FALSE;
  348. foreach (array('view', 'update', 'delete') as $op) {
  349. $$op = $grant["grant_$op"] - $data[$grant['realm']][$grant['gid']]['current']["grant_$op"];
  350. }
  351. $priority = $grant['priority'] - $data[$grant['realm']][$grant['gid']]['current']['priority'];
  352. if ($view || $update || $delete || $priority) {
  353. // it was changed
  354. $data[$grant['realm']][$grant['gid']]['current'] = $grant;
  355. $data[$grant['realm']][$grant['gid']]['current']['#module'] = $module;
  356. $data[$grant['realm']][$grant['gid']]['changes'][] = array(
  357. 'op' => 'altered by ' . $module,
  358. 'grant' => $grant,
  359. );
  360. $grants[$i]['#module'] = $module;
  361. }
  362. }
  363. $data[$grant['realm']][$grant['gid']]['found'] = TRUE;
  364. }
  365. // check for newly introduced duplicates
  366. _devel_node_access_build_nar_data($grants, $node, 'hook_node_access_records_alter()');
  367. // look for grant records that have disappeared
  368. foreach ($data as $realm => $data2) {
  369. foreach ($data2 as $gid => $data3) {
  370. if (empty($data[$realm][$gid]['found']) && isset($data[$realm][$gid]['current'])) {
  371. unset($data[$realm][$gid]['current']);
  372. $data[$realm][$gid]['changes'][] = array('op' => 'removed by ' . $module);
  373. }
  374. else {
  375. unset($data[$realm][$gid]['found']);
  376. }
  377. }
  378. }
  379. }
  380. //dpm($data, '_devel_node_access_nar_alter() returns');
  381. //dpm($grants, '_devel_node_access_nar_alter(): grants OUT');
  382. return $data;
  383. }
  384. /**
  385. * Helper function to mimic hook_node_grants_alter() and trace what
  386. * each module does with it.
  387. *
  388. * @param object $grants
  389. * An indexed array of grant records, augmented by the '#module' key,
  390. * as created by _devel_node_access_module_invoke_all('node_grants').
  391. * This array is updated by the hook_node_grants_alter()
  392. * implementations.
  393. *
  394. * @param $node
  395. * The node that the grant records belong to.
  396. *
  397. * @return
  398. * A tree representation of the grant records in $grants including their
  399. * history:
  400. * $data[$realm][$gid]
  401. * ['cur'] - TRUE or FALSE whether the gid is present or not
  402. * ['ori'][] - array of module names that contributed this grant (if any)
  403. * ['chg'][] - array of changes, such as
  404. * - 'added' if module name is a prefix if the $realm,
  405. * - 'added by module' otherwise, or
  406. * - 'removed by module'
  407. */
  408. function _devel_node_access_ng_alter(&$grants, $account, $op) {
  409. //dpm($grants, '_devel_node_access_ng_alter(): grants IN');
  410. $dummy = array();
  411. drupal_alter('node_grants', $dummy, $account, $op);
  412. static $drupal_static = array();
  413. isset($drupal_static['drupal_alter']) || ($drupal_static['drupal_alter'] = &drupal_static('drupal_alter'));
  414. $functions = $drupal_static['drupal_alter'];
  415. // build the initial structure
  416. $data = array();
  417. foreach ($grants as $realm => $gids) {
  418. foreach ($gids as $i => $gid) {
  419. if ($i !== '#module') {
  420. $data[$realm][$gid]['cur'] = TRUE;
  421. $data[$realm][$gid]['ori'][] = $gids['#module'];
  422. }
  423. }
  424. unset($grants[$realm]['#module']);
  425. }
  426. // simulate drupal_alter('node_grants', $grants, $account, $op);
  427. foreach ($functions['node_grants'] as $function) {
  428. // call hook_node_grants_alter() for one module at a time and analyze
  429. $function($grants, $account, $op); // <==
  430. $module = substr($function, 0, strlen($function) - 18);
  431. // check for new gids
  432. foreach ($grants as $realm => $gids) {
  433. foreach ($gids as $i => $gid) {
  434. if (empty($data[$realm][$gid]['cur'])) {
  435. $data[$realm][$gid]['cur'] = TRUE;
  436. $data[$realm][$gid]['chg'][] = 'added by ' . $module;
  437. }
  438. }
  439. }
  440. // check for removed gids
  441. foreach ($data as $realm => $gids) {
  442. foreach ($gids as $gid => $history) {
  443. if ($history['cur'] && array_search($gid, $grants[$realm]) === FALSE) {
  444. $data[$realm][$gid]['cur'] = FALSE;
  445. $data[$realm][$gid]['chg'][] = 'removed by ' . $module;
  446. }
  447. }
  448. }
  449. }
  450. //dpm($data, '_devel_node_access_ng_alter() returns');
  451. //dpm($grants, '_devel_node_access_ng_alter(): grants OUT');
  452. return $data;
  453. }
  454. /**
  455. * Implements hook_block_info().
  456. */
  457. function devel_node_access_block_info() {
  458. $blocks['dna_node'] = array(
  459. 'info' => t('Devel Node Access'),
  460. 'region' => 'footer',
  461. 'status' => 1,
  462. 'cache' => DRUPAL_NO_CACHE,
  463. );
  464. $blocks['dna_user'] = array(
  465. 'info' => t('Devel Node Access by User'),
  466. 'region' => 'footer',
  467. 'status' => 0,
  468. 'cache' => DRUPAL_NO_CACHE,
  469. );
  470. return $blocks;
  471. }
  472. /**
  473. * Implements hook_block_view().
  474. */
  475. function devel_node_access_block_view($delta) {
  476. global $user;
  477. global $theme_key;
  478. static $block1_visible, $hint = '';
  479. if (!isset($block1_visible)) {
  480. $block1_visible = db_query("SELECT status FROM {block} WHERE module = 'devel_node_access' AND delta = 'dna_user' AND theme = :theme", array(
  481. ':theme' => $theme_key,
  482. ))->fetchField();
  483. if (!$block1_visible) {
  484. $hint = t('For per-user access permissions enable the <a href="@link">%DNAbU block</a>.', array('@link' => url('admin/structure/block'), '%DNAbU' => t('Devel Node Access by User')));
  485. }
  486. }
  487. if (!user_access(DNA_ACCESS_VIEW)) {
  488. return;
  489. }
  490. $output = array();
  491. switch ($delta) {
  492. case 'dna_node':
  493. if (!count(dna_visible_nodes())) {
  494. return;
  495. }
  496. // include rows where nid == 0
  497. $nids = array_merge(array(0 => 0), dna_visible_nodes());
  498. $query = db_select('node_access', 'na');
  499. $query
  500. ->fields('na')
  501. ->condition('na.nid', $nids, 'IN')
  502. ->orderBy('na.nid')
  503. ->orderBy('na.realm')
  504. ->orderBy('na.gid');
  505. $nodes = node_load_multiple($nids);
  506. if (!variable_get('devel_node_access_debug_mode', FALSE)) {
  507. $headers = array(t('node'), t('realm'), t('gid'), t('view'), t('update'), t('delete'), t('explained'));
  508. $rows = array();
  509. foreach ($query->execute() as $row) {
  510. $explained = module_invoke_all('node_access_explain', $row);
  511. $rows[] = array(
  512. (empty($row->nid) ? '0' : '<a href="#node-' . $row->nid . '">' . _devel_node_access_get_node_title($nodes[$row->nid], TRUE) . '</a>'),
  513. $row->realm,
  514. $row->gid,
  515. $row->grant_view,
  516. $row->grant_update,
  517. $row->grant_delete,
  518. implode('<br />', $explained),
  519. );
  520. }
  521. $output[] = array(
  522. '#theme' => 'table',
  523. '#header' => $headers,
  524. '#rows' => $rows,
  525. '#attributes' => array('style' => 'text-align: left')
  526. );
  527. $hint = t('To see more details enable <a href="@debug_mode">debug mode</a>.', array('@debug_mode' => url('admin/config/development/devel', array('fragment' => 'edit-devel-node-access')))) . (empty($hint) ? '' : ' ' . $hint);
  528. }
  529. else {
  530. $tr = 't';
  531. $variables = array('!na' => '{node_access}');
  532. $states = array(
  533. 'default' => array(t('default'), 'ok', t('Default grant supplied by core in the absence of any other non-empty grants; in !na.', $variables)),
  534. 'ok' => array(t('ok'), 'ok', t('Highest priority grant; in !na.', $variables)),
  535. 'removed' => array(t('removed'), '', t('Was removed in @func; not in !na.', $variables + array('@func' => 'hook_node_access_records_alter()'))),
  536. 'static' => array(t('static'), 'ok', t('Non-standard grant in !na.', $variables)),
  537. 'unexpected' => array(t('unexpected'), 'warning', t('The 0/all/0/... grant applies to all nodes and all users -- usually it should not be present in !na if any node access module is active!')),
  538. 'ignored' => array(t('ignored'), 'warning', t('Lower priority grant; not in !na and thus ignored.', $variables)),
  539. 'empty' => array(t('empty'), 'warning', t('Does not grant any access, but could block lower priority grants; not in !na.', $variables)),
  540. 'wrong' => array(t('wrong'), 'error', t('Is rightfully in !na but at least one access flag is wrong!', $variables)),
  541. 'missing' => array(t('missing'), 'error', t("Should be in !na but isn't!", $variables)),
  542. 'removed!' => array(t('removed!'), 'error', t('Was removed in @func; should NOT be in !na!', $variables + array('@func' => 'hook_node_access_records_alter()'))),
  543. 'illegitimate' => array(t('illegitimate'), 'error', t('Should NOT be in !na because of lower priority!', $variables)),
  544. 'alien' => array(t('alien'), 'error', t('Should NOT be in !na because of unknown origin!', $variables)),
  545. );
  546. $active_states = array('default', 'ok', 'static', 'unexpected', 'wrong', 'illegitimate', 'alien');
  547. $headers = array(t('node'), t('prio'), t('status'), t('realm'), t('gid'), t('view'), t('update'), t('delete'), t('explained'));
  548. $headers = _devel_node_access_format_row($headers);
  549. $active_grants = array();
  550. foreach ($query->execute() as $active_grant) {
  551. $active_grants[$active_grant->nid][$active_grant->realm][$active_grant->gid] = $active_grant;
  552. }
  553. $all_grants = $checked_grants = $published_nid = array();
  554. foreach ($nids as $nid) {
  555. $acquired_grants_nid = array();
  556. if ($node = node_load($nid)) {
  557. // check node_access_acquire_grants()
  558. $grants = _devel_node_access_module_invoke_all('node_access_records', $node);
  559. // check drupal_alter('node_access_records')
  560. $data = _devel_node_access_nar_alter($grants, $node);
  561. /* (This was the D6 implementation that didn't analyze the hook_node_access_records_alter() details.)
  562. if (!empty($grants)) {
  563. $top_priority = NULL;
  564. foreach ($grants as $grant) {
  565. $priority = intval($grant['priority']);
  566. $top_priority = (isset($top_priority) ? max($top_priority, $priority) : $priority);
  567. $grant['priority'] = (isset($grant['priority']) ? $priority : '&ndash;&nbsp;');
  568. $acquired_grants_nid[$priority][$grant['realm']][$grant['gid']] = $grant + array(
  569. '#title' => _devel_node_access_get_node_title($node, TRUE),
  570. '#module' => (isset($grant['#module']) ? $grant['#module'] : ''),
  571. );
  572. }
  573. krsort($acquired_grants_nid);
  574. }
  575. /*/
  576. // (This is the new D7 implementation; it retains backward compatibility.)
  577. if (!empty($data)) {
  578. foreach ($data as $data_by_realm) {
  579. foreach ($data_by_realm as $data_by_realm_gid) { // by gid
  580. if (isset($data_by_realm_gid['current'])) {
  581. $grant = $data_by_realm_gid['current'];
  582. }
  583. elseif (isset($data_by_realm_gid['original'])) {
  584. $grant = $data_by_realm_gid['original'];
  585. $grant['#removed'] = 1;
  586. }
  587. else {
  588. continue;
  589. }
  590. $priority = intval($grant['priority']);
  591. $top_priority = (isset($top_priority) ? max($top_priority, $priority) : $priority);
  592. $grant['priority'] = (isset($grant['priority']) ? $priority : '&ndash;&nbsp;');
  593. $grant['history'] = $data_by_realm_gid;
  594. $acquired_grants_nid[$priority][$grant['realm']][$grant['gid']] = $grant + array(
  595. '#title' => _devel_node_access_get_node_title($node),
  596. '#module' => (isset($grant['#module']) ? $grant['#module'] : ''),
  597. );
  598. }
  599. }
  600. krsort($acquired_grants_nid);
  601. }
  602. /**/
  603. //dpm($acquired_grants_nid, "acquired_grants_nid =");
  604. // check node_access_grants()
  605. $published_nid[$nid] = $node->status;
  606. if ($node->nid) {
  607. foreach (array('view', 'update', 'delete') as $op) {
  608. $grants = _devel_node_access_module_invoke_all('node_grants', $user, $op);
  609. // call all hook_node_grants_alter() implementations
  610. $ng_alter_datas[$op] = _devel_node_access_ng_alter($grants, $user, $op);
  611. $checked_grants[$nid][$op] = array_merge(array('all' => array(0)), $grants);
  612. }
  613. }
  614. }
  615. // check for grants in the node_access table that aren't returned by node_access_acquire_grants()
  616. if (isset($active_grants[$nid])) {
  617. foreach ($active_grants[$nid] as $realm => $active_grants_realm) {
  618. foreach ($active_grants_realm as $gid => $active_grant) {
  619. $found = FALSE;
  620. $count_nonempty_grants = 0;
  621. foreach ($acquired_grants_nid as $priority => $acquired_grants_nid_priority) {
  622. if (isset($acquired_grants_nid_priority[$realm][$gid])) {
  623. $found = TRUE;
  624. }
  625. }
  626. if ($acquired_grants_nid_priority = reset($acquired_grants_nid)) { // highest priority only
  627. foreach ($acquired_grants_nid_priority as $acquired_grants_nid_priority_realm) {
  628. foreach ($acquired_grants_nid_priority_realm as $acquired_grants_nid_priority_realm_gid) {
  629. $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']));
  630. }
  631. }
  632. }
  633. $fixed_grant = (array) $active_grant;
  634. if ($count_nonempty_grants == 0 && $realm == 'all' && $gid == 0) {
  635. $fixed_grant += array(
  636. 'priority' => '&ndash;',
  637. 'state' => 'default',
  638. );
  639. }
  640. elseif (!$found) {
  641. $acknowledged = _devel_node_access_module_invoke_all('node_access_acknowledge', $fixed_grant);
  642. if (empty($acknowledged)) {
  643. // no module acknowledged this record, mark it as alien
  644. $fixed_grant += array(
  645. 'priority' => '?',
  646. 'state' => 'alien',
  647. );
  648. }
  649. else {
  650. // at least one module acknowledged the record, attribute it to the first one
  651. $acknowledged_keys = array_keys($acknowledged);
  652. $fixed_grant += array(
  653. 'priority' => '&ndash;',
  654. 'state' => 'static',
  655. '#module' => reset($acknowledged_keys),
  656. );
  657. }
  658. }
  659. else {
  660. continue;
  661. }
  662. $fixed_grant += array(
  663. 'nid' => $nid,
  664. '#title' => _devel_node_access_get_node_title($node),
  665. );
  666. $all_grants[] = $fixed_grant;
  667. }
  668. }
  669. }
  670. // order grants and evaluate their status
  671. foreach ($acquired_grants_nid as $priority => $acquired_grants_priority) {
  672. ksort($acquired_grants_priority);
  673. foreach ($acquired_grants_priority as $realm => $acquired_grants_realm) {
  674. ksort($acquired_grants_realm);
  675. foreach ($acquired_grants_realm as $gid => $acquired_grant) {
  676. if ($priority == $top_priority) {
  677. if (empty($acquired_grant['grant_view']) && empty($acquired_grant['grant_update']) && empty($acquired_grant['grant_delete'])) {
  678. $acquired_grant['state'] = 'empty';
  679. }
  680. else {
  681. if (isset($active_grants[$nid][$realm][$gid])) {
  682. $acquired_grant['state'] = (isset($acquired_grant['#removed']) ? 'removed!' : 'ok');
  683. }
  684. else {
  685. $acquired_grant['state'] = (isset($acquired_grant['#removed']) ? 'removed' : 'missing');
  686. }
  687. if ($acquired_grant['state'] == 'ok') {
  688. foreach (array('view', 'update', 'delete') as $op) {
  689. $active_grant = (array) $active_grants[$nid][$realm][$gid];
  690. if (empty($acquired_grant["grant_$op"]) != empty($active_grant["grant_$op"])) {
  691. $acquired_grant["grant_$op!"] = $active_grant["grant_$op"];
  692. }
  693. }
  694. }
  695. }
  696. }
  697. else {
  698. $acquired_grant['state'] = (isset($active_grants[$nid][$realm][$gid]) ? 'illegitimate' : 'ignored');
  699. }
  700. $all_grants[] = $acquired_grant + array('nid' => $nid);
  701. }
  702. }
  703. }
  704. }
  705. // fill in the table rows
  706. $rows = array();
  707. $error_count = 0;
  708. foreach ($all_grants as $grant) {
  709. $row = new stdClass();
  710. $row->nid = $grant['nid'];
  711. $row->title = $grant['#title'];
  712. $row->priority = $grant['priority'];
  713. $row->state = array('data' => $states[$grant['state']][0], 'title' => $states[$grant['state']][2]);
  714. $row->realm = $grant['realm'];
  715. $row->gid = $grant['gid'];
  716. $row->grant_view = $grant['grant_view'];
  717. $row->grant_update = $grant['grant_update'];
  718. $row->grant_delete = $grant['grant_delete'];
  719. $row->explained = implode('<br />', module_invoke_all('node_access_explain', $row));
  720. unset($row->title); // possibly needed above
  721. if ($row->nid == 0 && $row->gid == 0 && $row->realm == 'all' && count($all_grants) > 1) {
  722. $row->state = array('data' => $states['unexpected'][0], 'title' => $states['unexpected'][2]);
  723. $class = $states['unexpected'][1];
  724. }
  725. else {
  726. $class = $states[$grant['state']][1];
  727. }
  728. $row = (array) $row;
  729. foreach (array('view', 'update', 'delete') as $op) {
  730. $row["grant_$op"] = array('data' => $row["grant_$op"]);
  731. 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)) {
  732. $row["grant_$op"]['data'] .= '&prime;';
  733. $row["grant_$op"]['title'] = t('This entry grants access to this node to this user.');
  734. }
  735. if (isset($grant["grant_$op!"])) {
  736. $row["grant_$op"]['data'] = $grant["grant_$op!"] . '&gt;' . (!$row["grant_$op"]['data'] ? 0 : $row["grant_$op"]['data']);
  737. $row["grant_$op"]['class'][] = 'error';
  738. if ($class == 'ok') {
  739. $row['state'] = array('data' => $states['wrong'][0], 'title' => $states['wrong'][2]);
  740. $class = $states['wrong'][1];
  741. }
  742. }
  743. }
  744. $error_count += ($class == 'error');
  745. $row['nid'] = array(
  746. 'data' => '<a href="#node-' . $grant['nid'] . '">' . $row['nid'] . '</a>',
  747. 'title' => $grant['#title'],
  748. );
  749. if (empty($grant['#module']) || strpos($grant['realm'], $grant['#module']) === 0) {
  750. $row['realm'] = $grant['realm'];
  751. }
  752. else {
  753. $row['realm'] = array(
  754. 'data' => $grant['#module'] . ':<br />' . $grant['realm'],
  755. 'title' => t("The '@module' module fails to adhere to the best practice of naming its realm(s) after itself.", array('@module' => $grant['#module'])),
  756. );
  757. }
  758. // prepend information from the D7 hook_node_access_records_alter()
  759. $next_style = array();
  760. if (isset($grant['history'])) {
  761. $history = $grant['history'];
  762. if (($num_changes = count($history['changes']) - empty($history['current'])) > 0) {
  763. $first_row = TRUE;
  764. while (isset($history['original']) || $num_changes--) {
  765. if (isset($history['original'])) {
  766. $this_grant = $history['original'];
  767. $this_action = '[ Original by ' . $this_grant['#module'] . ':';
  768. unset($history['original']);
  769. }
  770. else {
  771. $change = $history['changes'][0];
  772. $this_grant = $change['grant'];
  773. $this_action = ($first_row ? '[ ' : '') . $change['op'] . ':';
  774. array_shift($history['changes']);
  775. }
  776. $rows[] = array(
  777. 'data' => array(
  778. 'data' => array(
  779. 'data' => $this_action,
  780. 'style' => array('padding-bottom: 0;'),
  781. ),
  782. ),
  783. 'style' => array_merge(($first_row ? array() : array('border-top-style: dashed;', 'border-top-width: 1px;')), array('border-bottom-style: none;')),
  784. );
  785. $next_style = array('border-top-style: none;');
  786. if (count($history['changes'])) {
  787. $g = $this_grant;
  788. $rows[] = array(
  789. 'data' => array('v', $g['priority'], '', $g['realm'], $g['gid'], $g['grant_view'], $g['grant_update'], $g['grant_delete'], 'v'),
  790. 'style' => array('border-top-style: none;', 'border-bottom-style: dashed;'),
  791. );
  792. $next_style = array('border-top-style: dashed;');
  793. }
  794. $first_row = FALSE;
  795. }
  796. }
  797. }
  798. // fix up the main row cells with the proper class (needed for Bartik)
  799. foreach ($row as $key => $value) {
  800. if (!is_array($value)) {
  801. $row[$key] = array('data' => $value);
  802. }
  803. $row[$key]['class'] = array($class);
  804. }
  805. // add the main row
  806. $will_append = empty($history['current']) && !empty($history['changes']);
  807. $rows[] = array(
  808. 'data' => array_values($row),
  809. 'class' => array($class),
  810. 'style' => array_merge($next_style, ($will_append ? array('border-bottom-style: none;') : array())),
  811. );
  812. // append information from the D7 hook_node_access_records_alter()
  813. if ($will_append) {
  814. $last_change = end($history['changes']);
  815. $rows[] = array(
  816. 'data' => array(
  817. 'data' => array(
  818. 'data' => $last_change['op'] . ' ]',
  819. 'style' => array('padding-top: 0;'),
  820. ),
  821. ),
  822. 'style' => array('border-top-style: none;'),
  823. );
  824. }
  825. }
  826. foreach ($rows as $i => $row) {
  827. $rows[$i] = _devel_node_access_format_row($row);
  828. }
  829. $output[] = array(
  830. '#theme' => 'table',
  831. '#header' => $headers,
  832. '#rows' => $rows,
  833. '#attributes' => array(
  834. 'class' => array('system-status-report'),
  835. 'style' => 'text-align: left;',
  836. ),
  837. );
  838. $output[] = array(
  839. '#theme' => 'form_element',
  840. '#description' => t('(Some of the table elements provide additional information if you hover your mouse over them.)'),
  841. );
  842. if ($error_count > 0) {
  843. $variables['!Rebuild_permissions'] = '<a href="' . url('admin/reports/status/rebuild') . '">' . $tr('Rebuild permissions') . '</a>';
  844. $output[] = array(
  845. '#prefix' => "\n<div class=\"error\">",
  846. '#markup' => 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),
  847. '#suffix' => "</div><br />\n",
  848. );
  849. }
  850. // Explain whether access is granted or denied, and why (using code from node_access()).
  851. $tr = 't';
  852. array_shift($nids); // remove the 0
  853. $accounts = array();
  854. $variables += array(
  855. '!username' => '<em class="placeholder">' . theme('username', array('account' => $user)) . '</em>',
  856. '%uid' => $user->uid,
  857. );
  858. if (user_access('bypass node access')) {
  859. $variables['%bypass_node_access'] = $tr('bypass node access');
  860. $output[] = array(
  861. '#markup' => t('!username has the %bypass_node_access permission and thus full access to all nodes.', $variables),
  862. '#suffix' => '<br />&nbsp;',
  863. );
  864. }
  865. else {
  866. $variables['!list'] = '<div style="margin-left: 2em">' . _devel_node_access_get_grant_list($nid, $ng_alter_datas['view']) . '</div>';
  867. $variables['%access'] = 'view';
  868. $output[] = array(
  869. '#prefix' => "\n<div style='text-align: left' title='" . t('These are the grants returned by hook_node_grants() for this user.') . "'>",
  870. '#markup' => t('!username (user %uid) can use these grants (if they are present above) for %access access: !list', $variables),
  871. '#suffix' => "</div>\n",
  872. );
  873. $accounts[] = $user;
  874. }
  875. $menu_item = menu_get_item();
  876. $map = $menu_item['original_map'];
  877. if ($map[0] == 'node' && isset($map[1]) && is_numeric($map[1]) && !isset($map[2]) && !$block1_visible) {
  878. // only for single nodes
  879. if (user_is_logged_in()) {
  880. $accounts[] = user_load(0); // Anonymous, too
  881. }
  882. foreach ($accounts as $account) {
  883. $account_items = array();
  884. $nid_items = array();
  885. foreach ($nids as $nid) {
  886. $op_items = array();
  887. foreach (array('create', 'view', 'update', 'delete') as $op) {
  888. $explain = _devel_node_access_explain_access($op, $nid, $account);
  889. $op_items[] = "<div style='width: 5em; display: inline-block'>" . t('%op:', array('%op' => $op)) . ' </div>' . $explain[2];
  890. }
  891. $nid_items[] = array(
  892. '#theme' => 'item_list',
  893. '#items' => $op_items,
  894. '#type' => 'ul',
  895. '#prefix' => t('to node !nid:', array('!nid' => l($nid, 'node/' . $nid))) . "\n<div style='margin-left: 2em'>",
  896. '#suffix' => '</div>',
  897. );
  898. }
  899. if (count($nid_items) == 1) {
  900. $account_items = $nid_items[0];
  901. }
  902. else {
  903. $account_items = array(
  904. '#theme' => 'item_list',
  905. '#items' => $nid_items,
  906. '#type' => 'ul',
  907. '#prefix' => "\n<div style='margin-left: 2em'>",
  908. '#suffix' => '</div>',
  909. );
  910. }
  911. $variables['!username'] = '<em class="placeholder">' . theme('username', array('account' => $account)) . '</em>';
  912. $output[] = array(
  913. '#prefix' => "\n<div style='text-align: left'>",
  914. '#type' => 'item',
  915. 'lead-in' => array('#markup' => t("!username has the following access", $variables) . ' '),
  916. 'items' => $account_items,
  917. '#suffix' => "\n</div>\n",
  918. );
  919. }
  920. }
  921. }
  922. if (!empty($hint)) {
  923. $output[] = array(
  924. '#theme' => 'form_element',
  925. '#description' => '(' . $hint . ')',
  926. );
  927. }
  928. $output[]['#markup'] = '<br /><br />';
  929. $subject = t('node_access entries for nodes shown on this page');
  930. return array('subject' => $subject, 'content' => $output);
  931. case 'dna_user':
  932. // show which users can access this node
  933. $menu_item = menu_get_item();
  934. $map = $menu_item['original_map'];
  935. if ($map[0] != 'node' || !isset($map[1]) || !is_numeric($map[1]) || isset($map[2])) {
  936. // Ignore anything but node/%.
  937. return;
  938. }
  939. if (isset($menu_item['map'][1]) && is_object($node = $menu_item['map'][1])) {
  940. // We have the node.
  941. }
  942. elseif (is_numeric($menu_item['original_map'][1])) {
  943. $node = node_load($menu_item['original_map'][1]);
  944. }
  945. if (isset($node)) {
  946. $nid = $node->nid;
  947. $node_type = node_type_get_type($node);
  948. $variables = array('@Node_type' => ($node_type ? $node_type->name : $node->type));
  949. $create_header = '<span title="' . t("Create nodes of the '@Node_type' type.", $variables) . '">' . t('create') . '</span>';
  950. if (!$node_type) {
  951. $create_header .= '<br /><span class="error">' . t("(missing type: '@Node_type')", $variables) . '</span>';
  952. }
  953. $headers = array(t('username'), $create_header, t('view'), t('update'), t('delete'));
  954. $rows = array();
  955. // Determine whether to use Ajax or prepopulate the tables.
  956. if ($ajax = variable_get('devel_node_access_user_ajax', FALSE)) {
  957. drupal_add_js(drupal_get_path('module', 'devel_node_access') . '/devel_node_access.js');
  958. }
  959. // Find all users. The following operations are very inefficient, so we
  960. // limit the number of users returned. It would be better to make a
  961. // pager query, or at least make the number of users configurable. If
  962. // anyone is up for that please submit a patch.
  963. $query = db_select('users', 'u')
  964. ->fields('u', array('uid'))
  965. ->orderBy('u.access', 'DESC')
  966. ->range(0, 9);
  967. $uids = $query->execute()->fetchCol();
  968. array_unshift($uids, 0);
  969. $accounts = user_load_multiple($uids);
  970. foreach ($accounts as $account) {
  971. $username = theme('username', array('account' => $account));
  972. if ($account->uid == $user->uid) {
  973. $username = '<strong>' . $username . '</strong>';
  974. }
  975. $rows[] = array(
  976. $username,
  977. array(
  978. 'id' => 'create-' . $nid . '-' . $account->uid,
  979. 'class' => 'dna-permission',
  980. 'data' => $ajax ? NULL : theme('dna_permission', array('permission' => _devel_node_access_explain_access('create', $nid, $account))),
  981. ),
  982. array(
  983. 'id' => 'view-' . $nid . '-' . $account->uid,
  984. 'class' => 'dna-permission',
  985. 'data' => $ajax ? NULL : theme('dna_permission', array('permission' => _devel_node_access_explain_access('view', $nid, $account))),
  986. ),
  987. array(
  988. 'id' => 'update-' . $nid . '-' . $account->uid,
  989. 'class' => 'dna-permission',
  990. 'data' => $ajax ? NULL : theme('dna_permission', array('permission' => _devel_node_access_explain_access('update', $nid, $account))),
  991. ),
  992. array(
  993. 'id' => 'delete-' . $nid . '-' . $account->uid,
  994. 'class' => 'dna-permission',
  995. 'data' => $ajax ? NULL : theme('dna_permission', array('permission' => _devel_node_access_explain_access('delete', $nid, $account))),
  996. ),
  997. );
  998. }
  999. if (count($rows)) {
  1000. $output[] = array(
  1001. '#theme' => 'table',
  1002. '#header' => $headers,
  1003. '#rows' => $rows,
  1004. '#attributes' => array('style' => 'text-align: left'),
  1005. );
  1006. $output[] = array(
  1007. '#theme' => 'form_element',
  1008. '#description' => t('(This table lists the most-recently active users. Hover your mouse over each result for more details.)'),
  1009. );
  1010. return array(
  1011. 'subject' => t('Access permissions by user'),
  1012. 'content' => $output,
  1013. );
  1014. }
  1015. }
  1016. break;
  1017. }
  1018. }
  1019. /**
  1020. * Callback function for node access by user block ajax.
  1021. */
  1022. function devel_node_access_user_ajax($data_type, $cell) {
  1023. list($op, $nid, $uid) = explode('-', $cell);
  1024. $account = user_load($uid);
  1025. $output = array(
  1026. '#theme' => 'dna_permission',
  1027. '#permission' => _devel_node_access_explain_access($op, $nid, $account),
  1028. );
  1029. // JSON output for Ajax callbacks.
  1030. if ($data_type == 'json') {
  1031. drupal_json_output(drupal_render($output));
  1032. exit;
  1033. }
  1034. // HTML output for error message click-throughs.
  1035. if ($node = node_load($nid)) {
  1036. $node_title = l($node->title, 'node/' . $node->nid);
  1037. }
  1038. else {
  1039. $node_title = t('unknown node %nid', array('%nid' => $nid));
  1040. }
  1041. drupal_set_title(
  1042. t(
  1043. 'Devel node access: %op permission for !user on !node',
  1044. array(
  1045. '%op' => $op,
  1046. '!user' => theme('username', array('account' => $account)),
  1047. '!node' => $node_title,
  1048. )
  1049. ),
  1050. PASS_THROUGH
  1051. );
  1052. return $output;
  1053. }
  1054. /**
  1055. * Helper function that mimicks node.module's node_access() function.
  1056. *
  1057. * Unfortunately, this needs to be updated manually whenever node.module changes!
  1058. *
  1059. * @return
  1060. * An array suitable for theming with theme_dna_permission().
  1061. */
  1062. function _devel_node_access_explain_access($op, $node, $account = NULL) {
  1063. global $user;
  1064. if (is_numeric($node) && !($node = node_load($node))) {
  1065. return array(
  1066. FALSE,
  1067. '???',
  1068. t('Unable to load the node &ndash; this should never happen!'),
  1069. );
  1070. }
  1071. if (!in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) {
  1072. return array(
  1073. FALSE,
  1074. t('!NO: invalid $op', array('!NO' => t('NO'))),
  1075. t("'@op' is an invalid operation!", array('@op' => $op)),
  1076. );
  1077. }
  1078. if ($op == 'create' && is_object($node)) {
  1079. $node = $node->type;
  1080. }
  1081. if (!empty($account)) {
  1082. // To try to get the most authentic result we impersonate the given user!
  1083. // This may reveal bugs in other modules, leading to contradictory results.
  1084. $saved_user = $user;
  1085. drupal_save_session(FALSE);
  1086. $user = $account;
  1087. $result = _devel_node_access_explain_access($op, $node, NULL);
  1088. $user = $saved_user;
  1089. drupal_save_session(TRUE);
  1090. $second_opinion = node_access($op, $node, $account);
  1091. if ($second_opinion != $result[0]) {
  1092. $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>';
  1093. }
  1094. return $result;
  1095. }
  1096. $variables = array(
  1097. '!NO' => t('NO'),
  1098. '!YES' => t('YES'),
  1099. '!bypass_node_access' => t('bypass node access'),
  1100. '!access_content' => t('access content'),
  1101. );
  1102. if (user_access('bypass node access')) {
  1103. return array(
  1104. TRUE,
  1105. t('!YES: bypass node access', $variables),
  1106. t("!YES: This user has the '!bypass_node_access' permission and may do everything with nodes.", $variables),
  1107. );
  1108. }
  1109. if (!user_access('access content')) {
  1110. return array(
  1111. FALSE,
  1112. t('!NO: access content', $variables),
  1113. t("!NO: This user does not have the '!access_content' permission and is denied doing anything with content.", $variables),
  1114. );
  1115. }
  1116. foreach (module_implements('node_access') as $module) {
  1117. $function = $module . '_node_access';
  1118. if (function_exists($function)) {
  1119. $result = $function($node, $op, $user);
  1120. if ($module == 'node') {
  1121. $module = 'node (permissions)';
  1122. }
  1123. if (isset($result)) {
  1124. if ($result === NODE_ACCESS_DENY) {
  1125. $denied_by[] = $module;
  1126. }
  1127. elseif ($result === NODE_ACCESS_ALLOW) {
  1128. $allowed_by[] = $module;
  1129. }
  1130. $access[] = $result;
  1131. }
  1132. }
  1133. }
  1134. $variables += array(
  1135. '@deniers' => (empty($denied_by) ? NULL : implode(', ', $denied_by)),
  1136. '@allowers' => (empty($allowed_by) ? NULL : implode(', ', $allowed_by)),
  1137. );
  1138. if (!empty($denied_by)) {
  1139. $variables += array(
  1140. '%module' => $denied_by[0] . (count($denied_by) > 1 ? '+' : ''),
  1141. );
  1142. return array(
  1143. FALSE,
  1144. t('!NO: by %module', $variables),
  1145. empty($allowed_by)
  1146. ? t("!NO: hook_node_access() of the following module(s) denies this: @deniers.", $variables)
  1147. : t("!NO: hook_node_access() of the following module(s) denies this: @deniers &ndash; even though the following module(s) would allow it: @allowers.", $variables),
  1148. );
  1149. }
  1150. if (!empty($allowed_by)) {
  1151. $variables += array(
  1152. '%module' => $allowed_by[0] . (count($allowed_by) > 1 ? '+' : ''),
  1153. '!view_own_unpublished_content' => t('view own unpublished content'),
  1154. );
  1155. return array(
  1156. TRUE,
  1157. t('!YES: by %module', $variables),
  1158. t("!YES: hook_node_access() of the following module(s) allows this: @allowers.", $variables),
  1159. );
  1160. }
  1161. if ($op == 'view' && !$node->status && user_access('view own unpublished content') && $user->uid == $node->uid && $user->uid != 0) {
  1162. return array(
  1163. TRUE,
  1164. t('!YES: view own unpublished content', $variables),
  1165. t("!YES: The node is unpublished, but the user has the '!view_own_unpublished_content' permission.", $variables),
  1166. );
  1167. }
  1168. if ($op != 'create' && $node->nid) {
  1169. if (node_access($op, $node)) { // delegate this part
  1170. $variables['@node_access_table'] = '{node_access}';
  1171. return array(
  1172. TRUE,
  1173. t('!YES: @node_access_table', $variables),
  1174. t('!YES: Node access allows this based on one or more records in the @node_access_table table (see the other DNA block!).', $variables),
  1175. );
  1176. }
  1177. }
  1178. return array(
  1179. FALSE,
  1180. t('!NO: no reason', $variables),
  1181. t("!NO: None of the checks resulted in allowing this, so it's denied.", $variables)
  1182. . ($op == 'create' ? ' ' . t('This is most likely due to a withheld permission.') : ''),
  1183. );
  1184. }
  1185. /**
  1186. * Helper function to create a list of the grants returned by hook_node_grants().
  1187. */
  1188. function _devel_node_access_get_grant_list($nid, $ng_alter_data) {
  1189. //dpm($ng_alter_data, "_devel_node_access_get_grant_list($nid,");
  1190. $ng_alter_data = array_merge(array('all' => array(0 => array('cur' => TRUE, 'ori' => array('all')))), $ng_alter_data);
  1191. $items = array();
  1192. if (count($ng_alter_data)) {
  1193. foreach ($ng_alter_data as $realm => $gids) {
  1194. ksort($gids);
  1195. $gs = array();
  1196. foreach ($gids as $gid => $history) {
  1197. if ($history['cur']) {
  1198. if (isset($history['ori'])) {
  1199. $g = $gid; // original grant, still active
  1200. }
  1201. else {
  1202. $g = '<u>' . $gid . '</u>'; // new grant, still active
  1203. }
  1204. }
  1205. else {
  1206. $g = '<del>' . $gid . '</del>'; // deleted grant
  1207. }
  1208. $ghs = array();
  1209. if (isset($history['ori']) && strpos($realm, $history['ori'][0]) !== 0) {
  1210. $ghs[] = 'by ' . $history['ori'][0];
  1211. }
  1212. if (isset($history['chg'])) {
  1213. foreach ($history['chg'] as $h) {
  1214. $ghs[] = $h;
  1215. }
  1216. }
  1217. if (!empty($ghs)) {
  1218. $g .= ' (' . implode(', ', $ghs) . ')';
  1219. }
  1220. $gs[] = $g;
  1221. }
  1222. $items[] = $realm . ': ' . implode(', ', $gs);
  1223. }
  1224. if (!empty($items)) {
  1225. return theme('item_list', array('items' => $items, 'type' => 'ul'));
  1226. }
  1227. }
  1228. }
  1229. /**
  1230. * Implements hook_node_access_explain().
  1231. */
  1232. function devel_node_access_node_access_explain($row) {
  1233. if ($row->gid == 0 && $row->realm == 'all') {
  1234. foreach (array('view', 'update', 'delete') as $op) {
  1235. $gop = 'grant_' . $op;
  1236. if (!empty($row->$gop)) {
  1237. $ops[] = $op;
  1238. }
  1239. }
  1240. if (empty($ops)) {
  1241. return '(No access granted to ' . ($row->nid == 0 ? 'any nodes.)' : 'this node.)');
  1242. }
  1243. else {
  1244. return 'All users may ' . implode('/', $ops) . ($row->nid == 0 ? ' all nodes.' : ' this node.');
  1245. }
  1246. }
  1247. }
  1248. /**
  1249. * Helper function to return a sanitized node title.
  1250. */
  1251. function _devel_node_access_get_node_title($node, $clip_and_decorate = FALSE) {
  1252. if (isset($node)) {
  1253. if (isset($node->title)) {
  1254. $node_title = check_plain(!is_array($node->title) ? $node->title : $node->title[LANGUAGE_NONE][0]['value']);
  1255. if ($clip_and_decorate) {
  1256. if (drupal_strlen($node_title) > 20) {
  1257. $node_title = "<span title='node/$node->nid: $node_title'>" . drupal_substr($node_title, 0, 15) . '...</span>';
  1258. }
  1259. $node_title = '<span title="node/' . $node->nid . '">' . $node_title . '</span>';
  1260. }
  1261. return $node_title;
  1262. }
  1263. elseif (isset($node->nid)) {
  1264. return $node->nid;
  1265. }
  1266. }
  1267. return '&mdash;';
  1268. }
  1269. /**
  1270. * Helper function to apply common formatting to a debug-mode table row.
  1271. */
  1272. function _devel_node_access_format_row($row, $may_unpack = TRUE) {
  1273. if ($may_unpack && isset($row['data'])) {
  1274. $row['data'] = _devel_node_access_format_row($row['data'], FALSE);
  1275. $row['class'][] = 'even';
  1276. return $row;
  1277. }
  1278. if (count($row) == 1) {
  1279. if (is_scalar($row['data'])) {
  1280. $row['data'] = array('data' => $row['data']);
  1281. }
  1282. $row['data']['colspan'] = 9;
  1283. }
  1284. else {
  1285. $row = array_values($row);
  1286. foreach (array(0, 1, 4) as $j) { // node, prio, gid
  1287. if (is_scalar($row[$j])) {
  1288. $row[$j] = array('data' => $row[$j]);
  1289. }
  1290. $row[$j]['style'][] = 'text-align: right;';
  1291. }
  1292. }
  1293. return $row;
  1294. }
  1295. /**
  1296. * Implements hook_theme().
  1297. */
  1298. function devel_node_access_theme() {
  1299. return array(
  1300. 'dna_permission' => array(
  1301. 'variables' => array(
  1302. 'permission' => NULL,
  1303. ),
  1304. ),
  1305. );
  1306. }
  1307. /**
  1308. * Indicate whether user has a permission or not.
  1309. */
  1310. function theme_dna_permission($variables) {
  1311. $permission = &$variables['permission'];
  1312. return '<span class="' . ($permission[0] ? 'ok' : 'error') . '" title="' . $permission[2] . '">' . $permission[1] . '</span>';
  1313. }