Drupal user access role function

This is a little function that can be used like user_access but for checking if the user is in some role.

<?php
/**
 * Check if user has role
 *
 * @param unknown_type $role
 * @return unknown
 */
function _user_has_role($role) {
  global
$user;

  if (
is_string($role)) {
   
$role = array($role);
  }

  foreach (
$role as $key => $role_name) {
    if (
in_array($role_name, $user->roles)) {
        return
TRUE;
    }
  }

  return
FALSE;

}


//implementation of has one of the roles
 
if (_user_has_role(array('sales', 'management', 'admin'))) {
     
//some action
 
}

//implementation of has this role
 
if (_user_has_role('sales'))) {
     
//some action
 
}

?>
Knowledge keywords: