I'm not very familier (yet) with Drupal node access stuff...

I'm using search_api module with view_unpublished.
I need to display unpublished nodes from a search_api query.

Like the 'Published or admin' features for Views, search_api provides a way to 'Add node access information to the index' in order to filter results.
But it does not work. I don't know if it comes from view_unpublished or search_api...

Here is the code where search_api add node_access info (callback_node_access.inc). But in my case node_access('view', $item, $account) always return true, so it does not query node_access table.

/**
 * Alter items before indexing.
 *
 * Items which are removed from the array won't be indexed, but will be marked
 * as clean for future indexing. This could for instance be used to implement
 * some sort of access filter for security purposes (e.g., don't index
 * unpublished nodes or comments).
 *
 * @param array $items
 *   An array of items to be altered, keyed by item IDs.
 */
public function alterItems(array &$items) {
  static $account;

  if (!isset($account)) {
    // Load the anonymous user.
    $account = drupal_anonymous_user();
  }

  foreach ($items as $nid => &$item) {
    // Check whether all users have access to the node.
    if (!node_access('view', $item, $account)) {
      // Get node access grants.
      $result = db_query('SELECT * FROM {node_access} WHERE (nid = 0 OR nid = :nid) AND grant_view = 1', array(':nid' => $item->nid));

      // Store all grants together with it's realms in the item.
      foreach ($result as $grant) {
        if (!isset($items[$nid]->search_api_access_node)) {
          $items[$nid]->search_api_access_node = array();
        }
        $items[$nid]->search_api_access_node[] = "node_access_$grant->realm:$grant->gid";
      }
    }
    else {
      // Add the generic view grant if we are not using node access or the
      // node is viewable by anonymous users.
      $items[$nid]->search_api_access_node = array('node_access__all');
    }
  }
}

Any idea ?

Did someone use view_unpublished module with search_api to filter indexed nodes?

Comments

cthiebault’s picture

I've also created an issue for search_api as it seems that they do not support view_unpublished permissions:
http://drupal.org/node/1617794

entendu’s picture

I don't know anything about search_api, but it looks like you got it to work with a patch over there. Is that true?

cthiebault’s picture

It seems to work but I'm not a pro with Drupal permissions neither search_api code.
If someone used to view_unpublished could validate the patch it would be helpful...

entendu’s picture

Status: Active » Closed (works as designed)

Marking this closed in favor of the search_api patch.