You are here

function tac_lite_query_term_access_alter in Taxonomy Access Control Lite 8

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

Implements hook_query_TAG_alter().

File

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

Code

function tac_lite_query_term_access_alter(AlterableInterface $query) {
  $account = \Drupal::currentUser();

  // If this user has administer rights, don't filter.
  if (\Drupal::currentUser()
    ->hasPermission('administer tac_lite')) {
    return;
  }

  // Get our vocabularies and schemes from variables. Return if we have none.
  $settings = \Drupal::config('tac_lite.settings');
  $vids = $settings
    ->get('tac_lite_categories');
  $schemes = $settings
    ->get('tac_lite_schemes');
  if (!$vids || !count($vids) || !$schemes) {
    return;
  }

  // The terms this user is allowed to see.
  $term_visibility = FALSE;
  $tids = [];
  for ($i = 1; $i <= $schemes; $i++) {
    $config = SchemeForm::tacLiteConfig($i);
    if ($config['term_visibility']) {
      $tids = array_merge($tids, _tac_lite_user_tids($account, $i, $config));
      $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 $info) {
      $table = $info['table'];
      if ($table == 'taxonomy_term_data' || $table == 'taxonomy_term_field_data') {
        $primary_table = $info['alias'];
      }
    }

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