private bool DeletePartialViewMacro(string path, PartialViewType partialViewType, int userId = 0) { using (var uow = UowProvider.GetUnitOfWork()) { var repository = GetPartialViewRepository(partialViewType, uow); var partialView = repository.Get(path); if (partialView == null) { uow.Commit(); return(false); } if (uow.Events.DispatchCancelable(DeletingPartialView, this, new DeleteEventArgs <IPartialView>(partialView))) { uow.Commit(); return(false); } repository.Delete(partialView); uow.Events.Dispatch(DeletedPartialView, this, new DeleteEventArgs <IPartialView>(partialView, false)); Audit(uow, AuditType.Delete, string.Format("Delete {0} performed by user", partialViewType), userId, -1); uow.Commit(); } return(true); }
private Attempt <IPartialView?> SavePartialView(IPartialView partialView, PartialViewType partialViewType, int?userId = null) { using (ICoreScope scope = ScopeProvider.CreateCoreScope()) { EventMessages eventMessages = EventMessagesFactory.Get(); var savingNotification = new PartialViewSavingNotification(partialView, eventMessages); if (scope.Notifications.PublishCancelable(savingNotification)) { scope.Complete(); return(Attempt <IPartialView?> .Fail()); } userId ??= Constants.Security.SuperUserId; IPartialViewRepository repository = GetPartialViewRepository(partialViewType); repository.Save(partialView); Audit(AuditType.Save, userId.Value, -1, partialViewType.ToString()); scope.Notifications.Publish( new PartialViewSavedNotification(partialView, eventMessages).WithStateFrom(savingNotification)); scope.Complete(); } return(Attempt.Succeed(partialView)); }
private bool DeletePartialViewMacro(string path, PartialViewType partialViewType, int userId = 0) { using (var scope = ScopeProvider.CreateScope()) { var repository = GetPartialViewRepository(partialViewType); var partialView = repository.Get(path); if (partialView == null) { scope.Complete(); return(true); } var deleteEventArgs = new DeleteEventArgs <IPartialView>(partialView); if (scope.Events.DispatchCancelable(DeletingPartialView, this, deleteEventArgs)) { scope.Complete(); return(false); } repository.Delete(partialView); deleteEventArgs.CanCancel = false; scope.Events.Dispatch(DeletedPartialView, this, deleteEventArgs); Audit(AuditType.Delete, userId, -1, partialViewType.ToString()); scope.Complete(); } return(true); }
private bool DeletePartialViewMacro(string path, PartialViewType partialViewType, int?userId = null) { using (ICoreScope scope = ScopeProvider.CreateCoreScope()) { IPartialViewRepository repository = GetPartialViewRepository(partialViewType); IPartialView? partialView = repository.Get(path); if (partialView == null) { scope.Complete(); return(true); } EventMessages eventMessages = EventMessagesFactory.Get(); var deletingNotification = new PartialViewDeletingNotification(partialView, eventMessages); if (scope.Notifications.PublishCancelable(deletingNotification)) { scope.Complete(); return(false); } userId ??= Constants.Security.SuperUserId; repository.Delete(partialView); scope.Notifications.Publish(new PartialViewDeletedNotification(partialView, eventMessages).WithStateFrom(deletingNotification)); Audit(AuditType.Delete, userId.Value, -1, partialViewType.ToString()); scope.Complete(); } return(true); }
private IPartialViewRepository GetPartialViewRepository(PartialViewType partialViewType, IUnitOfWork uow) { switch (partialViewType) { case PartialViewType.PartialView: return _repositoryFactory.CreatePartialViewRepository(uow); case PartialViewType.PartialViewMacro: return _repositoryFactory.CreatePartialViewMacroRepository(uow); } throw new ArgumentOutOfRangeException("partialViewType"); }
private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType) { if (snippetName.IsNullOrWhiteSpace()) { throw new ArgumentNullException(nameof(snippetName)); } string partialViewHeader; switch (partialViewType) { case PartialViewType.PartialView: partialViewHeader = PartialViewHeader; break; case PartialViewType.PartialViewMacro: partialViewHeader = PartialViewMacroHeader; break; default: throw new ArgumentOutOfRangeException(nameof(partialViewType)); } var snippetProvider = new EmbeddedFileProvider(GetType().Assembly, "Umbraco.Cms.Core.EmbeddedResources.Snippets"); IFileInfo?file = snippetProvider.GetDirectoryContents(string.Empty) .FirstOrDefault(x => x.Exists && x.Name.Equals(snippetName + ".cshtml")); // Try and get the snippet path if (file is null) { throw new InvalidOperationException("Could not load snippet with name " + snippetName); } using (var snippetFile = new StreamReader(file.CreateReadStream())) { var snippetContent = snippetFile.ReadToEnd().Trim(); // strip the @inherits if it's there snippetContent = StripPartialViewHeader(snippetContent); // Update Model.Content to be Model when used as PartialView if (partialViewType == PartialViewType.PartialView) { snippetContent = snippetContent .Replace("Model.Content.", "Model.") .Replace("(Model.Content)", "(Model)"); } var content = $"{partialViewHeader}{Environment.NewLine}{snippetContent}"; return(content); } }
private IPartialViewRepository GetPartialViewRepository(PartialViewType partialViewType) { switch (partialViewType) { case PartialViewType.PartialView: return(_partialViewRepository); case PartialViewType.PartialViewMacro: return(_partialViewMacroRepository); default: throw new ArgumentOutOfRangeException(nameof(partialViewType)); } }
private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType) { if (snippetName.IsNullOrWhiteSpace()) { throw new ArgumentNullException(nameof(snippetName)); } string partialViewHeader; switch (partialViewType) { case PartialViewType.PartialView: partialViewHeader = PartialViewHeader; break; case PartialViewType.PartialViewMacro: partialViewHeader = PartialViewMacroHeader; break; default: throw new ArgumentOutOfRangeException(nameof(partialViewType)); } // Try and get the snippet path Attempt <string> snippetPathAttempt = TryGetSnippetPath(snippetName); if (snippetPathAttempt.Success == false) { throw new InvalidOperationException("Could not load snippet with name " + snippetName); } using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result))) { var snippetContent = snippetFile.ReadToEnd().Trim(); //strip the @inherits if it's there snippetContent = StripPartialViewHeader(snippetContent); //Update Model.Content to be Model when used as PartialView if (partialViewType == PartialViewType.PartialView) { snippetContent = snippetContent .Replace("Model.Content.", "Model.") .Replace("(Model.Content)", "(Model)"); } var content = $"{partialViewHeader}{Environment.NewLine}{snippetContent}"; return(content); } }
private Attempt<IPartialView> SavePartialView(IPartialView partialView, PartialViewType partialViewType, int userId = 0) { if (SavingPartialView.IsRaisedEventCancelled(new SaveEventArgs<IPartialView>(partialView), this)) return Attempt<IPartialView>.Fail(); var uow = _fileUowProvider.GetUnitOfWork(); using (var repository = GetPartialViewRepository(partialViewType, uow)) { repository.AddOrUpdate(partialView); uow.Commit(); } Audit(AuditType.Save, string.Format("Save {0} performed by user", partialViewType), userId, -1); SavedPartialView.RaiseEvent(new SaveEventArgs<IPartialView>(partialView, false), this); return Attempt.Succeed(partialView); }
private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType) { if (snippetName.IsNullOrWhiteSpace()) { throw new ArgumentNullException("snippetName"); } string partialViewHeader; switch (partialViewType) { case PartialViewType.PartialView: partialViewHeader = PartialViewHeader; break; case PartialViewType.PartialViewMacro: partialViewHeader = PartialViewMacroHeader; break; default: throw new ArgumentOutOfRangeException("partialViewType"); } // Try and get the snippet path var snippetPathAttempt = TryGetSnippetPath(snippetName); if (snippetPathAttempt.Success == false) { throw new InvalidOperationException("Could not load snippet with name " + snippetName); } using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result))) { var snippetContent = snippetFile.ReadToEnd().Trim(); //strip the @inherits if it's there snippetContent = StripPartialViewHeader(snippetContent); var content = string.Format("{0}{1}{2}", partialViewHeader, Environment.NewLine, snippetContent); return(content); } }
private Attempt <IPartialView> SavePartialView(IPartialView partialView, PartialViewType partialViewType, int userId = 0) { using (var uow = UowProvider.GetUnitOfWork()) { if (uow.Events.DispatchCancelable(SavingPartialView, this, new SaveEventArgs <IPartialView>(partialView))) { uow.Commit(); return(Attempt <IPartialView> .Fail()); } var repository = GetPartialViewRepository(partialViewType, uow); repository.AddOrUpdate(partialView); uow.Events.Dispatch(SavedPartialView, this, new SaveEventArgs <IPartialView>(partialView, false)); Audit(uow, AuditType.Save, string.Format("Save {0} performed by user", partialViewType), userId, -1); uow.Commit(); } return(Attempt.Succeed(partialView)); }
private Attempt <IPartialView> SavePartialView(IPartialView partialView, PartialViewType partialViewType, int userId = 0) { using (var scope = ScopeProvider.CreateScope()) { var saveEventArgs = new SaveEventArgs <IPartialView>(partialView); if (scope.Events.DispatchCancelable(SavingPartialView, this, saveEventArgs)) { scope.Complete(); return(Attempt <IPartialView> .Fail()); } var repository = GetPartialViewRepository(partialViewType); repository.Save(partialView); saveEventArgs.CanCancel = false; Audit(AuditType.Save, userId, -1, partialViewType.ToString()); scope.Events.Dispatch(SavedPartialView, this, saveEventArgs); scope.Complete(); } return(Attempt.Succeed(partialView)); }
private bool DeletePartialViewMacro(string path, PartialViewType partialViewType, int userId = 0) { var uow = _fileUowProvider.GetUnitOfWork(); using (var repository = GetPartialViewRepository(partialViewType, uow)) { var partialView = repository.Get(path); if (partialView == null) return true; if (DeletingPartialView.IsRaisedEventCancelled(new DeleteEventArgs<IPartialView>(partialView), this)) return false; repository.Delete(partialView); uow.Commit(); DeletedPartialView.RaiseEvent(new DeleteEventArgs<IPartialView>(partialView, false), this); Audit(AuditType.Delete, string.Format("Delete {0} performed by user", partialViewType), userId, -1); } return true; }
private Attempt<IPartialView> CreatePartialViewMacro(IPartialView partialView, PartialViewType partialViewType, string snippetName = null, int userId = 0) { if (CreatingPartialView.IsRaisedEventCancelled(new NewEventArgs<IPartialView>(partialView, true, partialView.Alias, -1), this)) return Attempt<IPartialView>.Fail(); string partialViewHeader; switch (partialViewType) { case PartialViewType.PartialView: partialViewHeader = PartialViewHeader; break; case PartialViewType.PartialViewMacro: partialViewHeader = PartialViewMacroHeader; break; default: throw new ArgumentOutOfRangeException("partialViewType"); } if (snippetName.IsNullOrWhiteSpace() == false) { //create the file var snippetPathAttempt = TryGetSnippetPath(snippetName); if (snippetPathAttempt.Success == false) { throw new InvalidOperationException("Could not load snippet with name " + snippetName); } using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result))) { var snippetContent = snippetFile.ReadToEnd().Trim(); //strip the @inherits if it's there snippetContent = StripPartialViewHeader(snippetContent); var content = string.Format("{0}{1}{2}", partialViewHeader, Environment.NewLine, snippetContent); partialView.Content = content; } } var uow = _fileUowProvider.GetUnitOfWork(); using (var repository = GetPartialViewRepository(partialViewType, uow)) { repository.AddOrUpdate(partialView); uow.Commit(); CreatedPartialView.RaiseEvent(new NewEventArgs<IPartialView>(partialView, false, partialView.Alias, -1), this); } Audit(AuditType.Save, string.Format("Save {0} performed by user", partialViewType), userId, -1); return Attempt<IPartialView>.Succeed(partialView); }
internal PartialView(PartialViewType viewType, string path, Func <File, string> getFileContent) : base(path, getFileContent) { ViewType = viewType; }
public PartialView(PartialViewType viewType, string path) : this(viewType, path, null) { }
private Attempt <IPartialView> CreatePartialViewMacro(IPartialView partialView, PartialViewType partialViewType, string snippetName = null, int userId = 0) { using (var scope = UowProvider.ScopeProvider.CreateScope()) { scope.Complete(); // always if (scope.Events.DispatchCancelable(CreatingPartialView, this, new NewEventArgs <IPartialView>(partialView, true, partialView.Alias, -1))) { return(Attempt <IPartialView> .Fail()); } } if (snippetName.IsNullOrWhiteSpace() == false) { partialView.Content = GetPartialViewMacroSnippetContent(snippetName, partialViewType); } using (var uow = UowProvider.GetUnitOfWork()) { var repository = GetPartialViewRepository(partialViewType, uow); repository.AddOrUpdate(partialView); uow.Events.Dispatch(CreatedPartialView, this, new NewEventArgs <IPartialView>(partialView, false, partialView.Alias, -1)); Audit(uow, AuditType.Save, string.Format("Save {0} performed by user", partialViewType), userId, -1); uow.Commit(); } return(Attempt <IPartialView> .Succeed(partialView)); }
private Attempt <IPartialView?> CreatePartialViewMacro(IPartialView partialView, PartialViewType partialViewType, string?snippetName = null, int?userId = Constants.Security.SuperUserId) { string partialViewHeader; switch (partialViewType) { case PartialViewType.PartialView: partialViewHeader = PartialViewHeader; break; case PartialViewType.PartialViewMacro: partialViewHeader = PartialViewMacroHeader; break; default: throw new ArgumentOutOfRangeException(nameof(partialViewType)); } string?partialViewContent = null; if (snippetName.IsNullOrWhiteSpace() == false) { //create the file Attempt <string> snippetPathAttempt = TryGetSnippetPath(snippetName); if (snippetPathAttempt.Success == false) { throw new InvalidOperationException("Could not load snippet with name " + snippetName); } using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result !))) { var snippetContent = snippetFile.ReadToEnd().Trim(); //strip the @inherits if it's there snippetContent = StripPartialViewHeader(snippetContent); //Update Model.Content. to be Model. when used as PartialView if (partialViewType == PartialViewType.PartialView) { snippetContent = snippetContent.Replace("Model.Content.", "Model."); } partialViewContent = $"{partialViewHeader}{Environment.NewLine}{snippetContent}"; } } using (ICoreScope scope = ScopeProvider.CreateCoreScope()) { EventMessages eventMessages = EventMessagesFactory.Get(); var creatingNotification = new PartialViewCreatingNotification(partialView, eventMessages); if (scope.Notifications.PublishCancelable(creatingNotification)) { scope.Complete(); return(Attempt <IPartialView?> .Fail()); } IPartialViewRepository repository = GetPartialViewRepository(partialViewType); if (partialViewContent != null) { partialView.Content = partialViewContent; } repository.Save(partialView); scope.Notifications.Publish(new PartialViewCreatedNotification(partialView, eventMessages).WithStateFrom(creatingNotification)); Audit(AuditType.Save, userId !.Value, -1, partialViewType.ToString()); scope.Complete(); } return(Attempt <IPartialView?> .Succeed(partialView)); }
private Attempt <IPartialView> CreatePartialViewMacro(IPartialView partialView, PartialViewType partialViewType, string snippetName = null, int userId = 0) { string partialViewHeader; switch (partialViewType) { case PartialViewType.PartialView: partialViewHeader = PartialViewHeader; break; case PartialViewType.PartialViewMacro: partialViewHeader = PartialViewMacroHeader; break; default: throw new ArgumentOutOfRangeException(nameof(partialViewType)); } string partialViewContent = null; if (snippetName.IsNullOrWhiteSpace() == false) { //create the file var snippetPathAttempt = TryGetSnippetPath(snippetName); if (snippetPathAttempt.Success == false) { throw new InvalidOperationException("Could not load snippet with name " + snippetName); } using (var snippetFile = new StreamReader(System.IO.File.OpenRead(snippetPathAttempt.Result))) { var snippetContent = snippetFile.ReadToEnd().Trim(); //strip the @inherits if it's there snippetContent = StripPartialViewHeader(snippetContent); partialViewContent = $"{partialViewHeader}{Environment.NewLine}{snippetContent}"; } } using (var scope = ScopeProvider.CreateScope()) { var newEventArgs = new NewEventArgs <IPartialView>(partialView, true, partialView.Alias, -1); if (scope.Events.DispatchCancelable(CreatingPartialView, this, newEventArgs)) { scope.Complete(); return(Attempt <IPartialView> .Fail()); } var repository = GetPartialViewRepository(partialViewType); if (partialViewContent != null) { partialView.Content = partialViewContent; } repository.Save(partialView); newEventArgs.CanCancel = false; scope.Events.Dispatch(CreatedPartialView, this, newEventArgs); Audit(AuditType.Save, userId, -1, partialViewType.ToString()); scope.Complete(); } return(Attempt <IPartialView> .Succeed(partialView)); }
public PartialView(PartialViewType viewType, string path, Func <File, string?>?getFileContent) : base(path, getFileContent) { ViewType = viewType; }