content_access.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * @file Content access administration UI.
  4. */
  5. /**
  6. * Specifies the threshold until we try to mass update node grants immediately.
  7. */
  8. define('CONTENT_ACCESS_MASS_UPDATE_THRESHOLD', 1000);
  9. /**
  10. * Per node settings page.
  11. */
  12. function content_access_page($form, &$form_state, $node) {
  13. drupal_set_title(t('Access control for @title', array('@title' => $node->title)));
  14. foreach (_content_access_get_operations() as $op => $label) {
  15. $defaults[$op] = content_access_per_node_setting($op, $node);
  16. }
  17. // Get roles form
  18. content_access_role_based_form($form, $defaults, $node->type);
  19. // Add an after_build handler that disables checkboxes, which are enforced by permissions.
  20. $form['per_role']['#after_build'] = array('content_access_force_permissions');
  21. // ACL form
  22. if (module_exists('acl')) {
  23. // This is disabled when there is no node passed.
  24. $form['acl'] = array(
  25. '#type' => 'fieldset',
  26. '#title' => t('User access control lists'),
  27. '#description' => t('These settings allow you to grant access to specific users.'),
  28. '#collapsible' => TRUE,
  29. '#tree' => TRUE,
  30. );
  31. foreach (array('view', 'update', 'delete') as $op) {
  32. $acl_id = content_access_get_acl_id($node, $op);
  33. acl_node_add_acl($node->nid, $acl_id, (int) ($op == 'view'), (int) ($op == 'update'), (int) ($op == 'delete'), content_access_get_settings('priority', $node->type));
  34. $form['acl'][$op] = acl_edit_form($form_state, $acl_id, t('Grant !op access', array('!op' => $op)));
  35. $form['acl'][$op]['#collapsed'] = !isset($_POST['acl_' . $acl_id]) && !unserialize($form['acl'][$op]['user_list']['#default_value']);
  36. }
  37. }
  38. $form_state['node'] = $node;
  39. $form['reset'] = array(
  40. '#type' => 'submit',
  41. '#value' => t('Reset to defaults'),
  42. '#weight' => 10,
  43. '#submit' => array('content_access_page_reset'),
  44. '#access' => count(content_access_get_per_node_settings($node)) > 0,
  45. );
  46. $form['submit'] = array(
  47. '#type' => 'submit',
  48. '#value' => t('Submit'),
  49. '#weight' => 10,
  50. );
  51. // @todo not true anymore?
  52. // http://drupal.org/update/modules/6/7#hook_node_access_records
  53. if (!$node->status) {
  54. drupal_set_message(t("Warning: Your content is not published, so this settings are not taken into account as long as the content remains unpublished."), 'error');
  55. }
  56. return $form;
  57. }
  58. /**
  59. * Submit callback for content_access_page().
  60. */
  61. function content_access_page_submit($form, &$form_state) {
  62. $settings = array();
  63. $node = $form_state['node'];
  64. foreach (_content_access_get_operations() as $op => $label) {
  65. // Set the settings so that further calls will return this settings.
  66. $settings[$op] = array_keys(array_filter($form_state['values'][$op]));
  67. }
  68. // Save per-node settings.
  69. content_access_save_per_node_settings($node, $settings);
  70. if (module_exists('acl')) {
  71. foreach (array('view', 'update', 'delete') as $op) {
  72. acl_save_form($form_state['values']['acl'][$op]);
  73. module_invoke_all('user_acl', $settings);
  74. }
  75. }
  76. // Apply new settings.
  77. node_access_acquire_grants($node);
  78. module_invoke_all('per_node', $settings);
  79. drupal_set_message(t('Your changes have been saved.'));
  80. }
  81. /**
  82. * Submit callback for reset on content_access_page().
  83. */
  84. function content_access_page_reset($form, &$form_state) {
  85. content_access_delete_per_node_settings($form_state['node']);
  86. node_access_acquire_grants($form_state['node']);
  87. drupal_set_message(t('The permissions have been reseted to the content type defaults.'));
  88. }
  89. /**
  90. * Per content type settings form.
  91. */
  92. function content_access_admin_settings($form, &$form_state, $content_type) {
  93. $type = $content_type->type;
  94. $form_state['type'] = $type;
  95. // Add role based per content type settings
  96. $defaults = array();
  97. foreach (_content_access_get_operations() as $op => $label) {
  98. $defaults[$op] = content_access_get_settings($op, $type);
  99. }
  100. content_access_role_based_form($form, $defaults, $type);
  101. // Per node:
  102. $form['node'] = array(
  103. '#type' => 'fieldset',
  104. '#title' => t('Per content node access control settings'),
  105. '#collapsible' => TRUE,
  106. '#description' => t('Optionally you can enable per content node access control settings. If enabled, a new tab for the content access settings appears when viewing content. You have to configure permission to access these settings at the !permissions page.', array('!permissions' => l(t('permissions'), 'admin/people/permissions'))),
  107. );
  108. $form['node']['per_node'] = array(
  109. '#type' => 'checkbox',
  110. '#title' => t('Enable per content node access control settings'),
  111. '#default_value' => content_access_get_settings('per_node', $type),
  112. );
  113. $form['advanced'] = array(
  114. '#type' => 'fieldset',
  115. '#title' => t('Advanced'),
  116. '#collapsible' => TRUE,
  117. '#collapsed' => TRUE,
  118. );
  119. $form['advanced']['priority'] = array(
  120. '#type' => 'weight',
  121. '#title' => t('Give content node grants priority'),
  122. '#default_value' => content_access_get_settings('priority', $type),
  123. '#description' => t('If you are only using this access control module, you can safely ignore this. If you are using multiple access control modules you can adjust the priority of this module.'),
  124. );
  125. $form['submit'] = array(
  126. '#type' => 'submit',
  127. '#value' => t('Submit'),
  128. '#weight' => 10,
  129. );
  130. return $form;
  131. }
  132. /**
  133. * Submit handler for per content type settings form.
  134. */
  135. function content_access_admin_settings_submit($form, &$form_state) {
  136. $roles_permissions = user_role_permissions(user_roles());
  137. $permissions = user_permission_get_modules();
  138. $type = $form_state['type'];
  139. // Remove disabled modules permissions, so they can't raise exception
  140. // in content_access_save_permissions()
  141. foreach ($roles_permissions as $rid => $role_permissions) {
  142. foreach ($role_permissions as $permission => $value) {
  143. if (!array_key_exists($permission, $permissions)) {
  144. unset($roles_permissions[$rid][$permission]);
  145. }
  146. }
  147. }
  148. foreach (array('update', 'update_own', 'delete', 'delete_own') as $op) {
  149. foreach ($form_state['values'][$op] as $rid => $value) {
  150. $permission = content_access_get_permission_by_op($op, $form_state['type']);
  151. if ($value) {
  152. $roles_permissions[$rid][$permission] = TRUE;
  153. }
  154. else {
  155. $roles_permissions[$rid][$permission] = FALSE;
  156. }
  157. }
  158. // Don't save the setting, so its default value (get permission) is applied
  159. // always.
  160. unset($form_state['values'][$op]);
  161. }
  162. content_access_save_permissions($roles_permissions);
  163. // Update content access settings
  164. $settings = content_access_get_settings('all', $type);
  165. foreach (content_access_available_settings() as $setting) {
  166. if (isset($form_state['values'][$setting])) {
  167. $settings[$setting] = is_array($form_state['values'][$setting]) ? array_keys(array_filter($form_state['values'][$setting])) : $form_state['values'][$setting];
  168. }
  169. }
  170. content_access_set_settings($settings, $type);
  171. // Mass update the nodes, but only if necessary.
  172. if (content_access_get_settings('per_node', $type) ||
  173. content_access_get_settings('view', $type) != $form['per_role']['view']['#default_value'] ||
  174. content_access_get_settings('view_own', $type) != $form['per_role']['view_own']['#default_value'] ||
  175. content_access_get_settings('priority', $type) != $form['advanced']['priority']['#default_value'] ||
  176. content_access_get_settings('per_node', $type) != $form['node']['per_node']['#default_value']
  177. ) {
  178. // If per node has been disabled and we use the ACL integration, we have to remove possible ACLs now.
  179. if (!content_access_get_settings('per_node', $type) && $form['node']['per_node']['#default_value'] && module_exists('acl')) {
  180. _content_access_remove_acls($type);
  181. }
  182. if (content_access_mass_update(array($type))) {
  183. drupal_set_message(t('Permissions have been successfully rebuilt for the content type @types.', array('@types' => node_type_get_name($type))));
  184. }
  185. }
  186. drupal_set_message(t('Your changes have been saved.'));
  187. }
  188. /**
  189. * Mass updates node access records for nodes of the given types.
  190. * @param $types
  191. * An array of content type names.
  192. * @return
  193. * Whether the operation has been processed successfully (TRUE) or postponed (FALSE).
  194. */
  195. function content_access_mass_update($types) {
  196. $q = db_select('node', 'n')
  197. ->fields('n', array('nid'))
  198. ->condition('n.type', $types, 'IN');
  199. $count = $q->countQuery()->execute()->fetchField();
  200. node_access_needs_rebuild(TRUE);
  201. // If there not too much nodes affected, try to do it.
  202. if ($count <= CONTENT_ACCESS_MASS_UPDATE_THRESHOLD) {
  203. $records = $q->execute();
  204. foreach ($records as $node) {
  205. node_access_acquire_grants(node_load($node->nid));
  206. }
  207. cache_clear_all();
  208. node_access_needs_rebuild(FALSE);
  209. return TRUE;
  210. }
  211. return FALSE;
  212. }
  213. /**
  214. * Saves the given permissions by role to the database.
  215. */
  216. function content_access_save_permissions($roles_permissions) {
  217. foreach ($roles_permissions as $rid => $permissions) {
  218. user_role_change_permissions($rid, $permissions);
  219. }
  220. }
  221. /**
  222. * Builds the role based permission form for the given defaults.
  223. *
  224. * @param $defaults
  225. * Array of defaults for all operations.
  226. */
  227. function content_access_role_based_form(&$form, $defaults = array(), $type = NULL) {
  228. $form['per_role'] = array(
  229. '#type' => 'fieldset',
  230. '#title' => t('Role based access control settings'),
  231. '#collapsible' => TRUE,
  232. '#description' => t('Note that users need at least the %access_content permission to be able to deal in any way with content.', array('%access_content' => t('access content'))) .
  233. ' ' . t('Furthermore note that content which is not @published is treated in a different way by drupal: It can be viewed only by its author or users with the %administer_nodes permission.', array('@published' => t('published'), '%administer_nodes' => t('administer nodes'))),
  234. );
  235. $operations = _content_access_get_operations($type);
  236. $roles = array_map('filter_xss_admin', user_roles());
  237. foreach ($operations as $op => $label) {
  238. // Make sure defaults are set properly
  239. $defaults += array($op => array());
  240. $form['per_role'][$op] = array('#type' => 'checkboxes',
  241. '#prefix' => '<div class="content_access-div">',
  242. '#suffix' => '</div>',
  243. '#options' => $roles,
  244. '#title' => $label,
  245. '#default_value' => $defaults[$op],
  246. '#process' => array('form_process_checkboxes', 'content_access_disable_checkboxes'),
  247. );
  248. }
  249. $form['per_role']['clearer'] = array(
  250. '#value' => '<br clear="all" />',
  251. );
  252. drupal_add_css(drupal_get_path('module', 'content_access') . '/content_access.css');
  253. return $form;
  254. }
  255. /**
  256. * Formapi #after_build callback, that disables checkboxes for roles without access to content.
  257. */
  258. function content_access_force_permissions($element, &$form_state) {
  259. foreach (array('update', 'update_own', 'delete', 'delete_own') as $op) {
  260. foreach (content_access_get_settings($op, $form_state['node']->type) as $rid) {
  261. $element[$op][$rid]['#disabled'] = TRUE;
  262. $element[$op][$rid]['#attributes']['disabled'] = 'disabled';
  263. $element[$op][$rid]['#value'] = TRUE;
  264. $element[$op][$rid]['#checked'] = TRUE;
  265. $element[$op][$rid]['#prefix'] = '<span' . drupal_attributes(array('title' => t("Permission is granted due to the content type's access control settings."))) . '>';
  266. $element[$op][$rid]['#suffix'] = "</span>";
  267. }
  268. }
  269. return $element;
  270. }
  271. /**
  272. * Submit callback for the user permissions form.
  273. * Trigger changes to node permissions to rebuild our grants.
  274. */
  275. function content_access_user_admin_perm_submit($form, $form_state) {
  276. // Check for each content type, which has per node access activated
  277. // whether permissions have been changed.
  278. $types = array();
  279. foreach (array_filter(content_access_get_settings('per_node')) as $type => $value) {
  280. foreach (_content_access_get_node_permissions($type) as $perm) {
  281. foreach (user_roles() as $rid => $role) {
  282. if (isset($form_state['values'][$rid]) && in_array($perm, $form['checkboxes'][$rid]['#default_value']) != in_array($perm, $form_state['values'][$rid])) {
  283. //permission changed!
  284. $types[$type] = node_get_types('name', $type);
  285. continue 2;
  286. }
  287. }
  288. }
  289. }
  290. if ($types && content_access_mass_update(array_keys($types))) {
  291. drupal_set_message(format_plural(count($types),
  292. 'Permissions have been successfully rebuilt for the content type @types.',
  293. 'Permissions have been successfully rebuilt for the content types @types.',
  294. array('@types' => implode(', ', $types))
  295. ));
  296. }
  297. }