public ActionResult EditBaskets(UserPermissionsModel model) { if (ModelState.IsValid) { if (Authorized(RoleType.SystemManager)) { User userFromDB; List<Budgets_UsersToBaskets> existingPermissions; bool noErrors = true; using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId)) using (UsersToBasketsRepository userPermissionsRep = new UsersToBasketsRepository()) { userFromDB = usersRep.GetEntity(model.UserId); if (userFromDB != null) { if (userFromDB.CompanyId == CurrentUser.CompanyId) { existingPermissions = userPermissionsRep.GetList().Where(x => x.UserId == userFromDB.Id).ToList(); if (existingPermissions != null) { if (model.UserPermissions == null) return RedirectToAction("Index"); foreach (var permission in model.UserPermissions) { if (permission.IsActive) { if (!existingPermissions.Any(x => x.BasketId == permission.Permission.Id)) { Budgets_UsersToBaskets newPermission = new Budgets_UsersToBaskets() { UserId = userFromDB.Id, BasketId = permission.Permission.Id, CompanyId = CurrentUser.CompanyId }; if (!userPermissionsRep.Create(newPermission)) noErrors = false; } } else { Budgets_UsersToBaskets existingPermission = existingPermissions.SingleOrDefault(x => x.BasketId == permission.Permission.Id); if (existingPermission != null) { if (!userPermissionsRep.Delete(existingPermission.Id)) noErrors = false; } } } if (noErrors) return RedirectToAction("Index"); else return Error(Loc.Dic.error_user_edit_permissions_error); } else { return Error(Loc.Dic.error_database_error); } } else { return Error(Loc.Dic.error_no_permission); } } else { return Error(Loc.Dic.error_database_error); } } } else { return Error(Loc.Dic.error_no_permission); } } else { return Error(ModelState); } }
public ActionResult EditBaskets(int id = 0) { if (!Authorized(RoleType.SystemManager)) return Error(Loc.Dic.error_no_permission); UserPermissionsModel model = new UserPermissionsModel(); User user; List<Budgets_Baskets> allPermissions; using (UsersRepository usersRep = new UsersRepository(CurrentUser.CompanyId)) using (BudgetsPermissionsRepository permissionsRep = new BudgetsPermissionsRepository()) { user = usersRep.GetEntity(id); if (user == null) return Error(Loc.Dic.error_users_get_error); model.User = user; model.UserPermissions = user.Budgets_UsersToBaskets.Select(x => new UserPermission() { Permission = x.Budgets_Baskets, IsActive = true }).Where(x => x.Permission.CompanyId == CurrentUser.CompanyId).ToList(); if (model.UserPermissions == null) return Error(Loc.Dic.error_permissions_get_error); allPermissions = permissionsRep.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId).ToList(); if (allPermissions == null) return Error(Loc.Dic.error_database_error); List<Budgets_Baskets> allWithoutUserBasket = new List<Budgets_Baskets>(); foreach (Budgets_Baskets basket in allPermissions) allWithoutUserBasket.Add(basket); foreach (UserPermission UserBasket in model.UserPermissions) foreach (Budgets_Baskets basket in allPermissions) if (UserBasket.Permission.Id == basket.Id) allWithoutUserBasket.Remove(basket); model.UserId = user.Id; model.PermissionsSelectList = new SelectList(allWithoutUserBasket, "Id", "Name"); return View(model); } }