public RoleAccessControlObjectEditModel Get(long roleId) { if (roleId < 1) { throw new InvalidOperationException(); } var role = _roleRepository.GetByRoleId(roleId); var accessControlObjects = GetListofAccessControlObject(role, _accessControlObjectRepository.GetRootAccessControlObjects().ToArray()); var roleAccessControlObjects = _roleAccessControlObjectRepository.GetRoleAccessControlObjectByRoleId(roleId).ToArray(); var model = new RoleAccessControlObjectEditModel { RoleId = role.Id, RoleName = role.DisplayName, AccessControlObjects = CreateViewModel(accessControlObjects, roleAccessControlObjects).OrderBy(x => x.DisplayOrder).ToArray() }; //if (role.ParentId != null && role.ParentId > 0) //{ //var parentRole = _roleRepository.GetByRoleId(role.ParentId.Value); //var parentRoleAccessControlObjects = _roleAccessControlObjectRepository.GetRoleAccessControlObjectByRoleId(parentRole.Id).ToArray(); //DisableAccessControlNotInBaseRole(model.AccessControlObjects, parentRoleAccessControlObjects); //} return(model); }
//private void DisableAccessControlNotInBaseRole(IList<AccessControlObjectViewModel> model, IEnumerable<RoleAccessControlObject> parentRoleAccessControlObject) //{ // foreach (var accessControl in model) // { // int count = parentRoleAccessControlObject.Count(x => x.AccessControlObject.Id == accessControl.Id); // accessControl.IsAvailableInParent = count > 0; // if (accessControl.Children.Any()) // { // DisableAccessControlNotInBaseRole(accessControl.Children, parentRoleAccessControlObject); // } // } //} //private void UpdateChildAccessControlObjects(long parentRoleId) //{ // var parentRoleAccessControlObjects = _roleAccessControlObjectRepository.GetRoleAccessControlObjectByRoleId(parentRoleId); // var childRoles = _roleRepository.GetRolesByParentId(parentRoleId); // foreach (var role in childRoles) // { // var roleAccessControlObjects = _roleAccessControlObjectRepository.GetRoleAccessControlObjectByRoleId(role.Id); // foreach (var rac in roleAccessControlObjects.Where(y => !(parentRoleAccessControlObjects.Select(x => x.AccessControlObject.Id)).Contains(y.AccessControlObject.Id))) // { // rac.PermissionType = PermissionType.Deny; // _roleAccessControlObjectRepository.SaveRoleAccessControlObject(rac); // //_roleAccessControlObjectRepository.DeleteRoleAccessControlObject(rac); // } // } //} public void Save(RoleAccessControlObjectEditModel model) { if (model.RoleId < 1) { throw new InvalidOperationException(); } SaveWithoutUpdatingChildRole(model); //UpdateChildAccessControlObjects(model.RoleId); }
private void SaveWithoutUpdatingChildRole(RoleAccessControlObjectEditModel model) { var roleAccessControlObjects = _roleAccessControlObjectRepository.GetRoleAccessControlObjectByRoleId(model.RoleId).ToArray(); var objsToSave = GetSelectedObjects(model.AccessControlObjects); var objAccessIds = objsToSave.Select(x => x.FirstValue).ToArray(); var objsToDelete = roleAccessControlObjects.Where(x => !objAccessIds.Contains(x.AccessControlObject.Id)).ToArray(); foreach (RoleAccessControlObject roleAccessControlObject in objsToDelete) { roleAccessControlObject.PermissionType = PermissionType.Deny; _roleAccessControlObjectRepository.SaveRoleAccessControlObject(roleAccessControlObject); //_roleAccessControlObjectRepository.DeleteRoleAccessControlObject(roleAccessControlObject); } foreach (var item in objsToSave) { var obj = roleAccessControlObjects.SingleOrDefault(x => x.AccessControlObject.Id == item.FirstValue); if (obj != null) { obj.DataScope = (DataScope)item.SecondValue; obj.PermissionType = PermissionType.Allow; _roleAccessControlObjectRepository.SaveRoleAccessControlObject(obj); } else { obj = new RoleAccessControlObject { AccessControlObject = _accessControlObjectRepository.GetAccessControlObjectById(item.FirstValue), DataScope = (DataScope)item.SecondValue, Role = new Role(model.RoleId), PermissionType = PermissionType.Allow }; _roleAccessControlObjectRepository.SaveRoleAccessControlObject(obj); } } }
public virtual ActionResult Edit(RoleAccessControlObjectEditModel model) { if (!ModelState.IsValid) { return(PartialView(model)); } try { _service.Save(model); AccessControlCacheHelper.BuildSystemCache(model.RoleId); model.FeedbackMessage = FeedbackMessageModel.CreateSuccessMessage("Saved Successfully."); } catch (Exception ex) { _logger.Error(ex.Message); model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("Some Error Occured."); } model.AccessControlObjects = _service.Get(model.RoleId).AccessControlObjects; return(PartialView("Index", model)); }