You are here

function taxonomy_permissions_query_term_access_alter in Taxonomy Permissions 7

Implements hook_query_term_access_alter().

File

./taxonomy_permissions.module, line 64
taxonomy_permissions.module

Code

function taxonomy_permissions_query_term_access_alter(QueryAlterableInterface $query) {
  global $user;
  $uid = $user->uid;
  $vids_by_user =& drupal_static(__FUNCTION__, array());
  if (!isset($vids_by_user[$uid])) {

    // Save the vids that $user is allowed to access.
    $vids = array();
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $vid => $vocabulary) {
      if (user_access(taxonomy_permissions_permission_name($vocabulary))) {
        $vids[] = $vid;
      }
    }
    $vids_by_user[$uid] = $vids;
  }

  // Restrict {taxonomy_term_data} to the allowed vocabularies only.
  $tables = $query
    ->getTables();
  foreach ($tables as $table) {
    if ($table['table'] == 'taxonomy_term_data') {
      $or = db_or();
      if (!empty($vids_by_user[$uid])) {
        $or
          ->condition($table['alias'] . '.vid', $vids_by_user[$uid], 'IN');
      }
      $or
        ->isNull($table['alias'] . '.vid');
      $query
        ->condition($or);
    }
  }
}