0

I have these roles setup within identity and one role is assigned per user

00 - Super Admin
10 - Admin
20 - Staff Manager
30 - Staff
40 - User Manager
50 - User

The aim is to allow a user to change the role of other users below (not equal to) their own user role

so if I could get the first two chars from the Name of the role added to the current user e.g. 30 then the choice of roles that could be applied to other users would be > 30

to get the user state of the current user

var currentUser = await _userManager.GetUserAsync(HttpContext.User);
var currentUserRole = await _userManager.GetRolesAsync(currentUser);

Then something like below that get all the roles, with 'WHERE' clause, or following if statement, first two chars > current users first two chars

var allRoles = _roleManager.Roles.ToList();

Does anyone know of a way that this could be done, or if there is a better idea?

Many Thanks

EDIT:

Ok, I now have this hardcoded to a SelectList in the view, select roles > 30 which is working

ViewBag.allRoles = _roleManager.Roles.Where(a => Convert.ToInt32(a.Name.Remove(2)) > 30).Select(a => new SelectListItem()
{
    Value = a.Id.ToString(),
    Text = a.Name.Remove(0, 5).ToString(),
}).ToList();

just need to get the current user role name to a string?

Sparki
  • 161
  • 1
  • 7

1 Answers1

0

I have managed to sort it

var currentUser = await _userManager.GetUserAsync(HttpContext.User);

var currentUserRole = await _userManager.GetRolesAsync(currentUser);

int currentUserRoleLevel = int.Parse(currentUserRole[0].Remove(2));

ViewBag.allRoles = _roleManager.Roles.Where(a => Convert.ToInt32(a.Name.Remove(2)) > currentUserRoleLevel).Select(a => new SelectListItem()
{
    Value = a.Id.ToString(),
    Text = a.Name.Remove(0, 5).ToString(),
}).ToList();

The question still stands, is there a better way?

Sparki
  • 161
  • 1
  • 7