Change record status: 
Project: 
Introduced in branch: 
7.x
Introduced in version: 
7.36
Description: 

#2394517: Add a function to check if a user has a certain role has introduced a new API function to check if a user has a specific role.

Before:


function MYMODULE_foo() {
  global $user;
  $role = user_role_load_by_name('Author');
  $has_role = isset($user->roles[$role->rid]);
  if ($has_role) {
   // Code if user has 'Author' role...
  }
  else {
   // Code if user doesn't have 'Author' role...
  }
}

After:

function MYMODULE_foo() {
  $role = user_role_load_by_name('Author');
  if ($role && user_has_role($role->rid)) {
   // Code if user has 'Author' role...
  }
  else {
   // Code if user doesn't have 'Author' role...
  }
}

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

RajabNatshah’s picture

I do think this is better now.
Testing :)

Rewarded work .

Rajab Natshah

pelowe’s picture

I have always used in_array('xxxxx', $user->roles).

Is there a reason why the new implementation would be better?

Thanks,
- Perry

Cinedin’s picture

It should be...

$role = user_role_load_by_name('Author');
if ($role && user_has_role($role->rid)) {
    // ...

+1 to pelowe

sergeypavlenko’s picture

Hi, @Cinedin

I'm updated change record.

domignon’s picture

A quick dpm() reveals that the parameter $rid is an array instead of an integer, raising an error in the function implementation.
Sticking on drupal 7.35 for now

Any idea why ?

arulraj.m’s picture

It’s really good to see the new function to check a user have specific role or not.

I have a concerns here, Instead of checking role id we can check role name because am not sure we may have same data entry in roles table in development and Production servers(if records miss match it will give wrong matches with role id).

Changes :

function user_has_role($role_name, $user = NULL) {
if ($user == NULL) {
global $user;
}
if (is_array($user->roles) && in_array($role_name, array_values($user->roles))) {
return TRUE;
}

return FALSE;
}

tssarun’s picture

Passing role-name is a good idea.

Regards,
tssarun.

stickywes’s picture

Yeah, this should've been by role name.

alisonjo2786’s picture

I would love this!! Did anything ever come of this suggestion? As far as I can tell, user_has_role() is still just by role ID. Was the idea picked up in a different thread, or?

doublejosh’s picture

You might find this utility function handy...

/**
 * Check list of role names against current user.
 *
 * @param array $role_names
 *
 * @return boolean
 */
function my_module_check_roles($role_names) {
  foreach ($role_names as $name) {
    $role = user_role_load_by_name($name);
    if (!$role || !user_has_role($role->rid)) {
      return FALSE;
    }
  }
  return TRUE;
}


Example use...

if (my_module_check_roles('administrator', 'editor')) {
  drupal_set_message(t('You so fancy'));
}
sergeypavlenko’s picture

Hi

Your function will not work. Because:

my_module_check_roles('administrator', 'editor')

need update to:

my_module_check_roles(array('administrator', 'editor'))

Or update general function to:

function my_module_check_roles() {
  $role_names = func_get_args();

  foreach ($role_names as $name) {