You are here

function taxonomy_tools_build_grants in Taxonomy Tools 8

Same name and namespace in other branches
  1. 7 taxonomy_tools.module \taxonomy_tools_build_grants()

Builds node access grants for all user roles.

Parameters

stdClass $node: A node object.

Return value

array An array containing node access grants.

1 call to taxonomy_tools_build_grants()
taxonomy_tools_node_access_records in ./taxonomy_tools.module
Implements hook_node_access_records().

File

./taxonomy_tools.module, line 221
Drupal hooks and functions to work with taxonomy terms.

Code

function taxonomy_tools_build_grants($node) {
  $grants = array();

  // Get all terms associated with this node.
  $query = db_select('taxonomy_index', 'foo');
  $query
    ->addField('foo', 'tid');
  $query
    ->condition('foo.nid', $node->nid);
  $query
    ->join('taxonomy_term_data', 'bar', 'foo.tid = bar.tid');
  $query
    ->addField('bar', 'vid');
  $query
    ->join('taxonomy_vocabulary', 'baz', 'bar.vid = baz.vid');
  $query
    ->addField('baz', 'machine_name');
  $result = $query
    ->execute()
    ->fetchAll();

  // Get all user roles.
  $roles = user_roles();
  $roles = array_keys($roles);

  // Create node access grants for each user role.
  foreach ($roles as $rid) {
    $view = TRUE;
    if (!empty($result)) {

      // There are terms associated with this node.
      $permissions = taxonomy_tools_role_permissions_all($rid);
      if (module_exists('taxonomy_tools_publisher') && $view) {

        // If Taxonomy Publisher is enabled and node is visible to user role.
        $view = FALSE;
        $config = array_filter(variable_get('taxonomy_tools_publisher_config', array()));
        foreach ($result as $term_data) {
          if (!in_array($term_data->machine_name, $config) || in_array($term_data->machine_name, $config) && taxonomy_tools_publisher_check_branch($term_data->tid)) {

            // One of terms is not controlled by Taxonomy Publisher
            // or is controlled but is seen by user.
            $view = TRUE;
            break;
          }
        }
        if (!$view && in_array('bypass taxonomy publisher', $permissions)) {

          // None of associated terms is available to the user,
          // but user is allowed to see nodes from unpublished terms.
          $view = TRUE;
        }
      }
      if (module_exists('taxonomy_tools_role_access') && $view) {

        // If Taxonomy Role Access is enabled and node is visible to user role.
        $v_config = array_filter(variable_get('taxonomy_tools_role_access_vocab_config', array()));
        $r_config = variable_get('taxonomy_tools_role_access_role_config', array());
        if (in_array($rid, $r_config)) {
          $view = FALSE;

          // Only if user role is controlled by Taxonomy Role Access.
          foreach ($result as $term_data) {
            if (!in_array($term_data->machine_name, $v_config) || in_array($term_data->machine_name, $v_config) && taxonomy_tools_role_access_get_access($term_data->tid, $rid)) {

              // One of terms is not controlled by Taxonomy Role Access
              // or is controlled but is available to this user role.
              $view = TRUE;
              break;
            }
          }
          if (!$view && in_array('bypass taxonomy role access', $permissions)) {

            // None of associated terms is available to the user,
            // but user is allowed to see nodes from restricted terms.
            $view = TRUE;
          }
        }
      }
    }
    $grants[] = array(
      'realm' => 'taxonomy_tools',
      'gid' => $rid,
      'grant_view' => $view ? 1 : 0,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );
  }
  return $grants;
}