node_access.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon node type.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t("Node: accessible"),
  12. 'description' => t('Control access with built in Drupal node access test.'),
  13. 'callback' => 'ctools_node_access_ctools_access_check',
  14. 'default' => array('type' => 'view'),
  15. 'settings form' => 'ctools_node_access_ctools_access_settings',
  16. 'settings form submit' => 'ctools_node_access_ctools_access_settings_submit',
  17. 'summary' => 'ctools_node_access_ctools_access_summary',
  18. 'required context' => array(
  19. new ctools_context_required(t('User'), 'user'),
  20. new ctools_context_required(t('Node'), 'node'),
  21. ),
  22. );
  23. /**
  24. * Settings form for the 'by node_access' access plugin
  25. */
  26. function ctools_node_access_ctools_access_settings($form, &$form_state, $conf) {
  27. $form['settings']['type'] = array(
  28. '#title' => t('Operation'),
  29. '#type' => 'radios',
  30. '#options' => array(
  31. 'view' => t('View'),
  32. 'update' => t('Update'),
  33. 'delete' => t('Delete'),
  34. 'create' => t('Create nodes of the same type'),
  35. ),
  36. '#description' => t('Using built in Drupal node access rules, determine if the user can perform the selected operation on the node.'),
  37. '#default_value' => $conf['type'],
  38. );
  39. return $form;
  40. }
  41. /**
  42. * Check for access.
  43. */
  44. function ctools_node_access_ctools_access_check($conf, $context) {
  45. // As far as I know there should always be a context at this point, but this
  46. // is safe.
  47. list($user_context, $node_context) = $context;
  48. if (empty($node_context) || empty($node_context->data) || empty($node_context->data->type)) {
  49. return FALSE;
  50. }
  51. if (empty($user_context) || empty($user_context->data)) {
  52. return FALSE;
  53. }
  54. if ($conf['type'] == 'create') {
  55. return node_access('create', $node_context->data->type, $user_context->data);
  56. }
  57. else {
  58. return node_access($conf['type'], $node_context->data, $user_context->data);
  59. }
  60. }
  61. /**
  62. * Provide a summary description based upon the checked node_accesss.
  63. */
  64. function ctools_node_access_ctools_access_summary($conf, $context) {
  65. list($user_context, $node_context) = $context;
  66. $replacement = array('@user' => $user_context->identifier, '@node' => $node_context->identifier);
  67. switch ($conf['type']) {
  68. case 'view':
  69. return t('@user can view @node.', $replacement);
  70. case 'update':
  71. return t('@user can edit @node.', $replacement);
  72. case 'delete':
  73. return t('@user can delete @node.', $replacement);
  74. case 'create':
  75. return t('@user can create nodes of the same type as @node.', $replacement);
  76. }
  77. }