public ActionResult Add(Role role, FormCollection f) { //if (!aclService.HasRight(Rights.CreateNewRoles)) if (!aclService.HasRight(Rights.CreateNewRoles)) { return(RedirectToAction("AccessDenied", "Home")); } if (ModelState.IsValid) { int duplicateRole = context.Role.Where(m => m.Name == role.Name && m.IsDeleted == false).Count(); if (duplicateRole == 0) { Role dbRole = new Role(); dbRole.GUID = Functions.GetRandomGUID(); dbRole.Name = role.Name; dbRole.AddedOn = DateTime.UtcNow; dbRole.AddedBy = Functions.CurrentUserID(); dbRole.IsDeleted = false; context.Role.Add(dbRole); context.SaveChanges(); for (int i = 0; i < AllRightsList.Count; i++) { if (f[AllRightsList[i].Name] != null) { bool isChecked = f[AllRightsList[i].Name].Contains("true"); if (isChecked) { RightsInRole RIR = new RightsInRole(); RIR.RightsName = AllRightsList[i].Name; RIR.RoleID = dbRole.ID; context.RightsInRole.Add(RIR); context.SaveChanges(); } } } TempData["SuccessMessage"] = "Role added successfully."; return(RedirectToAction("List")); } else { TempData["ErrorMessage"] = "Role is already exist with this name. Please enter different role name."; } } return(View(role)); }
public ActionResult Edit(Role role, FormCollection f) { if (Functions.CurrentUserID() == role.AddedBy) { //if (!aclService.HasRight(Rights.EditOwnRoles)) if (!aclService.HasRight(Rights.EditOwnRoles)) { return(RedirectToAction("AccessDenied", "Home")); } } else { //if (!aclService.HasRight(Rights.EditOtherUsersRoles)) if (!aclService.HasRight(Rights.EditOtherUsersRoles)) { return(RedirectToAction("AccessDenied", "Home")); } } if (ModelState.IsValid) { int duplicateRole = context.Role.Where(m => m.Name == role.Name && m.ID != role.ID && m.IsDeleted == false).Count(); if (duplicateRole == 0) { Role dbRole = context.Role.Where(m => m.ID == role.ID && m.IsDeleted == false).FirstOrDefault(); if (dbRole != null) { dbRole.Name = role.Name; dbRole.ModifiedOn = DateTime.UtcNow; dbRole.ModifiedBy = Functions.CurrentUserID(); context.SaveChanges(); List <RightsInRole> RightsInRoleList = context.RightsInRole.Where(m => m.RoleID == role.ID).ToList(); context.RightsInRole.RemoveRange(RightsInRoleList); context.SaveChanges(); //List<AccessPermission> AllRightsList = UserRights.GetAllAccessPermission(); for (int i = 0; i < AllRightsList.Count; i++) { if (f[AllRightsList[i].Name] != null) { bool isChecked = f[AllRightsList[i].Name].Contains("true"); if (isChecked) { RightsInRole RIR = new RightsInRole(); RIR.RightsName = AllRightsList[i].Name; RIR.RoleID = dbRole.ID; context.RightsInRole.Add(RIR); context.SaveChanges(); } } } var users = context.User.Where(x => x.RoleID == role.ID).ToList(); foreach (var user in users) { aclService.SetRights(user.ID, role.ID); } TempData["SuccessMessage"] = "Role updated successfully."; } else { TempData["ErrorMessage"] = "Oops, there seems to be some problem please try again."; } return(RedirectToAction("List")); } else { TempData["ErrorMessage"] = "Role is already exist with this name. Please enter different role name."; } } return(RedirectToAction("Edit")); }