Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get users based on role?

How could users in a "customer" role be retrieved from a MembershipUserCollection?

like image 794
G. M. Nazmul Hossain Avatar asked Jan 24 '11 06:01

G. M. Nazmul Hossain


People also ask

How do you find the role of a user?

To check if a user has a specific role, you have to get a list of their roles and see if the role is listed there. $user_meta=get_userdata($user_id); $user_roles=$user_meta->roles; if (in_array("subscriber", $user_roles)){} Results in a role check for subscriber.

How do I find user roles in Salesforce?

Required Editions and User PermissionsFrom Setup, in the Quick Find box, enter Roles , then select Roles. If the “Understanding Roles” page is displayed, click Set Up Roles. Find the role under which you want to add the new role. Click Add Role.


1 Answers

Roles.GetUsersInRole returns a string[] of user names in a role. If you really want the MembershipUser objects, you can use:

var list = Roles.GetUsersInRole("roleName").Select(Membership.GetUser).ToList()

Of course, this is performance intensive as it hits the database once for every user.

If you are willing to give up provider-independence, you can query the underlying database directly and perform a join on the database server to get all users in a specific role.

like image 168
mmx Avatar answered Oct 02 '22 21:10

mmx