public ActionResult CountryRoleDetails(long?id) { if (!Permission.CanUpdatePermissions) { return(new HttpUnauthorizedResult("User does not have permission to manage permissions")); } Context context = new Context(); CountryRole role = context.CountryRoles.FirstOrDefault(cr => cr.CountryRoleID == id); if (role == null) { return(HttpNotFound("Could not find Role")); } ViewData["Users"] = context.Users.ToList(); return(View(role)); }
public ActionResult CountryRoleDelete(long?id) { if (!permission.CanAddCountries) { return(new HttpUnauthorizedResult("User does not have permission to Add Country Roles")); } Context context = new Context(); CountryRole role = context.CountryRoles.FirstOrDefault(cr => cr.CountryRoleID == id); if (role == null) { return(HttpNotFound("Could not find Country Role")); } context.CountryRoles.Remove(role); context.SaveChanges(); return(RedirectToAction("CountryDetails", new { id = role.CountryID })); }
public ActionResult CountryRoleDelete(long?id) { if (!Permission.CanUpdatePermissions) { return(new HttpUnauthorizedResult("User does not have access to update permissions")); } Context context = new Context(); CountryRole countryRole = context.CountryRoles.FirstOrDefault(cr => cr.CountryRoleID == id); if (countryRole == null) { return(HttpNotFound("Could not find Role")); } context.CountryRoles.Remove(countryRole); context.SaveChanges(); return(RedirectToAction("CountryIndex", new { countryid = CountryID })); }
public ActionResult CountryRoleDetails(CountryRole countryRole) { if (!Permission.CanUpdatePermissions) { return(new HttpUnauthorizedResult("User does not have permission to manage permissions")); } if (countryRole.CountryRoleID == null) { throw new NotImplementedException("Country Role creation should go through modal"); } Context context = new Context(); CountryRole databaseCountryRole = context.CountryRoles.FirstOrDefault(cr => cr.CountryRoleID == countryRole.CountryRoleID); if (databaseCountryRole == null) { return(HttpNotFound("Country Role not found")); } databaseCountryRole.Name = countryRole.Name; databaseCountryRole.CanAddPages = countryRole.CanAddPages; databaseCountryRole.CanDeletePages = countryRole.CanDeletePages; databaseCountryRole.CanUpdatePermissions = countryRole.CanUpdatePermissions; try { context.SaveChanges(); } catch (DbEntityValidationException ex) { ModelState.AddErrors(ex); } ViewData["SaveSuccessful"] = ModelState.IsValid; ViewData["Users"] = context.Users.ToList(); return(View(databaseCountryRole)); }
protected override void PreActionCheck(ActionExecutingContext filterContext, SessionCache.CachedSession cachedSession) { if (!RouteData.Values.Keys.Contains("countryid")) { if (filterContext.ActionDescriptor.ActionName == "Index") { Permission = null; return; } else { filterContext.Result = HttpNotFound("Country ID was not supplied"); } } if (!long.TryParse(RouteData.Values["countryid"] as string, out long countryID)) { filterContext.Result = HttpNotFound("CountryID is not valid"); return; } CountryID = countryID; Context context = new Context(); IEnumerable <CountryRole> countryRoles = context.Users.First(u => u.UserID == UserID).CountryRoleUsers.Where(cru => cru.CountryRole.CountryID == CountryID).Select(cru => cru.CountryRole); if (!countryRoles.Any(cr => cr.CountryID == CountryID)) { filterContext.Result = new HttpUnauthorizedResult("User does not have access to edit this country"); } Permission = new CountryRole(); Permission.CanAddPages = countryRoles.Any(cr => cr.CountryID == CountryID && cr.CanAddPages); Permission.CanDeletePages = countryRoles.Any(cr => cr.CountryID == CountryID && cr.CanDeletePages); Permission.CanUpdatePermissions = countryRoles.Any(cr => cr.CountryID == CountryID && cr.CanUpdatePermissions); filterContext.Controller.ViewData["Permission"] = Permission; }
public ActionResult CountryRoleAdd(CountryRole countryRole) { if (!Permission.CanUpdatePermissions) { return(new HttpUnauthorizedResult("User does not have permission to add Country Roles")); } countryRole.CountryRoleUsers = new List <CountryRoleUser>(); foreach (string key in Request.Form.AllKeys.Where(key => key.StartsWith("CountryRoleUser-"))) { if (!int.TryParse(Request.Form[key], out int userID)) { continue; } CountryRoleUser countryRoleUser = new CountryRoleUser(); countryRoleUser.UserID = userID; countryRole.CountryRoleUsers.Add(countryRoleUser); } Context context = new Context(); context.CountryRoles.Add(countryRole); try { context.SaveChanges(); } catch (DbEntityValidationException ex) { return(ModalSaveFailedResult(ex)); } return(Json(new { success = true })); }
public ActionResult CountryRoleAdd(CountryRole countryRole) { if (!permission.CanAddCountries) { return(new HttpUnauthorizedResult("User does not have permission to Add Country Roles")); } Context context = new Context(); using (DbContextTransaction transaction = context.Database.BeginTransaction()) { try { context.CountryRoles.Add(countryRole); try { context.SaveChanges(); } catch (DbEntityValidationException ex) { return(ModalSaveFailedResult(ex)); } Dictionary <string, string> errors = new Dictionary <string, string>(); Dictionary <long?, string> keyByUserID = new Dictionary <long?, string>(); foreach (string key in Request.Form.AllKeys.Where(aKey => aKey.StartsWith("CountryRoleUser-"))) { if (!long.TryParse(Request.Form[key], out long userID)) { errors.Add(key, "User ID is not valid"); continue; } CountryRoleUser countryRoleUser = new CountryRoleUser(); countryRoleUser.CountryRoleID = countryRole.CountryRoleID.Value; countryRoleUser.UserID = userID; context.CountryRoleUsers.Add(countryRoleUser); if (keyByUserID.ContainsKey(userID)) { errors.Add(key, "User must be unique"); } keyByUserID[userID] = key; } if (errors.Any()) { return(Json(new { success = false, errors })); } try { context.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult entityValidationResult in ex.EntityValidationErrors) { long?userID = entityValidationResult.Entry.Property("UserID").CurrentValue as long?; if (keyByUserID.ContainsKey(userID)) { errors[keyByUserID[userID]] = entityValidationResult.ValidationErrors.FirstOrDefault().ErrorMessage; } } } if (errors.Any()) { return(Json(new { success = false, errors })); } transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } return(Json(new { success = true })); }