You are here

function term_permissions_allowed in Taxonomy Term Permissions 7

Same name and namespace in other branches
  1. 6 term_permissions.module \term_permissions_allowed()

Given a term ID, determine if a user has access to that term. UID 1 is always allowed access. If no permissions are set on the term, allow access by default.

Parameters

$tid: The term ID to look up.

$user: The user to determine if it has access to the term ID.

Return value

bool TRUE if the user has access to the term, otherwise FALSE.

2 calls to term_permissions_allowed()
term_permissions_field_widget_validate in ./term_permissions.module
Form element validation handler for taxonomy term reference element.
term_permissions_form_alter in ./term_permissions.module
Implements hook_form_alter().

File

./term_permissions.module, line 275
Allows access to terms in a vocabulary to be limited by user or role.

Code

function term_permissions_allowed($tid, $user) {
  if ($user->uid == 1) {
    return TRUE;
  }

  // Are permissions enabled on this term?
  $term_users = db_select('term_permissions_user', 'u')
    ->condition('u.tid', $tid)
    ->countQuery();
  $term_roles = db_select('term_permissions_role', 'r')
    ->condition('r.tid', $tid)
    ->countQuery();
  if (!($term_users
    ->execute()
    ->fetchField() || $term_roles
    ->execute()
    ->fetchField())) {
    return TRUE;
  }

  // Permissions are enabled, so check to see if this user is allowed.
  $users = db_select('term_permissions_user', 't')
    ->fields('t', array(
    'uid',
  ))
    ->condition('t.tid', $tid)
    ->condition('t.uid', $user->uid)
    ->execute()
    ->fetchField();
  if ($users) {
    return TRUE;
  }

  // Or one of this user's roles is allowed.
  $roles = db_select('term_permissions_role', 't')
    ->fields('t', array(
    'rid',
  ))
    ->condition('t.tid', $tid)
    ->condition('t.rid', array_keys($user->roles), 'IN')
    ->execute()
    ->fetchField();
  return $roles ? TRUE : FALSE;
}