Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get users not in role,

Is there any reasonably efficient way to get a list of users who are not in a specific role?

The only methods I can see are

  1. Get all the users from DB and do a check in code

  2. Go directly to the db and sidestep the role provider

like image 797
Mr. Flibble Avatar asked Mar 16 '11 12:03

Mr. Flibble


4 Answers

Another way is to extend RoleProvider with the method GetUsersNotInRole() and query the DB there. You can also combine RoleProvider.GetUsersInRole() with MembershipProvider.GetAllUsers() and find the difference

like image 130
Robert Avatar answered Nov 20 '22 06:11

Robert


You could just get the all user list and extract users in role specified from the list:

var usersInRole = Roles.GetUsersInRole("admin");
var users = Membership.GetAllUsers()
    .Cast<MembershipUser>()
    .Select(u => 
        !usersInRole.Contains(u.UserName)
    );
like image 30
Oleks Avatar answered Nov 20 '22 07:11

Oleks


Changing the Select to Where in the code provided by Alex will actually return the results instead of just true/false.

var usersInRole = Roles.GetUsersInRole("admin");
var users = Membership.GetAllUsers()
    .Cast<MembershipUser>()
    .Where(u => 
        !usersInRole.Contains(u.UserName)
    );
like image 4
Arun K Kotte Avatar answered Nov 20 '22 06:11

Arun K Kotte


I don't think that there is anything wrong with by-passing the role provider and querying the database directly. You will definitely get the best performance this way.

like image 2
Klaus Byskov Pedersen Avatar answered Nov 20 '22 05:11

Klaus Byskov Pedersen