public ActionResult Update(string subverse) { var subverseObject = DataCache.Subverse.Retrieve(subverse); if (subverseObject == null) { ViewBag.SelectedSubverse = "404"; return(ErrorView(ErrorViewModel.GetErrorViewModel(ErrorType.SubverseNotFound))); } if (!ModeratorPermission.HasPermission(User, subverse, Domain.Models.ModeratorAction.ModifySettings)) { return(RedirectToRoute(Models.ROUTE_NAMES.SUBVERSE_INDEX, new { subverse = subverse })); } // map existing data to view model for editing and pass it to frontend // NOTE: we should look into a mapper which automatically maps these properties to corresponding fields to avoid tedious manual mapping var viewModel = new SubverseSettingsViewModel { Name = subverseObject.Name, Title = subverseObject.Title, Description = subverseObject.Description, SideBar = subverseObject.SideBar, //Stylesheet = subverseObject.Stylesheet, IsAdult = subverseObject.IsAdult, IsPrivate = subverseObject.IsPrivate, IsThumbnailEnabled = subverseObject.IsThumbnailEnabled, ExcludeSitewideBans = subverseObject.ExcludeSitewideBans, IsAuthorizedOnly = subverseObject.IsAuthorizedOnly, IsAnonymized = subverseObject.IsAnonymized, MinCCPForDownvote = subverseObject.MinCCPForDownvote, LastUpdateDate = subverseObject.LastUpdateDate }; ViewBag.SelectedSubverse = string.Empty; ViewBag.SubverseName = subverseObject.Name; SetNavigationViewModel(subverseObject.Name); return(View("~/Views/Subverses/Admin/SubverseSettings.cshtml", viewModel)); }
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)); } }