Mo Gusbi
Mo Gusbi

Reputation: 297

filter user list asp.net identity

I'm wanting to filter the list of registered users based on their roles. I've managed to do it so that it shows users of one certain role but what I want to achieve is the complete opposite and show only the users who are not in that user group

This is the code I have:

var users = UserManager.Users.ToListAsync();

var roleUsers = RoleManager.Roles.Single(a => a.Name.Equals("Super Admin")).Users;

var list = (from r in roleUsers
            join u in users on r.UserId equals u.Id
            select u);

Update

Thanks to @sjkm I've been able to filter out the super admins with this

var users = UserManager.Users.ToList();

var roles = RoleManager.Roles.Where(a => a.Name != "Super Admin").ToList();

var userList = new List<IdentityUserRole>();

foreach (var role in roles)
{
    userList.AddRange(role.Users.ToList());
}

var list = (from r in userList
            join u in users on r.UserId equals u.Id
            select u).Distinct();

return View(list);

My problem now is a user who is both an super admin and another role, is shown in this list so I now need to further filter down this list to remove any super admins who may be in another role

I also forgot to mention this is using asp.net identity 2

Upvotes: 3

Views: 5179

Answers (2)

Mo Gusbi
Mo Gusbi

Reputation: 297

I've managed to filter out the super admins who have secondary roles, fairly simple really, just get a list of user ids of super admins and remove them from the list of non-super admin users to find and remove those with the secondary role

var users = UserManager.Users.ToList();

var roles = RoleManager.Roles.Where(a => a.Name != "Super Admin").ToList();

var superAdmins = RoleManager.Roles.Single(b => b.Name == "Super Admin").Users;

var userList = new List<IdentityUserRole>();

foreach (var role in roles)
{
    userList.AddRange(role.Users.ToList());
}

foreach (var superAdmin in superAdmins)
{
    userList.RemoveAll(x => x.UserId == superAdmin.UserId);
}

var list = (from r in userList
            join u in users on r.UserId equals u.Id
            orderby u.FirstName ascending
            orderby u.LastName ascending
            select u).Distinct();

return View(list);

No doubts this is a really rubbish and inefficient way of doing it but it does the job nonetheless but if anyone can streamline it then please be my guest

Hope this will be of some use to somebody else out there!

Update

Managed to streamline this by just getting the user list and removing those whose user Ids match those who appear in the Super Admin user list

var users = UserManager.Users.ToList();
var superAdmins = RoleManager.Roles.Single(b => b.Name == "Super Admin").Users;

foreach (var superAdmin in superAdmins)
{
    users.RemoveAll(c => c.Id == superAdmin.UserId);
}

return View(users.ToList());

Upvotes: 2

sjkm
sjkm

Reputation: 3937

var roles = RoleManager.Roles.Where(r => r.Name != "Super Admin").ToList();

var users = new List<User>();

foreach(var role in roles) {
    users.AddRange(role.Users.ToList());
}

// 'users'-List contains all the users which do not have the 'Super Admin' role

(For a better perfomance get all ids of the roles and select the users with a single query Users.Where(u => rolesIds.Contains(u.Role_IDFS)))

Upvotes: 1

Related Questions