callback_node_access.inc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiAlterNodeAccess class.
  5. */
  6. /**
  7. * Adds node access information to node indexes.
  8. */
  9. class SearchApiAlterNodeAccess extends SearchApiAbstractAlterCallback {
  10. /**
  11. * Overrides SearchApiAbstractAlterCallback::supportsIndex().
  12. *
  13. * Returns TRUE only for indexes on nodes.
  14. */
  15. public function supportsIndex(SearchApiIndex $index) {
  16. // Currently only node access is supported.
  17. return $index->getEntityType() === 'node';
  18. }
  19. /**
  20. * Overrides SearchApiAbstractAlterCallback::propertyInfo().
  21. *
  22. * Adds the "search_api_access_node" property.
  23. */
  24. public function propertyInfo() {
  25. return array(
  26. 'search_api_access_node' => array(
  27. 'label' => t('Node access information'),
  28. 'description' => t('Data needed to apply node access.'),
  29. 'type' => 'list<token>',
  30. ),
  31. );
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function alterItems(array &$items) {
  37. static $account;
  38. if (!isset($account)) {
  39. // Load the anonymous user.
  40. $account = drupal_anonymous_user();
  41. }
  42. foreach ($items as $id => $item) {
  43. $node = $this->getNode($item);
  44. // Check whether all users have access to the node.
  45. if (!node_access('view', $node, $account)) {
  46. // Get node access grants.
  47. $result = db_query('SELECT * FROM {node_access} WHERE (nid = 0 OR nid = :nid) AND grant_view = 1', array(':nid' => $node->nid));
  48. // Store all grants together with their realms in the item.
  49. foreach ($result as $grant) {
  50. $items[$id]->search_api_access_node[] = "node_access_{$grant->realm}:{$grant->gid}";
  51. }
  52. }
  53. else {
  54. // Add the generic view grant if we are not using node access or the
  55. // node is viewable by anonymous users.
  56. $items[$id]->search_api_access_node = array('node_access__all');
  57. }
  58. }
  59. }
  60. /**
  61. * Retrieves the node related to a search item.
  62. *
  63. * In the default implementation for nodes, the item is already the node.
  64. * Subclasses may override this to easily provide node access checks for
  65. * items related to nodes.
  66. */
  67. protected function getNode($item) {
  68. return $item;
  69. }
  70. /**
  71. * Overrides SearchApiAbstractAlterCallback::configurationFormSubmit().
  72. *
  73. * If the data alteration is being enabled, set "Published" and "Author" to
  74. * "indexed", because both are needed for the node access filter.
  75. */
  76. public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
  77. $old_status = !empty($form_state['index']->options['data_alter_callbacks']['search_api_alter_node_access']['status']);
  78. $new_status = !empty($form_state['values']['callbacks']['search_api_alter_node_access']['status']);
  79. if (!$old_status && $new_status) {
  80. $form_state['index']->options['fields']['status']['type'] = 'boolean';
  81. $form_state['index']->options['fields']['author']['type'] = 'integer';
  82. $form_state['index']->options['fields']['author']['entity_type'] = 'user';
  83. }
  84. return parent::configurationFormSubmit($form, $values, $form_state);
  85. }
  86. }