fpa.module 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @file
  4. * Main module file for FPA.
  5. */
  6. define('FPA_ATTR_PERMISSION', 'fpa-permission');
  7. define('FPA_ATTR_MODULE', 'fpa-module');
  8. define('FPA_ATTR_ROLE', 'fpa-role');
  9. define('FPA_ATTR_CHECKED', 'fpa-checked');
  10. define('FPA_ATTR_NOT_CHECKED', 'fpa-not-checked');
  11. define('FPA_ATTR_SYSTEM_NAME', 'fpa-system-name');
  12. // Replacement for improper use of hook_hook_info().
  13. require_once dirname(__FILE__) . '/fpa.theme.inc';
  14. require_once dirname(__FILE__) . '/fpa.form_alter.inc';
  15. /**
  16. * Implements hook_menu().
  17. */
  18. function fpa_menu() {
  19. $items = array();
  20. $items['fpa_modal/%fpa_js/permissions'] = array(
  21. 'title' => 'Permissions',
  22. 'page callback' => '_fpa_ctools',
  23. 'page arguments' => array(1),
  24. 'access arguments' => array('administer permissions'),
  25. 'type' => MENU_CALLBACK,
  26. );
  27. return $items;
  28. }
  29. /**
  30. * Implements hook_admin_paths().
  31. */
  32. function fpa_admin_paths() {
  33. return array(
  34. 'fpa_modal/*/permissions' => TRUE,
  35. );
  36. }
  37. /**
  38. * Implements hook_help().
  39. */
  40. function fpa_help($path, $arg) {
  41. if ($path == 'admin/people/permissions') {
  42. $output = '';
  43. $output .= '<p>' . t('Permissions and Module names will match on the readable or system name. The system name is provided as a togglable column.') . '</p>';
  44. $output .= '<p>' . t('Enter in the format of "permission@module", e.g. "admin@system" will show only permissions with the text "admin" in modules with the text "system".') . '</p>';
  45. return $output;
  46. }
  47. }
  48. /**
  49. * Loader function to wrap ctools loader function.
  50. */
  51. function fpa_js_load($arg) {
  52. $return_code = 0;
  53. if (module_exists('ctools')) {
  54. $return_code = ctools_js_load($arg);
  55. }
  56. return $return_code;
  57. }
  58. function fpa_l($permission = '', $text = NULL, $options = array()) {
  59. // Available modal module system names in order of priority.
  60. $supported_modules = array(
  61. 'ctools',
  62. );
  63. if (is_null($text)) {
  64. $text = t('Manage Permissions');
  65. }
  66. // Find the most preferred modal method from the available methods.
  67. $modules = array_intersect($supported_modules, array_keys(module_list()));
  68. $method = array_shift($modules);
  69. $path = 'fpa_modal/nojs/permissions';
  70. $query = array(
  71. 'fpa_perm' => $permission,
  72. 'fpa_method' => $method,
  73. );
  74. $attributes = array();
  75. switch ($method) {
  76. case 'ctools':
  77. ctools_include('ajax');
  78. ctools_include('modal');
  79. ctools_modal_add_js();
  80. $attributes['class'] = 'ctools-use-modal';
  81. break;
  82. default:
  83. $path = 'admin/people/permissions';
  84. $attributes['target'] = '_blank';
  85. $query['fpa_method'] = '_blank';
  86. break;
  87. }
  88. $options = array_merge_recursive(array('query' => $query, 'attributes' => $attributes), $options);
  89. return l($text, $path, $options);
  90. }
  91. function fpa_fieldset($filter_string, &$parent_item, $group = array()) {
  92. $parent_item['fpa_fieldset'] = array(
  93. '#type' => 'fieldset',
  94. '#title' => t('Permissions'),
  95. '#collapsible' => TRUE,
  96. '#collapsed' => TRUE,
  97. '#description' => t('Clicking on this link will launch a modal/tab/window displaying the appropriate permissions.'),
  98. );
  99. $parent_item['fpa_fieldset'] = array_merge_recursive($parent_item['fpa_fieldset'], $group);
  100. $parent_item['fpa_fieldset']['fpa'] = array(
  101. '#type' => 'markup',
  102. '#markup' => fpa_l($filter_string),
  103. );
  104. }
  105. /**
  106. * Page callback for CTools modal path.
  107. */
  108. function _fpa_ctools($js = NULL) {
  109. // Need to include the file that contains the permissions form.
  110. module_load_include('inc', 'user', 'user.admin');
  111. $form_id = 'user_admin_permissions';
  112. // Fall back if $js is not set.
  113. if (!$js) {
  114. return drupal_get_form($form_id);
  115. }
  116. ctools_include('modal');
  117. ctools_include('ajax');
  118. $form_state = array(
  119. 'title' => t('Permissions'),
  120. 'ajax' => TRUE,
  121. );
  122. switch ($_GET['fpa_method']) {
  123. case 'ctools':
  124. $output = ctools_modal_form_wrapper($form_id, $form_state);
  125. break;
  126. }
  127. if (!empty($form_state['executed'])) {
  128. $output = array();
  129. $output[] = ctools_ajax_command_reload();
  130. }
  131. echo ajax_render($output);
  132. exit;
  133. }
  134. /**
  135. * @return int Approximate number of bytes of ram required to render the permissions form.
  136. */
  137. function _fpa_memory_required() {
  138. $permissions_count = count(module_invoke_all('permission'));
  139. $user_roles_count = count(user_roles());
  140. $page_ram_required = (9 * 1024 * 1024);
  141. // Takes ~26kb per row without any checkboxes.
  142. $permission_row_overhead = 27261.028783658;
  143. $permissions_ram_required = $permissions_count * $permission_row_overhead;
  144. // Determined by checking peak ram on permissions page, over several different number of visible roles.
  145. $bytes_per_checkbox = 18924.508820799;
  146. $checkboxes_ram_required = $permissions_count * $user_roles_count * $bytes_per_checkbox;
  147. return (int) ($page_ram_required + $permissions_ram_required + $checkboxes_ram_required);
  148. }
  149. function _fpa_reset_filter_defaults() {
  150. setcookie("fpa_module_match", "", time() - 3600, '/');
  151. setcookie("fpa_filter", "", time() - 3600, '/');
  152. setcookie("fpa_roles", "", time() - 3600, '/');
  153. }