user_roles($membersonly = FALSE, $permission = NULL)Retrieve an array of roles matching specified conditions.
$membersonly Set this to TRUE to exclude the 'anonymous' role.
$permission A string containing a permission. If set, only roles containing that permission are returned.
An associative array with the role id as the key and the role name as value.
modules/user/user.module, line 2495
<?php
function user_roles($membersonly = FALSE, $permission = NULL) {
// System roles take the first two positions.
$roles = array(
DRUPAL_ANONYMOUS_RID => NULL,
DRUPAL_AUTHENTICATED_RID => NULL,
);
if (!empty($permission)) {
$result = db_query("SELECT r.* FROM {role} r INNER JOIN {role_permission} p ON r.rid = p.rid WHERE p.permission = :permission ORDER BY r.name", array(':permission' => $permission));
}
else {
$result = db_query('SELECT * FROM {role} ORDER BY name');
}
foreach ($result as $role) {
switch ($role->rid) {
// We only translate the built in role names
case DRUPAL_ANONYMOUS_RID:
if (!$membersonly) {
$roles[$role->rid] = t($role->name);
}
break;
case DRUPAL_AUTHENTICATED_RID:
$roles[$role->rid] = t($role->name);
break;
default:
$roles[$role->rid] = $role->name;
}
}
// Filter to remove unmatched system roles.
return array_filter($roles);
}
?>