View source
<?php
function apachesolr_nodeaccess_apachesolr_update_index(&$document, $node, $namespace) {
static $account;
if (!isset($account)) {
$account = drupal_anonymous_user();
}
if (!node_access('view', $node, $account)) {
$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 {
$document
->setMultiValue('nodeaccess_all', 0);
}
}
function _apachesolr_nodeaccess_build_subquery($account) {
if (!user_access('access content', $account)) {
throw new Exception('No access');
}
$node_access_query = apachesolr_drupal_query();
if (empty($node_access_query)) {
throw new Exception('No query object in apachesolr_nodeaccess');
}
if (user_access('administer nodes', $account)) {
$node_access_query
->add_filter('nodeaccess_all', 0);
$node_access_query
->add_filter('hash', apachesolr_site_hash());
}
else {
$grants = node_access_grants('view', $account);
foreach ($grants as $realm => $gids) {
foreach ($gids as $gid) {
$node_access_query
->add_filter('nodeaccess_' . apachesolr_site_hash() . '_' . $realm, $gid);
}
}
$node_access_query
->add_filter('nodeaccess_all', 0);
}
return $node_access_query;
}
function apachesolr_nodeaccess_apachesolr_modify_query(&$query, &$params, $caller) {
global $user;
try {
$subquery = _apachesolr_nodeaccess_build_subquery($user);
} catch (Exception $e) {
$query = NULL;
watchdog("apachesolr_nodeaccess", 'User %name (UID:!uid) cannot search: @message', array(
'%name' => $user->name,
'!uid' => $user->uid,
'@message' => $e
->getMessage(),
));
return;
}
if (!empty($subquery)) {
$query
->add_subquery($subquery, 'OR');
}
}
function apachesolr_nodeaccess_nodeapi(&$node, $op) {
switch ($op) {
case 'insert':
case 'update':
$node->apachesolr_nodeaccess_ignore = 1;
break;
}
}
function apachesolr_nodeaccess_node_access_records($node) {
if (empty($node->apachesolr_nodeaccess_ignore) && !node_access_needs_rebuild()) {
apachesolr_mark_node($node->nid);
}
}
function apachesolr_nodeaccess_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_configure_rebuild_confirm') {
$form['#submit'][] = 'apachesolr_nodeaccess_rebuild_nodeaccess';
}
}
function apachesolr_nodeaccess_rebuild_nodeaccess(&$form, $form_state) {
drupal_set_message(t('Solr search index will be rebuilt.'));
apachesolr_clear_last_index();
}
function apachesolr_nodeaccess_enable() {
drupal_set_message(t('Your content <a href="@url">must be re-indexed</a> before Apache Solr node access will be functional on searches.', array(
'@url' => url('admin/settings/apachesolr/index'),
)), 'warning');
}