apachesolr_access.module 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Implements hook_apachesolr_index_document_build_node()
  4. *
  5. * Add node access grants of generic view grants if node access is not used.
  6. *
  7. * @param $document
  8. * The document to add our node access information to
  9. * @param $node
  10. * The node which is used to built the document from
  11. * @param $env_id
  12. * The environment for which we are building the document. This parameter does not have any effect in
  13. * this code so it can be ignored
  14. */
  15. function apachesolr_access_apachesolr_index_document_build_node(ApacheSolrDocument $document, $node, $env_id) {
  16. $account = &drupal_static(__FUNCTION__);
  17. if (!isset($account)) {
  18. // Load the anonymous user.
  19. $account = drupal_anonymous_user();
  20. }
  21. // When using a node access module like Domain Access which has
  22. // access grants that vary for anonymous users for the same content,
  23. // this variable should be set to 1. Note that doing so will prevent
  24. // any results from being returned if using apachesolr_multisitesearch
  25. // from a different site.
  26. $always_add = apachesolr_environment_variable_get($env_id, 'apachesolr_access_always_add_grants', 0);
  27. if ($always_add || !node_access('view', $node, $account)) {
  28. // Get node access grants.
  29. $result = db_query('SELECT * FROM {node_access} WHERE (nid = 0 OR nid = :nid) AND grant_view = 1', array(':nid' => $node->nid));
  30. foreach ($result as $grant) {
  31. $grant_realm = apachesolr_access_clean_realm_name($grant->realm);
  32. $key = 'access_node_' . apachesolr_site_hash() . '_' . $grant_realm;
  33. $document->addField($key, $grant->gid);
  34. }
  35. }
  36. else {
  37. // Add the generic view grant if we are not using
  38. // node access or the node is viewable by anonymous users.
  39. // We assume we'll never have an entity with the name '__all'.
  40. $document->addField('access__all', 0);
  41. }
  42. }
  43. /**
  44. * Creates a Solr query for a given user
  45. *
  46. * @param $account
  47. * an account to get grants for and build a solr query
  48. *
  49. * @throws Exception
  50. *
  51. * @return SolrFilterSubQuery
  52. * Instance of SolrFilterSubQuery
  53. */
  54. function apachesolr_access_build_subquery($account) {
  55. if (!user_access('access content', $account)) {
  56. throw new Exception('No access');
  57. }
  58. $node_access_query = apachesolr_drupal_subquery();
  59. if (user_access('bypass node access', $account)) {
  60. // Access all content from the current site.
  61. $node_access_query->addFilter('hash', apachesolr_site_hash());
  62. }
  63. else {
  64. // Get node access grants.
  65. $grants = node_access_grants('view', $account);
  66. foreach ($grants as $realm => $gids) {
  67. $realm = apachesolr_access_clean_realm_name($realm);
  68. foreach ($gids as $gid) {
  69. $node_access_query->addFilter('access_node_' . apachesolr_site_hash() . '_' . $realm, $gid);
  70. }
  71. }
  72. }
  73. // Everyone can access public content. Note that if the variable
  74. // 'apachesolr_access_always_add_grants' is TRUE, no content from this site
  75. // is considered "public". However, this condition may match documents in
  76. // the Solr index supplied by other sites when multiple sites are indexing
  77. // into the same index , i.e. multisite search.
  78. $node_access_query->addFilter('access__all', 0);
  79. return $node_access_query;
  80. }
  81. /**
  82. * Implements hook_apachesolr_query_alter().
  83. *
  84. * Alter the query to include the access subquery
  85. *
  86. * @param DrupalSolrQueryInterface $query
  87. *
  88. */
  89. function apachesolr_access_apachesolr_query_alter(DrupalSolrQueryInterface $query) {
  90. global $user;
  91. try {
  92. $subquery = apachesolr_access_build_subquery($user);
  93. $query->addFilterSubQuery($subquery);
  94. }
  95. catch (Exception $e) {
  96. watchdog("apachesolr_access", 'User %name (UID:!uid) cannot search: @message', array('%name' => $user->name, '!uid' => $user->uid, '@message' => $e->getMessage()));
  97. $query->abort_search = TRUE;
  98. }
  99. }
  100. /**
  101. * Implements hook_node_insert().
  102. *
  103. * hook_node_ACTION() is called before hook_node_access_records() in node_save().
  104. *
  105. * @param object $node
  106. */
  107. function apachesolr_access_node_insert($node) {
  108. $node->apachesolr_access_node_ignore = 1;
  109. }
  110. /**
  111. * Implements hook_node_update().
  112. *
  113. * hook_node_ACTION() is called before hook_node_access_records() in node_save().
  114. *
  115. * @param object $node
  116. */
  117. function apachesolr_access_node_update($node) {
  118. $node->apachesolr_access_node_ignore = 1;
  119. }
  120. /**
  121. * Implements hook_node_access_records().
  122. *
  123. * Listen to this hook to find out when a node needs to be re-indexed
  124. * for its node access grants.
  125. *
  126. * @param object $node
  127. */
  128. function apachesolr_access_node_access_records($node) {
  129. // node_access_needs_rebuild() will usually be TRUE during a
  130. // full rebuild.
  131. if (empty($node->apachesolr_access_node_ignore) && !node_access_needs_rebuild()) {
  132. // Only one node is being changed - mark for re-indexing.
  133. apachesolr_mark_entity('node', $node->nid);
  134. }
  135. }
  136. /**
  137. * Implements hook_form_alter().
  138. *
  139. * @param array $form
  140. * @param array $form_state
  141. * @param string $form_id
  142. *
  143. */
  144. function apachesolr_access_form_node_configure_rebuild_confirm_alter(&$form, $form_state, $form_id) {
  145. $form['#submit'][] = 'apachesolr_access_rebuild_nodeaccess';
  146. }
  147. /**
  148. * Implements hook_form_FORM_ID_alter().
  149. */
  150. function apachesolr_access_form_apachesolr_environment_edit_form_alter(&$form, $form_state) {
  151. $form['conf']['apachesolr_access_always_add_grants'] = array(
  152. '#type' => 'checkbox',
  153. '#title' => t('Add access grants even for public content'),
  154. '#default_value' => empty($form['#environment']['conf']['apachesolr_access_always_add_grants']) ? 0 : 1,
  155. '#description' => t('Normally should be disabled. Changing this value requires all content to be re-indexed. Useful for sites using Domamin Access or simliar node acess modules with grants that vary for anonymous users.'),
  156. );
  157. $form['actions']['save']['#submit'][] = 'apachesolr_access_environment_edit_form_submit';
  158. $form['actions']['save_edit']['#submit'][] = 'apachesolr_access_environment_edit_form_submit';
  159. }
  160. /**
  161. * Added button-level form submit function for apachesolr_environment_edit_form.
  162. */
  163. function apachesolr_access_environment_edit_form_submit($form, &$form_state) {
  164. $prior = empty($form['#environment']['conf']['apachesolr_access_always_add_grants']) ? 0 : 1;
  165. if ($form_state['values']['conf']['apachesolr_access_always_add_grants'] != $prior) {
  166. apachesolr_access_enable();
  167. }
  168. }
  169. /**
  170. * Force Solr to do a total re-index when node access rules change.
  171. *
  172. * This is unfortunate because not every node is going to be affected, but
  173. * there is little we can do.
  174. *
  175. * @param $form
  176. * @param $form_state
  177. *
  178. */
  179. function apachesolr_access_rebuild_nodeaccess($form, $form_state) {
  180. drupal_set_message(t('Solr search index will be rebuilt.'));
  181. // Clear last updated
  182. apachesolr_clear_last_index_position();
  183. }
  184. /**
  185. * Implements hook_enable().
  186. *
  187. * On enabling the module, tell the user to reindex
  188. */
  189. function apachesolr_access_enable() {
  190. drupal_set_message(t('Your content <a href="@url">must be re-indexed</a> before Apache Solr Access will be functional on searches.', array('@url' => url('admin/config/search/apachesolr/index'))), 'warning');
  191. }
  192. /**
  193. * Helper function - return a safe (PHP identifier) realm name.
  194. *
  195. * @todo See if we can replace this with a native php function
  196. *
  197. * @param string $realm
  198. *
  199. * @return string
  200. * Clean string without bad characters
  201. */
  202. function apachesolr_access_clean_realm_name($realm) {
  203. return preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '_', $realm);
  204. }