You are here

function _taxonomy_access_user_term_grants in Taxonomy Access Control 7

Retrieve terms that the current user may create or list.

Parameters

bool $create: (optional) Whether to fetch grants for create (TRUE) or list (FALSE). Defaults to FALSE.

array $vids: (optional) An array of vids to limit the query. Defaults to array().

object|null $account: (optional) The account for which to retrieve grants. If no account is passed, the current user will be used. Defaults to NULL.

Return value

array|true An array of term IDs, or TRUE if the user has the grant for all terms.

Related topics

3 calls to _taxonomy_access_user_term_grants()
taxonomy_access_user_create_terms in ./taxonomy_access.create.inc
Retrieve terms that the current user may create.
taxonomy_access_user_create_terms_by_vocab in ./taxonomy_access.create.inc
Retrieve terms that the current user may create in specific vocabularies.
taxonomy_access_user_list_terms in ./taxonomy_access.module
Retrieve terms that the current user may list.

File

./taxonomy_access.module, line 1504
Allows administrators to specify access control for taxonomy categories.

Code

function _taxonomy_access_user_term_grants($create = FALSE, array $vids = array(), $account = NULL) {
  $grant_type = $create ? 'create' : 'list';
  $grant_field_name = 'grant_' . $grant_type;

  // If no account was passed, default to current user.
  if (is_null($account)) {
    global $user;
    $account = $user;
  }

  // If the user can administer taxonomy, return TRUE for a global grant.
  if (user_access('administer taxonomy', $account)) {
    return TRUE;
  }

  // Build a term grant query.
  $query = _taxonomy_access_grant_query(array(
    $grant_type,
  ));

  // Select term grants for the user's roles.
  $query
    ->fields('td', array(
    'tid',
  ))
    ->groupBy('td.tid')
    ->condition('tadg.rid', array_keys($account->roles), 'IN');

  // Filter by the indicated vids, if any.
  if (!empty($vids)) {
    $query
      ->fields('td', array(
      'vid',
    ))
      ->condition('td.vid', $vids, 'IN');
  }

  // Fetch term IDs.
  $r = $query
    ->execute()
    ->fetchAll();
  $tids = array();

  // If there are results, initialize a flag to test whether the user
  // has the grant for all terms.
  $grants_for_all_terms = empty($r) ? FALSE : TRUE;
  foreach ($r as $record) {

    // If the user has the grant, add the term to the array.
    if ($record->{$grant_field_name}) {
      $tids[] = $record->tid;
    }
    else {
      $grants_for_all_terms = FALSE;
    }
  }

  // If the user has the grant for all terms, return TRUE for a global grant.
  if ($grants_for_all_terms) {
    return TRUE;
  }
  return $tids;
}