You are here

function tac_lite_query_term_access_alter in Taxonomy Access Control Lite 7

Same name and namespace in other branches
  1. 8 tac_lite.module \tac_lite_query_term_access_alter()

Implements hook_query_TAG_alter().

Acts on queries that list terms (generally these should be tagged with 'term_access') to remove any terms that this user should not be able to see.

File

./tac_lite.module, line 692
Control access to site content based on taxonomy, roles and users.

Code

function tac_lite_query_term_access_alter(QueryAlterableInterface $query) {
  global $user;

  // If this user has administer rights, don't filter
  if (user_access('administer tac_lite')) {
    return;
  }

  // Get our vocabularies and schemes from variables. Return if we have none.
  $vids = variable_get('tac_lite_categories', NULL);
  $schemes = variable_get('tac_lite_schemes', 1);
  if (!$vids || !count($vids) || !$schemes) {
    return;
  }

  // the terms this user is allowed to see
  $term_visibility = FALSE;
  $tids = array();
  for ($i = 1; $i <= $schemes; $i++) {
    $config = _tac_lite_config($i);
    if ($config['term_visibility']) {
      $tids = array_merge($tids, _tac_lite_user_tids($user, $i));
      $term_visibility = TRUE;
    }
  }
  if ($term_visibility) {

    // HELP: What is the proper way to find the alias of the primary table here?
    $primary_table = '';
    $t = $query
      ->getTables();
    foreach ($t as $key => $info) {
      if (!$info['join type']) {
        $primary_table = $info['alias'];
      }
    }

    // Prevent query from finding terms the current user does not have permission to see.
    $query
      ->leftJoin('taxonomy_term_data', 'tac_td', $primary_table . '.tid = tac_td.tid');
    $or = db_or();
    $or
      ->condition($primary_table . '.tid', $tids, 'IN');
    $or
      ->condition('tac_td.vid', $vids, 'NOT IN');
    $query
      ->condition($or);
  }
}