public ActionResult Edit(ApprovalRouteModel model) { if (!Authorized(RoleType.SystemManager)) return Error(Loc.Dic.error_no_permission); if (!ModelState.IsValid) return Error(Loc.Dic.error_invalid_form); Users_ApprovalRoutes newApprovalRoute; using (ApprovalRoutesRepository routesRep = new ApprovalRoutesRepository(CurrentUser.CompanyId)) using (ApprovalStepsRepository stepsRep = new ApprovalStepsRepository()) using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId)) { newApprovalRoute = routesRep.GetEntity(model.ApprovalRoute.Id, "Users_ApprovalStep"); if (newApprovalRoute == null) return Error(Loc.Dic.error_database_error); var usersIds = model.Steps.Select(x => x.UserId).Distinct(); List<User> routeApprovers = usersRep.GetList().Where(x => usersIds.Contains(x.Id)).ToList(); if (usersIds.Count() != routeApprovers.Count) return Error(Loc.Dic.error_invalid_form); foreach (var approver in routeApprovers) { if (!Roles.HasRole(approver.Roles, RoleType.OrdersApprover)) return Error(Loc.Dic.error_invalid_form); } foreach (var step in newApprovalRoute.Users_ApprovalStep) { stepsRep.Delete(step.Id); } foreach (var step in model.Steps) { newApprovalRoute.Users_ApprovalStep.Add( new Users_ApprovalStep() { UserId = step.UserId, StepNumber = step.StepNumber } ); } if (routesRep.Update(newApprovalRoute) == null) return Error(Loc.Dic.error_database_error); } return RedirectToAction("Index"); }
public ActionResult Edit(ApprovalRouteModel model) { if (!Authorized(RoleType.SystemManager)) { return(Error(Loc.Dic.error_no_permission)); } if (!ModelState.IsValid) { return(Error(Loc.Dic.error_invalid_form)); } Users_ApprovalRoutes newApprovalRoute; using (ApprovalRoutesRepository routesRep = new ApprovalRoutesRepository(CurrentUser.CompanyId)) using (ApprovalStepsRepository stepsRep = new ApprovalStepsRepository()) using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId)) { newApprovalRoute = routesRep.GetEntity(model.ApprovalRoute.Id, "Users_ApprovalStep"); if (newApprovalRoute == null) { return(Error(Loc.Dic.error_database_error)); } var usersIds = model.Steps.Select(x => x.UserId).Distinct(); List <User> routeApprovers = usersRep.GetList().Where(x => usersIds.Contains(x.Id)).ToList(); if (usersIds.Count() != routeApprovers.Count) { return(Error(Loc.Dic.error_invalid_form)); } foreach (var approver in routeApprovers) { if (!Roles.HasRole(approver.Roles, RoleType.OrdersApprover)) { return(Error(Loc.Dic.error_invalid_form)); } } foreach (var step in newApprovalRoute.Users_ApprovalStep) { stepsRep.Delete(step.Id); } foreach (var step in model.Steps) { newApprovalRoute.Users_ApprovalStep.Add( new Users_ApprovalStep() { UserId = step.UserId, StepNumber = step.StepNumber } ); } if (routesRep.Update(newApprovalRoute) == null) { return(Error(Loc.Dic.error_database_error)); } } return(RedirectToAction("Index")); }