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; // Update the Role using UpdateAsync var result = await Rolemanager.UpdateAsync(role); if (result.Succeeded) { return(RedirectToAction("GetAllRoles")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return(View(model)); } }
public async Task <IActionResult> EditRole(string id) { // Find the role by Role 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 }; // Retrieve all the Users foreach (var user in Usermanager.Users) { // If the user is in this role, add the username to // Users property of EditRoleViewModel. This model // object is then passed to the view for display if (await Usermanager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); } } return(View(model)); }
//Call the Dispose method later public void Dispose(bool disposing) { if (disposing) { Dispose(); } if (empRepo != null) { empRepo = null; } if (BunitsRepo != null) { BunitsRepo = null; } if (ReportRepo != null) { ReportRepo = null; } if (employeeManager != null) { employeeManager = null; } if (RoleManager != null) { RoleManager = null; } this.Dispose(); }
public HRController(IEmployees empParam, IBusinessUnits busParam, IReportManager rParam, Rolemanager RoleParam, EmployeeManager EParam, ApplicationDbContext dbParam, IEmploymentStatus SParam) { db = dbParam; UserManager = new ApplicationUserManager(new UserStore <ApplicationUser>(db)); empRepo = empParam; BunitsRepo = busParam; ReportRepo = rParam; employeeManager = EParam; RoleManager = RoleParam; db = dbParam; statusRepo = SParam; }
public async Task <IActionResult> EditUsersInRole(List <UserRoleViewModel> model, string roleId) { var role = await Rolemanager.FindByIdAsync(roleId); if (role == null) { ViewBag.ErrorMessage = $"Role with Id = {roleId} cannot be found"; return(View("NotFound")); } for (int i = 0; i < model.Count; i++) { var user = await Usermanager.FindByIdAsync(model[i].UserId); IdentityResult result = null; if (model[i].IsSelected && !(await Usermanager.IsInRoleAsync(user, role.Name))) { result = await Usermanager.AddToRoleAsync(user, role.Name); } else if (!model[i].IsSelected && await Usermanager.IsInRoleAsync(user, role.Name)) { result = await Usermanager.RemoveFromRoleAsync(user, role.Name); } else { continue; } if (result.Succeeded) { if (i < (model.Count - 1)) { continue; } else { return(RedirectToAction("EditRole", new { Id = roleId })); } } } return(RedirectToAction("EditRole", new { Id = roleId })); }
public async Task <IActionResult> CreateRole(CreateRoleViewmodel model) { var identityRole = new IdentityRole { Name = model.RoleName }; var result = await Rolemanager.CreateAsync(identityRole); if (result.Succeeded) { return(RedirectToAction("GetAllRoles", "Administration")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return(View(model)); }
public async Task <IActionResult> DeleteRole(string id) { var role = await Rolemanager.FindByIdAsync(id); if (role == null) { ViewBag.ErrorMessage = $"User with Id = {id} cannot be found"; return(View("NotFound")); } else { try { var result = await Rolemanager.DeleteAsync(role); if (result.Succeeded) { return(RedirectToAction("GetAllRoles")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return(View("GetAllRoles")); } catch { //Log the exception to a file. We discussed logging to a file // using Nlog in Part 63 of ASP.NET Core tutorial // Pass the ErrorTitle and ErrorMessage that you want to show to // the user using ViewBag. The Error view retrieves this data // from the ViewBag and displays to the user. ViewBag.ErrorTitle = $"{role.Name} role is in use"; ViewBag.ErrorMessage = $"{role.Name} role cannot be deleted as there are users in this role. If you want to delete this role, please remove the users from the role and then try to delete"; return(View("Error")); } } }
public async Task <IActionResult> EditUsersInRole(string roleId) { ViewBag.roleId = roleId; var role = await Rolemanager.FindByIdAsync(roleId); if (role == null) { ViewBag.ErrorMessage = $"Role with Id = {roleId} cannot be found"; return(View("NotFound")); } var model = new List <UserRoleViewModel>(); foreach (var user in Usermanager.Users) { if (User.IsInRole("Super Admin") || User.IsInRole("Admin") && !(await Usermanager.IsInRoleAsync(user, "Admin") || await Usermanager.IsInRoleAsync(user, "Super Admin"))) { var userRoleViewModel = new UserRoleViewModel { UserId = user.Id, UserName = user.UserName }; if (await Usermanager.IsInRoleAsync(user, role.Name)) { userRoleViewModel.IsSelected = true; } else { userRoleViewModel.IsSelected = false; } model.Add(userRoleViewModel); } } return(View(model)); }
// GET: Employee public ActionResult Index() { Departmanager Departmanager = new Departmanager(); var Depart = Departmanager.GetAll(); Depart.Insert(0, new Depart() { Id = 0, DepartName = "请选择部门类型" }); ViewBag.DepartId = new SelectList(Depart, "Id", "DepartName"); Rolemanager Rolemanager = new Rolemanager(); var Role = Rolemanager.GetAll(); Role.Insert(0, new Role() { Id = 0, RoleName = "请选择角色类型" }); ViewBag.RoleId = new SelectList(Role, "Id", "RoleName"); return(View()); }
public void CreateDeafaultRoles() { Rolemanager manager = new Rolemanager(); manager.CreateRoles(); }