public async Task <IActionResult> Assign(RoleUserVm ruVm) { var user = _context.AppUsers.FirstOrDefault(c => c.Id == ruVm.UserId); var isCheckRoleAssign = await _userManager.IsInRoleAsync(user, ruVm.RoleId); if (isCheckRoleAssign) { ViewBag.mgs = "Ten użytkownik ma już przypisaną rolę."; ViewBag.UserId = new SelectList(_context.AppUsers.Where(f => f.LockoutEnd < DateTime.Now || f.LockoutEnd == null).ToList(), "Id", "UserName"); ViewBag.RoleId = new SelectList(_manager.Roles.ToList(), "Name", "Name"); return(View()); } var role = await _userManager.AddToRoleAsync(user, ruVm.RoleId); if (role.Succeeded) { TempData["save"] = "Rola została przypisana użytkownikowi"; return(RedirectToAction(nameof(Index))); } return(View()); }
public async Task <IActionResult> EditUserRoles(string userId) { ViewBag.UserId = userId; var user = await userManager.FindByIdAsync(userId); if (user == null) { ViewBag.Message = $"User with id = {userId} not found."; return(View("NotFound")); } var model = new List <RoleUserVm>(); foreach (var role in roleManager.Roles) { var roleUserVm = new RoleUserVm { RoleId = role.Id, RoleName = role.Name }; if (await userManager.IsInRoleAsync(user, role.Name)) { roleUserVm.IsSelected = true; } else { roleUserVm.IsSelected = false; } model.Add(roleUserVm); } return(View(model)); }
public async Task <IActionResult> Assign(RoleUserVm roleUser) { var user = _db.ApplicationUsers.FirstOrDefault(c => c.Id == roleUser.UserId); var isChackRoleAssign = await _userManager.IsInRoleAsync(user, roleUser.RoleId); if (isChackRoleAssign) { ViewBag.msg = "This user already assign this role."; ViewData["UserId"] = new SelectList(_db.ApplicationUsers.Where(f => f.LockoutEnd < DateTime.Now || f.LockoutEnd == null).ToList() , "Id", "UserName"); //Id est l'ID dans la table AspNetUsers ViewData["RoleId"] = new SelectList(_roleManager.Roles.ToList(), "Name", "Name"); //Id est l'ID dans la table AspNetRoles return(View()); } var role = await _userManager.AddToRoleAsync(user, roleUser.RoleId); if (role.Succeeded) { TempData["save"] = "User Role assigned."; return(RedirectToAction(nameof(Index))); } return(View()); }
public async Task <IActionResult> DeleteAssignRole(RoleUserVm roleUser) { var user = _db.ApplicationUsers.FirstOrDefault(c => c.Id == roleUser.UserId); var isCheckRoleAssign = await _userManager.IsInRoleAsync(user, roleUser.RoleId); var role = await _userManager.RemoveFromRoleAsync(user, roleUser.RoleId); if (role.Succeeded) { TempData["Delete"] = "User Role Deleted."; return(RedirectToAction(nameof(AssignUserRole))); } return(View()); }
public async Task <IActionResult> Assign(RoleUserVm roleUser) { var user = _db.ApplicationUsers.FirstOrDefault(c => c.Id == roleUser.UserId); var IsCheckRoleAssign = await _userManager.IsInRoleAsync(user, roleUser.RoleId); //check if the role assign previously or not if (IsCheckRoleAssign) { ViewBag.message = "Already Assigned"; ViewData[("UserId")] = new SelectList(_db.ApplicationUsers.Where(c => c.LockoutEnd < DateTime.Now || c.LockoutEnd == null).ToList(), "Id", "UserName"); //dropdown data bind and check for disable lockout user //ViewData[("RoleId")] = new SelectList(_roleManager.Roles.ToList(),"Id","Name"); //dropdown data bind ViewData[("RoleId")] = new SelectList(_roleManager.Roles.ToList(), "Name", "Name"); //dropdown data bind return(View()); } var role = await _userManager.AddToRoleAsync(user, roleUser.RoleId); if (role.Succeeded) { TempData["save"] = "Role Assign Successfully"; //sucesss message return(RedirectToAction(nameof(Index))); //redirect to index page } return(View()); }
public async Task <IActionResult> Assign(RoleUserVm roleUser) { var user = _db.ApplicationUsers.FirstOrDefault(x => x.Id == roleUser.userId); var isExist = await _userManager.IsInRoleAsync(user, roleUser.roleId); if (isExist) { ViewBag.msg = "This Role is already assigned for this User"; ViewData["UserId"] = new SelectList(_db.ApplicationUsers.Where(x => x.LockoutEnd < DateTime.Now || x.LockoutEnd == null).ToList(), "Id", "UserName"); ViewData["RoleId"] = new SelectList(_roleManager.Roles.ToList(), "Name", "Name"); return(View()); } var role = await _userManager.AddToRoleAsync(user, roleUser.roleId); if (role.Succeeded) { TempData["save"] = "Role has been Assigned"; return(RedirectToAction(nameof(Index))); } return(View()); }