devel_node_access.module 56 KB

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