You are here

function cf_user_has_role in Common Functionality 7

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

Provide a simple way to confirm whether or not a user is in a given role by the role id.

Why: The roles stored in the global $user variable are role names and not ids. This provides a way to get user role ids without having to load the user data, process each role, and produce an array of role ids.

Parameters

int $rid: a rid of the role to check to see if a user has the given permission.

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

array $function_history: (optional) An array of function names, ie: array('0' => 'my_function_name').

Return value

bool FALSE on error, FALSE if not in role, and TRUE if in role.

File

modules/cf_user/cf_user.module, line 71

Code

function cf_user_has_role($rid, $uid = NULL, array $function_history = array()) {
  cf_error_append_history($function_history, __FUNCTION__);
  if (!is_numeric($rid)) {
    cf_error_not_numeric($function_history, 'rid');
    return FALSE;
  }
  $query = cf_user_get_rids($uid, $function_history);
  if (is_object($query)) {
    $and = db_and();
    $and
      ->condition('r.rid', $rid, '=');
    $query
      ->condition($and);
    try {
      $query_execute = $query
        ->execute();
      if ($query_execute
        ->rowCount() == 1) {
        return TRUE;
      }
    } catch (Exception $e) {
      cf_error_on_query_execution($function_history, $e);
    }
  }
  return FALSE;
}