public async Task <IActionResult> EditRole(int id) { var role = await _roleManager.FindByIdAsync(id.ToString()); if (role == null) { ViewBag.ErrorMessage = $"Role with Id = {id} cannot be found"; return(NotFound()); } var model = new EditRoleViewModel { Id = id, RoleName = role.Name, Users = new List <User>() }; foreach (var user in await _userManager.GetUsersInRoleAsync(role.Name)) { model.Users.Add(user); } return(View(model)); }
public async Task <IActionResult> EditRole(string id) { var roleChoice = await roleManager.FindByIdAsync(id); if (roleChoice == null) { ViewBag.Message = "The Role can not be found"; return(View("NotFound")); } var model = new EditRoleViewModel { Id = roleChoice.Id, RoleName = roleChoice.Name }; foreach (var user in userManager.Users) { if (await userManager.IsInRoleAsync(user, roleChoice.Name)) { model.Users.Add(user.FirstName); } } return(View(model)); }
public async Task <IActionResult> EditRole(EditRoleViewModel model) { var role = await RoleMangerService.FindByIdAsync(model.Id); if (role != null) { role.Name = model.RoleName; var result = await RoleMangerService.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("ListRole")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } //in case there were errors, return to edit return(View(model)); } ViewBag.ErrorMassage = $"role with id: {model.Id} cannot be found!"; return(View("NotFound")); }
public async Task <IActionResult> EditRoleGet(string id) { try { var roleResult = await _roleManager.FindByIdAsync(id); if (roleResult == null) { ViewBag.ErrorMessage = $"Role with id: {id} not found."; return(RedirectToAction("NotFound")); } // else if we have role then set edit role view model elements and pass it var model = new EditRoleViewModel { Id = roleResult.Id, RoleName = roleResult.Name }; // .... Iterate through all users and find users in role selected foreach (var itemUser in _userManager.Users.ToList()) { if (await _userManager.IsInRoleAsync(itemUser, roleResult.Name)) { model.Users.Add(itemUser.UserName); } } return(View(model)); } catch (Exception ex) { ViewBag.ErrorTitle = ex.Message; ViewBag.ErrorDescription = ex.StackTrace; return(View("Error")); } }
public async Task <IActionResult> EditRole(string Id) { var role = await roleManager.FindByIdAsync(Id); if (role == null) { ViewBag.ErrorMessage = $"Role with Id= {Id} cannot be found"; return(View("NotFound")); } var model = new EditRoleViewModel() { Id = role.Id, RoleName = role.Name, }; foreach (var user in userManager.Users) { if (await userManager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); } } return(View(model)); }
public async Task <IActionResult> EditRole(EditRoleViewModel model) { var role = await _roleManager.FindByIdAsync(model.Id); if (role == null) { throw new ArgumentNullException(); } else { role.Name = model.RoleName; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("ListRoles")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return(View(model)); } }
public async Task <IActionResult> Edit(string id) { if (id == null) { return(NotFound()); } // GET role var role = await _roleManager.FindByIdAsync(id); if (role == null) { return(NotFound()); } // Create an instance of the View Model inputs var model = new EditRoleViewModel { // Set input values with db values Id = role.Id, RoleName = role.Name }; // Loop through all users in database foreach (var user in _userManager.Users) { // Check if user is in current role if (await _userManager.IsInRoleAsync(user, role.Name)) { // Add to a list to return back model.Users.Add(user.UserName); } } return(View(model)); }
public async Task <IActionResult> EditRole(string id) { var role = await roleManager.FindByIdAsync(id); if (role == null) { ViewBag.ErrorMessage = $"Rollen med ID {id} finns inte"; return(View("NotFound")); } var model = new EditRoleViewModel { RoleID = role.Id, RoleName = role.Name, }; foreach (var user in userManager.Users) { if (await userManager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); } } return(View(model)); }
public async Task <IActionResult> EditRole(EditRoleViewModel model) { var role = await _roleManager.FindByIdAsync(model.Id); if (role == null) { return(NotFound()); } role.Name = model.RoleName; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { RedirectToAction("ListRoles"); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } return(View(model)); }
public async Task <IActionResult> EditRole(string id) { var role = await roleManager.FindByIdAsync(id); if (role == null) { ViewBag.ErrorMessage = $"id : {id} is not exists"; return(View("Error")); } EditRoleViewModel editRoleViewModel = new EditRoleViewModel() { Id = role.Id, RoleName = role.Name }; foreach (var user in userManager.Users) { if (await userManager.IsInRoleAsync(user, role.Name)) { editRoleViewModel.Users.Add(user.UserName); } } return(View(editRoleViewModel)); }
public async Task <IActionResult> DeleteRole(EditRoleViewModel model) { var role = await _roleManager.FindByIdAsync(model.Id); if (role == null) { ViewBag.ErrorMessage = $"Profil o id = {model.Id} nie został odnaleziony"; return(View("NotFound")); } else { var result = await _roleManager.DeleteAsync(role); if (result.Succeeded) { return(RedirectToAction("ListRoles")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return(View(model)); } }
public async Task <IActionResult> EditRole(string roleId) { var role = await roleManager.FindByIdAsync(roleId); if (role == null) { ViewBag.ErrorMessage = $"Role with the id {roleId} is Not Found."; return(View("NotFound")); } var model = new EditRoleViewModel { Id = role.Id, RoleName = role.Name }; foreach (var usr in userManager.Users) { if (await userManager.IsInRoleAsync(usr, role.Name)) { model.UsersUnderThisRole.Add(usr.UserName); } } return(View(model)); }
public async Task <IActionResult> EditRole(EditRoleViewModel editRoleViewModel) { if (ModelState.IsValid) { var role = await _roleManager.FindByIdAsync(editRoleViewModel.Id); role.Name = editRoleViewModel.RoleName; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("ListRoles", "Administration")); } else { foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } } ModelState.AddModelError(string.Empty, "An error occured!"); return(View(editRoleViewModel)); }
public async Task <IActionResult> EditRole(EditRoleViewModel model) { var role = await _roleManager.FindByIdAsync(model.Id.ToString()); if (role == null) { ViewBag.ErrorMessage = $"Role with Id = {model.Id} cannot be found"; System.Diagnostics.Debug.WriteLine($"Nie znaleziono roli o id = {model.Id}"); return(NotFound()); } role.Name = model.RoleName; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { System.Diagnostics.Debug.WriteLine($"Edytowano rolę o id = {model.Id}"); return(RedirectToAction("ListRoles")); } foreach (var error in result.Errors) { System.Diagnostics.Debug.WriteLine($"Nieudana rdycja roli o id = {model.Id}"); ModelState.AddModelError("", error.Description); } model.Users = new List <User>(); foreach (var user in await _userManager.GetUsersInRoleAsync(role.Name)) { model.Users.Add(user); } return(View(model)); }
public async Task <IActionResult> EditRole(string id) { var role = await roleManager.FindByIdAsync(id); if (role is null) { return(RedirectToAction("Index", "Home")); } var model = new EditRoleViewModel { Id = role.Id, RoleName = role.Name }; foreach (var user in userManager.Users) { if (await userManager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); } } return(View(model)); }
public async Task <IActionResult> Edit(EditRoleViewModel model) { if (ModelState.IsValid) { var role = await _roleManager.FindByIdAsync(model.Id); if (role == null) { ModelState.AddModelError("", "No role found with id " + model.Id); return(View()); } role.Name = model.RoleName; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("index")); } AddErrorsFromResult(result); } return(View()); }
public async Task <IActionResult> EditRole(string id) { var role = await roleManager.FindByIdAsync(id); if (role == null) { ViewBag.ErrorMessage = $"Role with id ={id} is NOT FOUND"; return(View("notfound")); } var model = new EditRoleViewModel { Id = role.Id, RoleName = role.Name }; foreach (var user in userManager.Users) { if (await userManager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); } } return(View(model)); }
public async Task <IActionResult> EditRole(string id) { var role = await roleManager.FindByIdAsync(id); if (role == null) { return(NotFound()); } var editRoleViewModel = new EditRoleViewModel { Id = role.Id, RoleName = role.Name }; foreach (var user in userManager.Users) { if (await userManager.IsInRoleAsync(user, role.Name)) { editRoleViewModel.Users.Add(user.UserName); } } return(View(editRoleViewModel)); }
//[Authorize(Policy = "EditRolePolicy")] public async Task <IActionResult> EditRole(EditRoleViewModel model) { var role = await _roleManager.FindByIdAsync(model.Id); if (role == null) { ViewBag.ErrorMessage = $"Role with {model.Id} cannot be found"; return(View("NotFound")); } else { role.Name = model.RoleName; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("ListRoles")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } } foreach (var user in _userManager.Users) { if (await _userManager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); } } return(View(model)); }
public async Task <IActionResult> RemoveFromRole(EditRoleViewModel model) { if (ModelState.IsValid) { var role = await roleManager.FindByIdAsync(model.Id); if (role == null) { return(NotFound()); } var user = await userManager.FindByEmailAsync(model.UserName); if (user == null) { return(NotFound()); } await userManager.RemoveFromRoleAsync(user, role.Name); await userManager.UpdateAsync(user); return(RedirectToAction("EditRole", new { id = model.Id })); } return(RedirectToAction("EditRole", new { id = model.Id })); }
public async Task <IActionResult> EditRole(string id) { var role = await _roleManager.FindByIdAsync(id); if (role == null) { return(RedirectToAction("RoleManagement", _roleManager.Roles)); } var editroleviewmodel = new EditRoleViewModel { Id = role.Id, RoleName = role.Name, Users = new List <string>() }; foreach (var user in _userManager.Users) { if (await _userManager.IsInRoleAsync(user, role.Name)) { editroleviewmodel.Users.Add(user.UserName); } } return(View(editroleviewmodel)); }
public async Task <IActionResult> EditRole(EditRoleViewModel model) { var role = await roleManager.FindByIdAsync(model.Id); if (role == null) { ViewBag.ErrorMessage = "Error"; return(View("Error")); } else { role.Name = model.RoleName; var result = await roleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("ListRoles")); } foreach (var error in result.Errors) { } return(View(model)); } }
public async Task <IActionResult> Edit(EditRoleViewModel model) { if (ModelState.IsValid) { var role = await roleManager.FindByIdAsync(model.RoleId); if (role != null) { role.Name = model.Name; var result = await roleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("Index", "RolesManager")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } } } return(View()); }
public async Task <IActionResult> Edit(EditRoleViewModel X) { var role = await roleManager.FindByIdAsync(X.RoleId); if (role == null) { return(View("NotFound")); } else { role.Name = X.RoleName; var result = await roleManager.UpdateAsync(role); if (!result.Succeeded) { foreach (var errore in result.Errors) { ModelState.AddModelError("", errore.Description); } } return(View(X)); } }
public async Task <IActionResult> EditRole(EditRoleViewModel editRole) { if (ModelState.IsValid) { var role = await roleManager.FindByIdAsync(editRole.RoleId); role.Name = editRole.RoleName; var result = await roleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("ListRoles")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } } editRole.Users = (List <User>) await userManager.GetUsersInRoleAsync(editRole.RoleId); ViewBag.OtherUsers = context.Users.ToList().Except(editRole.Users); return(View(editRole)); }
public async Task <ViewResult> EditRoles(string Id) { var role = await roleManager.FindByIdAsync(Id); if (role == null) { return(View("NotFound")); } var model = new EditRoleViewModel { Id = role.Id, RoleName = role.Name }; foreach (var user in userManager.Users.ToList()) { if (await userManager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); } } return(View(model)); }
public async Task <IActionResult> EditRole(string id) { var role = await _roleManager.FindByIdAsync(id); if (role == null) { ViewBag.ErrorMessage = "No roles found"; return(View("NotFound")); } var model = new EditRoleViewModel() { Id = role.Id, RoleName = role.Name }; foreach (var user in _userManager.Users) { if (await _userManager.IsInRoleAsync(user, role.Name)) { model.UserNames.Add(user.UserName); } } return(View(model)); }
public async Task <ActionResult> Edit(EditRoleViewModel model) { if (ModelState.IsValid) { Role role = await RoleManager.FindByIdAsync(model.Id); if (role != null) { role.Description = model.Description; role.Name = model.Name; IdentityResult result = await RoleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "Что-то пошло не так"); } } } return(View(model)); }
public async Task <IActionResult> EditRole(string id) { var role = await RoleMangerService.FindByIdAsync(id); if (role != null) { var model = new EditRoleViewModel { Id = role.Id, RoleName = role.Name, }; foreach (var user in await UserManager.GetUsersInRoleAsync(role.Name)) { model.Users.Add(user.UserName); } return(View(model)); } ViewBag.ErrorMassage = $"role with id: {id} cannot be found!"; return(View("NotFound")); }
public async Task <IActionResult> EditRole(EditRoleViewModel model) { var role = await RoleManager.FindByIdAsync(model.Id); if (role == null) { ViewBag.ErrorMessage = $"Role with Id = {model.Id} cannot be found"; return(View("NotFound")); } else { role.Name = model.RoleName; var result = await RoleManager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("ListRoles")); } AddErrors(result); return(View(model)); } }