View source
<?php
function apachesolr_access_apachesolr_index_document_build_node(ApacheSolrDocument $document, $node, $env_id) {
$account =& drupal_static(__FUNCTION__);
if (!isset($account)) {
$account = drupal_anonymous_user();
}
$always_add = apachesolr_environment_variable_get($env_id, 'apachesolr_access_always_add_grants', 0);
if ($always_add || !node_access('view', $node, $account)) {
$result = db_query('SELECT * FROM {node_access} WHERE (nid = 0 OR nid = :nid) AND grant_view = 1', array(
':nid' => $node->nid,
));
foreach ($result as $grant) {
$grant_realm = apachesolr_access_clean_realm_name($grant->realm);
$key = 'access_node_' . apachesolr_site_hash() . '_' . $grant_realm;
$document
->addField($key, $grant->gid);
}
}
else {
$document
->addField('access__all', 0);
}
}
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('bypass node access', $account)) {
$node_access_query
->addFilter('hash', apachesolr_site_hash());
}
else {
$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;
}
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;
}
}
function apachesolr_access_node_insert($node) {
$node->apachesolr_access_node_ignore = 1;
}
function apachesolr_access_node_update($node) {
$node->apachesolr_access_node_ignore = 1;
}
function apachesolr_access_node_access_records($node) {
if (empty($node->apachesolr_access_node_ignore) && !node_access_needs_rebuild()) {
apachesolr_mark_entity('node', $node->nid);
}
}
function apachesolr_access_form_node_configure_rebuild_confirm_alter(&$form, $form_state, $form_id) {
$form['#submit'][] = 'apachesolr_access_rebuild_nodeaccess';
}
function apachesolr_access_form_apachesolr_environment_edit_form_alter(&$form, $form_state) {
$form['conf']['apachesolr_access_always_add_grants'] = array(
'#type' => 'checkbox',
'#title' => t('Add access grants even for public content'),
'#default_value' => empty($form['#environment']['conf']['apachesolr_access_always_add_grants']) ? 0 : 1,
'#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.'),
);
$form['actions']['save']['#submit'][] = 'apachesolr_access_environment_edit_form_submit';
$form['actions']['save_edit']['#submit'][] = 'apachesolr_access_environment_edit_form_submit';
}
function apachesolr_access_environment_edit_form_submit($form, &$form_state) {
$prior = empty($form['#environment']['conf']['apachesolr_access_always_add_grants']) ? 0 : 1;
if ($form_state['values']['conf']['apachesolr_access_always_add_grants'] != $prior) {
apachesolr_access_enable();
}
}
function apachesolr_access_rebuild_nodeaccess($form, $form_state) {
drupal_set_message(t('Solr search index will be rebuilt.'));
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/config/search/apachesolr/index'),
)), 'warning');
}
function apachesolr_access_clean_realm_name($realm) {
return preg_replace('/[^a-zA-Z0-9_\\x7f-\\xff]/', '_', $realm);
}