public async Task <IActionResult> Manage(string userId) { ViewBag.userId = userId; var user = await _userManager.FindByIdAsync(userId); if (user == null) { ViewBag.ErrorMessage = $"User with Id = {userId} cannot be found"; return(NotFound()); } ViewBag.UserName = user.UserName; var model = new List <ManageUserRolesVM>(); foreach (var role in _roleManager.Roles.ToList()) { var userRolesViewModel = new ManageUserRolesVM { UlogaID = role.Id, ImeUloge = role.Name }; if (await _userManager.IsInRoleAsync(user, role.Name)) { userRolesViewModel.Selected = true; } else { userRolesViewModel.Selected = false; } model.Add(userRolesViewModel); } return(View(model)); }
public async Task OnGet(string userId) { var user = await _userManager.FindByIdAsync(userId); UserName = user.UserName; ManageUserRolesVMs = new List <ManageUserRolesVM>(); var roles = _roleManager.Roles.ToList(); int index = 0; foreach (var role in roles) { var manageUserRoles = new ManageUserRolesVM { Index = index, RoleId = role.Id, RoleName = role.Name }; index += 1; if (await _userManager.IsInRoleAsync(user, role.Name)) { manageUserRoles.Selected = true; } else { manageUserRoles.Selected = false; } ManageUserRolesVMs.Add(manageUserRoles); } }
public async Task <IActionResult> ManageUserRoles(string userId) { var user = await _userManager.FindByIdAsync(userId); if (user == null) { ViewBag.ErrorMessage = $"The user with id {userId} can not be found"; return(View("_NotFound")); } ManageUserRolesVM model = new ManageUserRolesVM(); model.UserId = user.Id; model.UserName = user.UserName; foreach (var role in _roleManager.Roles) { UserRole userRole = new UserRole { RoleId = role.Id, RoleName = role.Name, IsSelected = await _userManager.IsInRoleAsync(user, role.Name) }; model.Roles.Add(userRole); } return(View(model)); }
public async Task <IActionResult> ManageUserRoles(ManageUserRolesVM model) { var user = await _userManager.FindByIdAsync(model.UserId); if (user == null) { ViewBag.ErrorMessage = $"The user with id {model.UserId} can not be found"; return(View("_NotFound")); } for (int i = 0; i < model.Roles.Count; i++) { var role = await _roleManager.FindByIdAsync(model.Roles[i].RoleId); IdentityResult result = null; if (model.Roles[i].IsSelected && !(await _userManager.IsInRoleAsync(user, role.Name))) { result = await _userManager.AddToRoleAsync(user, role.Name); } else if (!model.Roles[i].IsSelected && await _userManager.IsInRoleAsync(user, role.Name)) { result = await _userManager.RemoveFromRoleAsync(user, role.Name); } else { continue; } if (result.Succeeded) { if (i < (model.Roles.Count - 1)) { continue; } else { return(RedirectToAction("EditUser", new { Id = model.UserId })); } } } return(RedirectToAction("EditUser", new { Id = model.UserId })); }
public async Task <IActionResult> ManageUserRoles(string id) { var user = await _userManager.FindByIdAsync(id); if (user == null) { ViewBag.ErrorMessage = $"User with ID = {id} do not exist."; return(View("Error", "Admin")); } ManageUserRolesVM model = new ManageUserRolesVM { user = new IdentityUser { UserName = user.UserName, Id = user.Id }, roles = _roleManager.Roles.Select(s => s.Name).ToList() }; return(View(model)); }