public async Task <ActionResult> SubverseStylesheetEditor(SubverseStylesheetViewModel model) { try { if (!ModelState.IsValid) { SetNavigationViewModel(model.Name); return(View("~/Views/Subverses/Admin/SubverseStylesheetEditor.cshtml")); } var existingSubverse = _db.Subverse.FirstOrDefault(x => x.Name.ToUpper() == model.Name.ToUpper()); // check if subverse exists before attempting to edit it if (existingSubverse != null) { SetNavigationViewModel(model.Name); // check if user requesting edit is authorized to do so for current subverse // check that the user requesting to edit subverse settings is subverse owner! if (!ModeratorPermission.HasPermission(User, existingSubverse.Name, Domain.Models.ModeratorAction.ModifyCSS)) { return(new EmptyResult()); } if (!String.IsNullOrEmpty(model.Stylesheet)) { if (model.Stylesheet.Length < 50001) { existingSubverse.Stylesheet = model.Stylesheet; } else { ModelState.AddModelError(string.Empty, "Sorry, custom CSS limit is set to 50000 characters."); return(View("~/Views/Subverses/Admin/SubverseStylesheetEditor.cshtml")); } } else { existingSubverse.Stylesheet = model.Stylesheet; } await _db.SaveChangesAsync(); //purge new minified CSS CacheHandler.Instance.Remove(CachingKey.SubverseStylesheet(existingSubverse.Name)); CacheHandler.Instance.Remove(CachingKey.Subverse(existingSubverse.Name)); // go back to this subverse return(RedirectToRoute(Models.ROUTE_NAMES.SUBVERSE_INDEX, new { subverse = model.Name })); } ModelState.AddModelError(string.Empty, "Sorry, The subverse you are trying to edit does not exist."); return(View("~/Views/Subverses/Admin/SubverseStylesheetEditor.cshtml", model)); } catch (Exception ex) { EventLogger.Instance.Log(ex); ModelState.AddModelError(string.Empty, "Something bad happened."); return(View("~/Views/Subverses/Admin/SubverseStylesheetEditor.cshtml", model)); } }
public async Task <ActionResult> Update(SubverseSettingsViewModel updatedModel) { try { if (!ModelState.IsValid) { SetNavigationViewModel(updatedModel.Name); return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel)); } var existingSubverse = _db.Subverse.FirstOrDefault(x => x.Name.ToUpper() == updatedModel.Name.ToUpper()); // check if subverse exists before attempting to edit it if (existingSubverse != null) { SetNavigationViewModel(existingSubverse.Name); // check if user requesting edit is authorized to do so for current subverse if (!ModeratorPermission.HasPermission(User, updatedModel.Name, Domain.Models.ModeratorAction.ModifySettings)) { return(new EmptyResult()); } //check description for banned domains if (BanningUtility.ContentContainsBannedDomain(existingSubverse.Name, updatedModel.Description)) { ModelState.AddModelError(string.Empty, "Sorry, description text contains banned domains."); return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel)); } //check sidebar for banned domains if (BanningUtility.ContentContainsBannedDomain(existingSubverse.Name, updatedModel.SideBar)) { ModelState.AddModelError(string.Empty, "Sorry, sidebar text contains banned domains."); return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel)); } // TODO investigate if EntityState is applicable here and use that instead // db.Entry(updatedModel).State = EntityState.Modified; existingSubverse.Title = updatedModel.Title; existingSubverse.Description = updatedModel.Description; existingSubverse.SideBar = updatedModel.SideBar; //if (updatedModel.Stylesheet != null) //{ // if (updatedModel.Stylesheet.Length < 50001) // { // existingSubverse.Stylesheet = updatedModel.Stylesheet; // } // else // { // ModelState.AddModelError(string.Empty, "Sorry, custom CSS limit is set to 50000 characters."); // return View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel); // } //} //else //{ // existingSubverse.Stylesheet = updatedModel.Stylesheet; //} existingSubverse.IsAdult = updatedModel.IsAdult; existingSubverse.IsThumbnailEnabled = updatedModel.IsThumbnailEnabled; existingSubverse.IsAuthorizedOnly = updatedModel.IsAuthorizedOnly; existingSubverse.ExcludeSitewideBans = updatedModel.ExcludeSitewideBans; //Only update if time lock has expired if (existingSubverse.LastUpdateDate == null || (Repository.CurrentDate.Subtract(existingSubverse.LastUpdateDate.Value) > TimeSpan.FromHours(VoatSettings.Instance.SubverseUpdateTimeLockInHours))) { existingSubverse.MinCCPForDownvote = updatedModel.MinCCPForDownvote; existingSubverse.IsPrivate = updatedModel.IsPrivate; } // these properties are currently not implemented but they can be saved and edited for future use //existingSubverse.Type = updatedModel.Type; //existingSubverse.SubmitLinkLabel = updatedModel.SubmitLinkLabel; //existingSubverse.SubmitPostLabel = updatedModel.SubmitPostLabel; //existingSubverse.SubmissionText = updatedModel.SubmissionText; //existingSubverse.IsDefaultAllowed = updatedModel.IsDefaultAllowed; //if (existingSubverse.IsAnonymized == true && updatedModel.IsAnonymized == false) //{ // ModelState.AddModelError(string.Empty, "Sorry, this subverse is permanently locked to anonymized mode."); // return View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel); //} // only subverse owners should be able to convert a sub to anonymized mode if (ModeratorPermission.IsLevel(User, updatedModel.Name, Domain.Models.ModeratorLevel.Owner)) { existingSubverse.IsAnonymized = updatedModel.IsAnonymized; } existingSubverse.LastUpdateDate = Repository.CurrentDate; await _db.SaveChangesAsync(); //purge new minified CSS CacheHandler.Instance.Remove(CachingKey.SubverseStylesheet(existingSubverse.Name)); //purge subvere CacheHandler.Instance.Remove(CachingKey.Subverse(existingSubverse.Name)); // go back to this subverse return(RedirectToRoute(Models.ROUTE_NAMES.SUBVERSE_INDEX, new { subverse = updatedModel.Name })); // user was not authorized to commit the changes, drop attempt } ModelState.AddModelError(string.Empty, "Sorry, The subverse you are trying to edit does not exist."); return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel)); } catch (Exception ex) { EventLogger.Instance.Log(ex); ModelState.AddModelError(string.Empty, "Something bad happened."); return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", updatedModel)); } }