You are here

function apachesolr_nodeaccess_apachesolr_update_index in Apache Solr Search 5.2

Same name and namespace in other branches
  1. 5 contrib/apachesolr_nodeaccess/apachesolr_nodeaccess.module \apachesolr_nodeaccess_apachesolr_update_index()
  2. 6 contrib/apachesolr_nodeaccess/apachesolr_nodeaccess.module \apachesolr_nodeaccess_apachesolr_update_index()
  3. 6.2 contrib/apachesolr_nodeaccess/apachesolr_nodeaccess.module \apachesolr_nodeaccess_apachesolr_update_index()

Implementation of apachesolr_update_index

File

contrib/apachesolr_nodeaccess/apachesolr_nodeaccess.module, line 6

Code

function apachesolr_nodeaccess_apachesolr_update_index(&$document, $node, $namespace) {
  static $anonymous_account;
  global $user;

  // We have to run as anonymous while node_access() doesn't take the $user as
  // argument but we want to check the access as anonymous. We are using
  // http://drupal.org/node/218104 as a trick to safe switch to anonymous.
  if ($user->uid) {
    if (!isset($anonymous_account)) {

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

    // Get node access grants.
    $result = db_query('SELECT * FROM {node_access} WHERE (nid = 0 OR nid = %d) AND grant_view = 1', $node->nid);
    while ($grant = db_fetch_object($result)) {
      $key = 'nodeaccess_' . 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.
    $document
      ->setMultiValue('nodeaccess_all', 0);
  }

  // Switch back the original user
  if (isset($original_user)) {
    $user = $original_user;
    session_save_session(TRUE);
  }
}