public static void SeedRoles(RoleManager <ApplicationRoleModel> roleManager) { if (!roleManager.RoleExistsAsync("Administrator").Result) { ApplicationRoleModel identityRole = new ApplicationRoleModel(); identityRole.Name = "Administrator"; identityRole.Description = "Full access to the application."; identityRole.NormalizedName = "Admin"; IdentityResult result = roleManager.CreateAsync(identityRole).Result; } if (!roleManager.RoleExistsAsync("Editor").Result) { ApplicationRoleModel identityRole = new ApplicationRoleModel(); identityRole.Name = "Editor"; identityRole.Description = "Can edit posts and comments."; identityRole.NormalizedName = "Edit"; IdentityResult result = roleManager.CreateAsync(identityRole).Result; } if (!roleManager.RoleExistsAsync("User").Result) { ApplicationRoleModel identityRole = new ApplicationRoleModel(); identityRole.Name = "User"; identityRole.Description = "Can write posts and comments."; identityRole.NormalizedName = "User"; IdentityResult result = roleManager.CreateAsync(identityRole).Result; } }
public ActionResult Edit(ApplicationRoleModel model) { if (ModelState.IsValid) { var role = db.Roles.Find(model.Id); if (role == null) { this.NotifyError("Role not found."); return(RedirectToAction("List")); } if (!role.Name.Equals(model.Name, StringComparison.CurrentCultureIgnoreCase)) { role.Name = model.Name; var result = db.SaveChanges(); if (result > 0) { this.NotifySuccess("Successfully saved."); } else { this.NotifyError("Role can not saved!"); } } else { this.NotifyInfo("No any change to save."); } return(RedirectToAction("Edit", new { name = role.Name })); } return(View(model)); }
public IActionResult New() { ApplicationRoleModel model = new ApplicationRoleModel { FormType = Global.FormType.Create, UserHandler = _userHandler, IsRecordOwner = true }; return(View("Form", model)); }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Edit(string id) { var role = db.Roles.Find(id); if (role == null) { this.NotifyError("Role not found."); return(RedirectToAction("List")); } var model = new ApplicationRoleModel { Id = role.Id, Name = role.Name, UserCount = role.Users.Count }; return(View(model)); }
public ActionResult Create(ApplicationRoleModel model) { if (ModelState.IsValid) { var newRole = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); newRole.Name = model.Name; db.Roles.Add(newRole); var result = db.SaveChanges(); if (result > 0) { this.NotifySuccess("Successfully saved."); } else { this.NotifyError("Role can not save!"); } return(RedirectToAction("Edit", new { Id = newRole.Id })); } return(View(model)); }
public ActionResult Create() { var model = new ApplicationRoleModel(); return(View(model)); }
public async Task <IActionResult> New(ApplicationRoleModel model) { model = await Transact(model); return(PartialView("_Form", model)); }
public async Task <IActionResult> ChangeStatus(ApplicationRoleModel model) { model = await Transact(model, true); return(PartialView("_Form", model)); }
private async Task <ApplicationRoleModel> Transact(ApplicationRoleModel model, bool changeStatus = false) { model.FormBehavior = new FormBehavior(); model.UserHandler = _userHandler; if (ModelState.IsValid) { try { var role = _mapper.Map <ApplicationRoleModel, ApplicationRole>(model); if (model.FormType == Global.FormType.Create) { IdentityResult result = await _roleManager.CreateAsync(role); if (!result.Succeeded) { ModelState.AddModelError(string.Empty, result.Errors.FirstOrDefault().Description); } else { model.FormBehavior = new FormBehavior { PageRedirect = new PageRedirect { Reload = true, URL = Url.Action("Form", "ApplicationRole", new { @id = role.Id }) } }; } model.IsRecordOwner = true; } else if (model.FormType == Global.FormType.Update || model.FormType == Global.FormType.ReadOnly) { var tempRole = _context.ApplicationRole.Find(model.Id); if ((tempRole.IsSysAdmin && !model.IsSysAdmin) || (tempRole.IsSysAdmin && changeStatus && tempRole.Status == Enums.Status.Active)) { var adminCount = _context.ApplicationRole.Where(r => r.IsSysAdmin).Count(); if (adminCount <= 1) { throw new Exception("There should be one existing System Administrator."); } } if (changeStatus) { var status = tempRole.Status == Enums.Status.Active ? Enums.Status.Inactive : Enums.Status.Active; tempRole.Status = status; _context.ApplicationRole.Update(tempRole); await _context.SaveChangesAsync(); model.FormBehavior = new FormBehavior { PageRedirect = new PageRedirect { Reload = true, URL = Url.Action("Form", "ApplicationRole", new { @id = model.Id }) } }; } else if (model.FormType == Global.FormType.Update) { tempRole.Description = model.Description; tempRole.IsSysAdmin = model.IsSysAdmin; _context.ApplicationRole.Update(tempRole); await _context.SaveChangesAsync(); } model.FormBehavior.Notification = new Notification { IsError = false, Message = "Changes successfuly saved.", Title = "Permission" }; model.IsRecordOwner = tempRole.CreatedBy == _userHandler.User.Id; } } catch (Exception ex) { model.FormBehavior.Notification = new Notification { IsError = true, Message = ex.Message, Title = "Application Role" }; } } LookupControlModel lookupModel = new LookupControlModel(); lookupModel.Title = "Application Permissions"; lookupModel.LookupControlId = "permissionlookup"; lookupModel.PrimaryRecordId = model.Id; lookupModel.RelationshipName = "RolePermission"; model.PermissionLookup = lookupModel; return(model); }