You are here

apachesolr_access.module in Apache Solr Search 6.3

File

apachesolr_access/apachesolr_access.module
View source
<?php

/**
 * Implements hook_apachesolr_index_document_build_node()
 */
function apachesolr_access_apachesolr_index_document_build_node($document, $node, $env_id) {
  static $account;
  if (!isset($account)) {

    // Load the anonymous user.
    $account = drupal_anonymous_user();
  }
  if (!node_access('view', $node, $account)) {

    // Get node access grants.
    $query = "SELECT * FROM {node_access} WHERE (nid = 0 OR nid = %d AND grant_view = 1)";
    $result = db_query($query, array(
      $node->nid,
    ));
    while ($grant = db_fetch_object($result)) {
      $grant_realm = apachesolr_access_clean_realm_name($grant->realm);
      $key = 'access_node_' . apachesolr_site_hash() . '_' . $grant_realm;
      $document
        ->setMultiValue($key, $grant->gid);
    }
  }
  else {

    // Add the generic view grant if we are not using
    // node access or the node is viewable by anonymous users.
    // We assume we'll never have an entity with the name '__all'.
    $document
      ->setMultiValue('access__all', 0);
  }
}

/**
 * Creates a Solr query for a given user
 *
 * @param $account an account to get grants for and build a solr query
 *
 * @throws Exception
 */
function apachesolr_access_build_subquery($account) {
  if (!user_access('access content', $account)) {
    throw new Exception('No access');
  }
  $node_access_query = apachesolr_drupal_subquery();
  if (user_access('administer nodes', $account)) {

    // Access all content from the current site, or public content.
    $node_access_query
      ->addFilter('access__all', 0);
    $node_access_query
      ->addFilter('hash', apachesolr_site_hash());
  }
  else {

    // Get node access grants.
    $grants = node_access_grants('view', $account);
    foreach ($grants as $realm => $gids) {
      $realm = apachesolr_access_clean_realm_name($realm);
      foreach ($gids as $gid) {
        $node_access_query
          ->addFilter('access_node_' . apachesolr_site_hash() . '_' . $realm, $gid);
      }
    }
    $node_access_query
      ->addFilter('access__all', 0);
  }
  return $node_access_query;
}

/**
 * Implements hook_apachesolr_query_alter().
 */
function apachesolr_access_apachesolr_query_alter(DrupalSolrQueryInterface $query) {
  global $user;
  try {
    $subquery = apachesolr_access_build_subquery($user);
    $query
      ->addFilterSubQuery($subquery);
  } catch (Exception $e) {
    watchdog("apachesolr_access", 'User %name (UID:!uid) cannot search: @message', array(
      '%name' => $user->name,
      '!uid' => $user->uid,
      '@message' => $e
        ->getMessage(),
    ));
    $query->abort_search = TRUE;
  }
}

/**
 * Implements hook_node_insert().
 * hook_node*() is called before hook_node_access_records() in node_save().
 */
function apachesolr_access_node_insert($node) {
  $node->apachesolr_access_node_ignore = 1;
}

/**
 * Implements hook_node_update().
 */
function apachesolr_access_node_update($node) {
  $node->apachesolr_access_node_ignore = 1;
}

/**
 * Implements hook_node_access_records().
 *
 * Listen to this hook to find out when a node needs to be re-indexed
 * for its node access grants.
 */
function apachesolr_access_node_access_records($node) {

  // node_access_needs_rebuild() will usually be TRUE during a
  // full rebuild.
  if (empty($node->apachesolr_access_node_ignore) && !node_access_needs_rebuild()) {

    // Only one node is being changed - mark for re-indexing.
    apachesolr_mark_entity('node', $node->nid);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function apachesolr_access_form_node_configure_alter(&$form, $form_state) {
  $form['access']['rebuild']['#submit'][] = 'apachesolr_access_rebuild_nodeaccess';
}

/**
 * Force Solr to do a total re-index when node access rules change.
 *
 * This is unfortunate because not every node is going to be affected, but
 * there is little we can do.
 */
function apachesolr_access_rebuild_nodeaccess(&$form, $form_state) {
  drupal_set_message(t('Solr search index will be rebuilt.'));

  // Clear last updated
  apachesolr_clear_last_index_position();
}
function apachesolr_access_enable() {
  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/settings/apachesolr/index'),
  )), 'warning');
}

/**
 * Helper function - return a safe (PHP identifier) realm name.
 */
function apachesolr_access_clean_realm_name($realm) {
  return preg_replace('/[^a-zA-Z0-9_\\x7f-\\xff]/', '_', $realm);
}

Functions

Namesort descending Description
apachesolr_access_apachesolr_index_document_build_node Implements hook_apachesolr_index_document_build_node()
apachesolr_access_apachesolr_query_alter Implements hook_apachesolr_query_alter().
apachesolr_access_build_subquery Creates a Solr query for a given user
apachesolr_access_clean_realm_name Helper function - return a safe (PHP identifier) realm name.
apachesolr_access_enable
apachesolr_access_form_node_configure_alter Implements hook_form_FORM_ID_alter().
apachesolr_access_node_access_records Implements hook_node_access_records().
apachesolr_access_node_insert Implements hook_node_insert(). hook_node*() is called before hook_node_access_records() in node_save().
apachesolr_access_node_update Implements hook_node_update().
apachesolr_access_rebuild_nodeaccess Force Solr to do a total re-index when node access rules change.