Drupal method to retrieve all existing roles and all users of a specified role I wish to retrieve the list of all users that belong to a particular role.

The query is to know:


if there's any drupal method that provides list of all available roles created under Admin >> User Management (admin/user/roles) similar to retrieving all node types using node_get_types method.
if there's a drupal method to get list of all users who belong to a particular role.


Though I could get this done by querying the role table and using the result with user_roles table, I felt that there might be some in-built method in drupal which must enable me to achieve the desired result which i might've missed out searching.

I'm aware that it's not ideal to check user permission related stuff with roles and rather user permissions instead from this post.

It'd be great if I could get a definite answer for my above mentioned queries.
You can use the user_roles() function to get a list of the roles in the system. You can even pass in specific conditions, for example if you want to get only the roles that contain a certain permission.

As far as getting a list of users by role, there is no such function, unfortunately; you'll have to write a small helper function yourself. I usually use something like this:

function users_by_role($role_id) {
  $sql = 'SELECT uid FROM {users_roles} WHERE rid = %d';
  $q = db_query($sql, $role_id);
  $uids = array();
  while ($row = db_fetch_array($q)) {
    $uids[] = $row['uid'];
  }

  return $uids;
}


That'll give you the user ids for a given role, after that you can use user_load() to get the user object for each, or simply add a join to the above query and include the data you need directly.