2

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.

4

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.

  • As the query is getting a single field, db_result() can be probably be used. – kiamlaluno Mar 16 '12 at 9:54
  • 1
    @kiamlaluno The query's getting multiple results...all of the user ids that are related to a specific role. I think it's very unlikely that there will only be one user per role :) – Clive Mar 16 '12 at 9:55
  • 2
    db_result() returns a single value, but it can be used in a loop; I tried with $q = db_query('SELECT uid FROM {users_roles}'); while ($uid = db_result($q)) $uids[] = $uid; and it worked. The fact the function returns the value of a single field doesn't mean it doesn't return the value for different rows. – kiamlaluno Apr 17 '12 at 11:37
  • @kiamlaluno I didn't know you could do that but it does make sense, thanks for the tip :) – Clive Apr 17 '12 at 18:27
3

user_roles() returns an array of roles. There is no function that returns all users of a particular role.

0

You can use Views to query a list of users, and filter them by Roles.

Or you can specify an Argument of User: Roles on the view. Then use user_roles() in a custom module to grab a list of all roles. Loop through the roles array, and inside the loop use views_embed_view('my_users_view', 'display_id', $roleid) passing the role id to this function as a third argument.

  • Thanks for the response, but my query was to know if there was a Drupal method for this without me doing anything other than calling a pre-defined function. – optimusprime619 Mar 16 '12 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.