You are here

function cf_user_get_rids in Common Functionality 7.2

Same name and namespace in other branches
  1. 7 modules/cf_user/cf_user.module \cf_user_get_rids()

Provide a simple way to get the roles ids for the current or specified user.

Returns a query object meant to return all rids for a given uid. This does not call ->execute() on the query object.

Can return an array of rids and will included the roles 0 and 2.

Justification: see cf_user_has_role().

Parameters

int $uid: (optional) a uid of the user to get the role ids of if the current user is not to be used.

int $object: (optional) If TRUE, then an un-executed query object is returned. If FALSE, then an array of rids is returned.

Return value

object|array Either a SelectQuery database object, this object has not been executed or an array of rids is returned.

Related topics

1 call to cf_user_get_rids()
cf_user_has_role in modules/cf_user/cf_user.module
Confirm whether a user is in a given role by the role id.

File

modules/cf_user/cf_user.module, line 37
Common Functionality - User module.

Code

function cf_user_get_rids($uid = NULL, $object = TRUE) {
  if ($uid == NULL) {
    $current_user = cf_current_user(FALSE);
    $uid = $current_user->uid;
  }
  elseif (!is_numeric($uid)) {
    if (class_exists('cf_error')) {
      cf_error::invalid_numeric('uid');
    }
    return FALSE;
  }
  $query = db_select('role', 'r');
  $query
    ->innerjoin('users_roles', 'ur', 'r.rid = ur.rid');
  $query
    ->fields('r');
  $query
    ->fields('ur');
  $and = db_and();
  $and
    ->condition('uid', $uid, '=');
  $query
    ->condition($and);
  if ($object) {
    return $query;
  }
  global $user;
  $rids = array();
  try {
    $query_execute = $query
      ->execute();
    $rids = array_keys($query_execute
      ->fetchAllAssoc('rid'));
  } catch (Exception $e) {
    if (class_exists('cf_error')) {
      cf_error::on_query_execution($e);
    }
  }
  if ($user->uid == 0) {
    if (!in_array(0, $rids)) {
      $rids[] = 0;
    }
  }
  else {
    if (!in_array(2, $rids)) {
      $rids[] = 2;
    }
    if ($user->uid == 1 && !in_array(1, $rids)) {
      $rids[] = 1;
    }
  }
  return $rids;
}