Checking if a Drupal User has a Role

uses:
if(user_has_role(‘parent’,$user)){
// code
}

function:

<?php
/**
* Check to see if a user has been assigned a certain role.
*
* @param $role
*   The name of the role you’re trying to find.
* @param $user
*   The user object for the user you’re checking; defaults to the current user.
* @return
*   TRUE if the user object has the role, FALSE if it does not.
*/
function user_has_role($role, $user = NULL) {
if ($user == NULL) {
global $user;
}

if (is_array($user->roles) && in_array($role, array_values($user->roles))) {
return TRUE;
}

return FALSE;
}
?>

——————————-

to get all roles use user_roles()

Advertisements