public async Task <IActionResult> SavePermission([FromBody] SavePermissionRequest data) { if (!ModelState.IsValid) { IEnumerable <ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors); return(new BadRequestObjectResult(allErrors)); } try { _permissionService.DeleteAll(data.FunctionId); foreach (var item in data.Permissions) { var permissionVm = new PermissionViewModel() { CanCreate = item.CanCreate, CanDelete = item.CanDelete, CanRead = item.CanRead, CanUpdate = item.CanUpdate, RoleId = item.RoleId, FunctionId = data.FunctionId }; _permissionService.Add(permissionVm); } var functions = _functionService.GetAllWithParentId(data.FunctionId); if (functions.Any()) { foreach (var item in functions) { _permissionService.DeleteAll(item.Id); foreach (var p in data.Permissions) { var childPermissionVm = new PermissionViewModel(); childPermissionVm.FunctionId = item.Id; childPermissionVm.RoleId = p.RoleId; childPermissionVm.CanRead = p.CanRead; childPermissionVm.CanCreate = p.CanCreate; childPermissionVm.CanDelete = p.CanDelete; childPermissionVm.CanUpdate = p.CanUpdate; _permissionService.Add(childPermissionVm); } } } _permissionService.SaveChange(); return(new NoContentResult()); } catch (Exception ex) { return(new BadRequestObjectResult(ex.Message)); } }
public HttpResponseMessage SavePermission(HttpRequestMessage request, SavePermissionRequest data) { if (ModelState.IsValid) { _permissionService.DeleteAll(data.FunctionId); Permission permission = null; foreach (var item in data.Permissions) { permission = new Permission(); permission.UpdatePermission(item); permission.FunctionId = data.FunctionId; _permissionService.Add(permission); } var functions = _functionService.GetAllWithParentId(data.FunctionId); if (functions.Any()) { foreach (var item in functions) { _permissionService.DeleteAll(item.ID); foreach (var p in data.Permissions) { var childPermission = new Permission(); childPermission.FunctionId = item.ID; childPermission.RoleId = p.RoleId; childPermission.CanRead = p.CanRead; childPermission.CanCreate = p.CanCreate; childPermission.CanDelete = p.CanDelete; childPermission.CanUpdate = p.CanUpdate; _permissionService.Add(childPermission); } } } try { _permissionService.SaveChange(); return(request.CreateResponse(HttpStatusCode.OK, "Lưu quyền thành cống")); } catch (Exception ex) { return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } } else { return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
public async Task <IActionResult> SavePermission([FromBody] SavePermissionRequest data) { var hasPermission = await _authorizationService.AuthorizeAsync(User, "FUNCTION", Operations.Create); if (hasPermission.Succeeded == false) { return(new BadRequestObjectResult(CommonConstants.Forbidden)); } if (ModelState.IsValid) { _permissionService.DeleteAll(data.FunctionId); foreach (var permissionVm in data.Permissions) { permissionVm.FunctionId = data.FunctionId; Permission permissionDb = new Permission(); permissionDb.UpdatePermission(permissionVm); _permissionService.AddDb(permissionDb); } List <FunctionViewModel> childFunctions = _functionService.GetAllWithParentId(data.FunctionId); if (childFunctions.Count() > 0) { foreach (var childFunction in childFunctions) { _permissionService.DeleteAll(childFunction.Id); foreach (var permissionVm in data.Permissions) { permissionVm.FunctionId = childFunction.Id; Permission permissionDb = new Permission(); permissionDb.UpdatePermission(permissionVm); _permissionService.AddDb(permissionDb); } } } _permissionService.SaveChanges(); return(new OkObjectResult("Success")); } return(new BadRequestObjectResult(ModelState)); }