user_roles

Versions
4.6 – 5
user_roles($membersonly = 0, $permission = 0)
6 – 7
user_roles($membersonly = FALSE, $permission = NULL)

Retrieve an array of roles matching specified conditions.

Parameters

$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.

Return value

An associative array with the role id as the key and the role name as value.

▾ 14 functions call user_roles()

filter_admin_format_form in modules/filter/filter.admin.inc
Generate a text format form.
filter_get_roles_by_format in modules/filter/filter.module
Retrieves a list of roles that are allowed to use a given text format.
filter_update_7005 in modules/filter/filter.install
Integrate text formats with the user permissions system.
node_update_7008 in modules/node/node.install
Split the 'administer nodes' permission from 'access content overview'.
theme_comment_post_forbidden in modules/comment/comment.module
Theme a "you can't post comments" notice.
theme_user_admin_new_role in modules/user/user.admin.inc
Theme the new-role form.
theme_user_admin_permissions in modules/user/user.admin.inc
Theme the administer permissions page.
upload_admin_settings in modules/upload/upload.admin.inc
Menu callback for the upload settings form.
user_account_form in modules/user/user.module
Helper function to add default user account fields to user registration and edit form.
user_admin_account in modules/user/user.admin.inc
Form builder; User administration page.
user_admin_permissions in modules/user/user.admin.inc
Menu callback: administer permissions.
user_admin_settings in modules/user/user.admin.inc
Form builder; Configure user settings for this site.
user_filters in modules/user/user.module
List user administration filters that can be applied.
user_user_operations in modules/user/user.module
Implement hook_user_operations().

Code

modules/user/user.module, line 2336

<?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);
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.