From b60c0377fb6089171cc3d6c584e75d856c69fbd7 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Tue, 16 Mar 2021 16:13:15 +0000 Subject: [PATCH 1/7] Uninstall package event/notificiation ported --- .../Events/UninstallPackageNotification.cs | 16 ++++++++++++++++ .../Services/Implement/PackagingService.cs | 15 +++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 src/Umbraco.Core/Events/UninstallPackageNotification.cs diff --git a/src/Umbraco.Core/Events/UninstallPackageNotification.cs b/src/Umbraco.Core/Events/UninstallPackageNotification.cs new file mode 100644 index 0000000000..9ad71c800c --- /dev/null +++ b/src/Umbraco.Core/Events/UninstallPackageNotification.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using Umbraco.Cms.Core.Packaging; + +namespace Umbraco.Cms.Core.Events +{ + public class UninstallPackageNotification : ICancelableNotification + { + public UninstallPackageNotification(IEnumerable uninstallationSummary) => UninstallationSummary = uninstallationSummary; + + public IEnumerable UninstallationSummary { get; } + + public bool Cancel { get; set; } + } +} + + diff --git a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs index 4a6e1eb8e3..67766359d5 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -22,6 +22,7 @@ namespace Umbraco.Cms.Core.Services.Implement { private readonly IPackageInstallation _packageInstallation; private readonly IHostingEnvironment _hostingEnvironment; + private readonly IEventAggregator _eventAggregator; private readonly IAuditService _auditService; private readonly ICreatedPackagesRepository _createdPackages; private readonly IInstalledPackagesRepository _installedPackages; @@ -32,13 +33,15 @@ namespace Umbraco.Cms.Core.Services.Implement ICreatedPackagesRepository createdPackages, IInstalledPackagesRepository installedPackages, IPackageInstallation packageInstallation, - IHostingEnvironment hostingEnvironment) + IHostingEnvironment hostingEnvironment, + IEventAggregator eventAggregator) { _auditService = auditService; _createdPackages = createdPackages; _installedPackages = installedPackages; _packageInstallation = packageInstallation; _hostingEnvironment = hostingEnvironment; + _eventAggregator = eventAggregator; } #region Package Files @@ -168,7 +171,7 @@ namespace Umbraco.Cms.Core.Services.Implement } // trigger the UninstalledPackage event - UninstalledPackage.RaiseEvent(new UninstallPackageEventArgs(allSummaries, false), this); + _eventAggregator.Publish(new UninstallPackageNotification(allSummaries)); return summary; } @@ -249,11 +252,7 @@ namespace Umbraco.Cms.Core.Services.Implement /// public static event TypedEventHandler> ImportedPackage; - /// - /// Occurs after a package is uninstalled - /// - public static event TypedEventHandler UninstalledPackage; - + #endregion From 1b12e368b24d8cf0ea36a3ff3826742ebe726d43 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Wed, 17 Mar 2021 09:51:43 +0000 Subject: [PATCH 2/7] Adds ImportPackageNotification/Event --- .../Events/ImportPackageNotification.cs | 21 +++++++++++++++++++ .../Services/Implement/PackagingService.cs | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/Umbraco.Core/Events/ImportPackageNotification.cs diff --git a/src/Umbraco.Core/Events/ImportPackageNotification.cs b/src/Umbraco.Core/Events/ImportPackageNotification.cs new file mode 100644 index 0000000000..0430e10c90 --- /dev/null +++ b/src/Umbraco.Core/Events/ImportPackageNotification.cs @@ -0,0 +1,21 @@ +using Umbraco.Cms.Core.Models.Packaging; +using Umbraco.Cms.Core.Packaging; + +namespace Umbraco.Cms.Core.Events +{ + public class ImportPackageNotification : ICancelableNotification + { + + public ImportPackageNotification(InstallationSummary installationSummary, IPackageInfo packageMetaData) + { + InstallationSummary = installationSummary; + PackageMetaData = packageMetaData; + } + + public IPackageInfo PackageMetaData { get; } + + public InstallationSummary InstallationSummary { get; } + + public bool Cancel { get; set; } + } +} diff --git a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs index 67766359d5..75bd0b8ced 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs @@ -129,7 +129,8 @@ namespace Umbraco.Cms.Core.Services.Implement _auditService.Add(AuditType.PackagerInstall, userId, -1, "Package", $"Package data installed for package '{compiledPackage.Name}'."); - ImportedPackage.RaiseEvent(new ImportPackageEventArgs(summary, compiledPackage, false), this); + // trigger the ImportPackage event + _eventAggregator.Publish(new ImportPackageNotification(summary, compiledPackage)); return summary; } From 9c42d4792e43f3c1a601fd77b3741dbb09f326b6 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Wed, 17 Mar 2021 10:17:05 +0000 Subject: [PATCH 3/7] Remove old events --- .../Events/ImportPackageEventArgs.cs | 59 ------------------- .../Events/UninstallPackageEventArgs.cs | 15 ----- 2 files changed, 74 deletions(-) delete mode 100644 src/Umbraco.Core/Events/ImportPackageEventArgs.cs delete mode 100644 src/Umbraco.Core/Events/UninstallPackageEventArgs.cs diff --git a/src/Umbraco.Core/Events/ImportPackageEventArgs.cs b/src/Umbraco.Core/Events/ImportPackageEventArgs.cs deleted file mode 100644 index 5b04bff318..0000000000 --- a/src/Umbraco.Core/Events/ImportPackageEventArgs.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.Generic; -using Umbraco.Cms.Core.Models.Packaging; - -namespace Umbraco.Cms.Core.Events -{ - public class ImportPackageEventArgs : CancellableEnumerableObjectEventArgs, IEquatable> - { - public ImportPackageEventArgs(TEntity eventObject, IPackageInfo packageMetaData, bool canCancel) - : base(new[] { eventObject }, canCancel) - { - PackageMetaData = packageMetaData ?? throw new ArgumentNullException(nameof(packageMetaData)); - } - - public ImportPackageEventArgs(TEntity eventObject, IPackageInfo packageMetaData) - : this(eventObject, packageMetaData, true) - { - - } - - public IPackageInfo PackageMetaData { get; } - - public IEnumerable InstallationSummary => EventObject; - - public bool Equals(ImportPackageEventArgs other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - // TODO: MetaData for package metadata has no equality operators :/ - return base.Equals(other) && PackageMetaData.Equals(other.PackageMetaData); - } - - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((ImportPackageEventArgs) obj); - } - - public override int GetHashCode() - { - unchecked - { - return (base.GetHashCode() * 397) ^ PackageMetaData.GetHashCode(); - } - } - - public static bool operator ==(ImportPackageEventArgs left, ImportPackageEventArgs right) - { - return Equals(left, right); - } - - public static bool operator !=(ImportPackageEventArgs left, ImportPackageEventArgs right) - { - return !Equals(left, right); - } - } -} diff --git a/src/Umbraco.Core/Events/UninstallPackageEventArgs.cs b/src/Umbraco.Core/Events/UninstallPackageEventArgs.cs deleted file mode 100644 index e83210b3a0..0000000000 --- a/src/Umbraco.Core/Events/UninstallPackageEventArgs.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Collections.Generic; -using Umbraco.Cms.Core.Packaging; - -namespace Umbraco.Cms.Core.Events -{ - public class UninstallPackageEventArgs: CancellableObjectEventArgs> - { - public UninstallPackageEventArgs(IEnumerable eventObject, bool canCancel) - : base(eventObject, canCancel) - { - } - - public IEnumerable UninstallationSummary => EventObject; - } -} From 657fc2d83b226031f7b416d06b01ba784215ded9 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Wed, 17 Mar 2021 10:17:56 +0000 Subject: [PATCH 4/7] Cant cancel an Uninstall so make it a normal notification --- src/Umbraco.Core/Events/UninstallPackageNotification.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Umbraco.Core/Events/UninstallPackageNotification.cs b/src/Umbraco.Core/Events/UninstallPackageNotification.cs index 9ad71c800c..2ce76fd5fc 100644 --- a/src/Umbraco.Core/Events/UninstallPackageNotification.cs +++ b/src/Umbraco.Core/Events/UninstallPackageNotification.cs @@ -3,13 +3,11 @@ using Umbraco.Cms.Core.Packaging; namespace Umbraco.Cms.Core.Events { - public class UninstallPackageNotification : ICancelableNotification + public class UninstallPackageNotification : INotification { public UninstallPackageNotification(IEnumerable uninstallationSummary) => UninstallationSummary = uninstallationSummary; public IEnumerable UninstallationSummary { get; } - - public bool Cancel { get; set; } } } From 8563643e80af1c69d3637757066c13e0c83f0f85 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Wed, 17 Mar 2021 10:18:56 +0000 Subject: [PATCH 5/7] Imported & Importing events. Only the Importing event can be cancelled & stop execution in package service --- ...tion.cs => ImportedPackageNotification.cs} | 6 ++---- .../Events/ImportingPackageNotification.cs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) rename src/Umbraco.Core/Events/{ImportPackageNotification.cs => ImportedPackageNotification.cs} (62%) create mode 100644 src/Umbraco.Core/Events/ImportingPackageNotification.cs diff --git a/src/Umbraco.Core/Events/ImportPackageNotification.cs b/src/Umbraco.Core/Events/ImportedPackageNotification.cs similarity index 62% rename from src/Umbraco.Core/Events/ImportPackageNotification.cs rename to src/Umbraco.Core/Events/ImportedPackageNotification.cs index 0430e10c90..7052a0f234 100644 --- a/src/Umbraco.Core/Events/ImportPackageNotification.cs +++ b/src/Umbraco.Core/Events/ImportedPackageNotification.cs @@ -3,10 +3,10 @@ using Umbraco.Cms.Core.Packaging; namespace Umbraco.Cms.Core.Events { - public class ImportPackageNotification : ICancelableNotification + public class ImportedPackageNotification : INotification { - public ImportPackageNotification(InstallationSummary installationSummary, IPackageInfo packageMetaData) + public ImportedPackageNotification(InstallationSummary installationSummary, IPackageInfo packageMetaData) { InstallationSummary = installationSummary; PackageMetaData = packageMetaData; @@ -15,7 +15,5 @@ namespace Umbraco.Cms.Core.Events public IPackageInfo PackageMetaData { get; } public InstallationSummary InstallationSummary { get; } - - public bool Cancel { get; set; } } } diff --git a/src/Umbraco.Core/Events/ImportingPackageNotification.cs b/src/Umbraco.Core/Events/ImportingPackageNotification.cs new file mode 100644 index 0000000000..836acfb9a4 --- /dev/null +++ b/src/Umbraco.Core/Events/ImportingPackageNotification.cs @@ -0,0 +1,19 @@ +using Umbraco.Cms.Core.Models.Packaging; + +namespace Umbraco.Cms.Core.Events +{ + public class ImportingPackageNotification : ICancelableNotification + { + public ImportingPackageNotification(string packageName, IPackageInfo packageMetaData) + { + PackageName = packageName; + PackageMetaData = packageMetaData; + } + + public string PackageName { get; } + + public IPackageInfo PackageMetaData { get; } + + public bool Cancel { get; set; } + } +} From 2901cb10f815ffb39c0e731354411abde1bfabbd Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Wed, 17 Mar 2021 12:57:24 +0000 Subject: [PATCH 6/7] Remove last static event and implement canceable notification where users are able to cancel the import process --- .../Services/Implement/PackagingService.cs | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs index 75bd0b8ced..bf86d965b1 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs @@ -120,7 +120,12 @@ namespace Umbraco.Cms.Core.Services.Implement var compiledPackage = GetCompiledPackageInfo(packageFile); if (compiledPackage == null) throw new InvalidOperationException("Could not read the package file " + packageFile); - if (ImportingPackage.IsRaisedEventCancelled(new ImportPackageEventArgs(packageFile.Name, compiledPackage), this)) + // Trigger the Importing Package Notification + var notification = new ImportingPackageNotification(packageFile.Name, compiledPackage); + _eventAggregator.Publish(notification); + + // Stop execution if event/user is cancelling it + if (notification.Cancel) return new InstallationSummary { MetaData = compiledPackage }; var summary = _packageInstallation.InstallPackageData(packageDefinition, compiledPackage, userId); @@ -129,8 +134,8 @@ namespace Umbraco.Cms.Core.Services.Implement _auditService.Add(AuditType.PackagerInstall, userId, -1, "Package", $"Package data installed for package '{compiledPackage.Name}'."); - // trigger the ImportPackage event - _eventAggregator.Publish(new ImportPackageNotification(summary, compiledPackage)); + // trigger the ImportedPackage event + _eventAggregator.Publish(new ImportedPackageNotification(summary, compiledPackage)); return summary; } @@ -240,22 +245,5 @@ namespace Umbraco.Cms.Core.Services.Implement } #endregion - - #region Event Handlers - - /// - /// Occurs before Importing umbraco package - /// - public static event TypedEventHandler> ImportingPackage; - - /// - /// Occurs after a package is imported - /// - public static event TypedEventHandler> ImportedPackage; - - - #endregion - - } } From c6d647149c47e5435b0443241c21751f71c6b37e Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 18 Mar 2021 09:43:00 +0100 Subject: [PATCH 7/7] Moved files to core to enable reuse + use them --- .../CancelableEnumerableObjectNotification.cs | 3 +-- .../Events}/CancelableObjectNotification.cs | 4 +--- .../Events}/CopiedNotification.cs | 4 +--- .../Events}/CopyingNotification.cs | 4 +--- .../Events}/DeletedNotification.cs | 3 +-- .../Events}/EnumerableObjectNotification.cs | 3 +-- src/Umbraco.Core/Events/EventAggregator.cs | 24 +++++++++++++++++++ src/Umbraco.Core/Events/IEventAggregator.cs | 18 ++++++++++++++ .../Events/ImportedPackageNotification.cs | 2 +- .../Events/ImportingPackageNotification.cs | 2 +- .../Events}/MovedNotification.cs | 3 +-- .../Events}/MovingNotification.cs | 3 +-- .../Events}/ObjectNotification.cs | 4 +--- .../Events}/RolledBackNotification.cs | 4 +--- .../Events}/RollingBackNotification.cs | 4 +--- .../Events}/SavedNotification.cs | 3 +-- .../Events}/SavingNotification.cs | 3 +-- .../Events}/SortedNotification.cs | 3 +-- .../Events}/SortingNotification.cs | 3 +-- .../Events}/StatefulNotification.cs | 3 +-- .../Events/UnattendedInstallNotification.cs | 5 +--- src/Umbraco.Infrastructure/RuntimeState.cs | 1 - .../Services/Implement/PackagingService.cs | 15 ++++++------ .../ContentNotificationExtensions.cs | 1 + .../UmbracoBuilderExtensions.cs | 2 -- ...CreateUnattendedUserNotificationHandler.cs | 1 - 26 files changed, 69 insertions(+), 56 deletions(-) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/CancelableEnumerableObjectNotification.cs (85%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/CancelableObjectNotification.cs (86%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/CopiedNotification.cs (86%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/CopyingNotification.cs (84%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/DeletedNotification.cs (80%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/EnumerableObjectNotification.cs (84%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/MovedNotification.cs (86%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/MovingNotification.cs (86%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/ObjectNotification.cs (81%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/RolledBackNotification.cs (77%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/RollingBackNotification.cs (78%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/SavedNotification.cs (84%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/SavingNotification.cs (84%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/SortedNotification.cs (80%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/SortingNotification.cs (81%) rename src/{Umbraco.Infrastructure/Services/Notifications => Umbraco.Core/Events}/StatefulNotification.cs (87%) diff --git a/src/Umbraco.Infrastructure/Services/Notifications/CancelableEnumerableObjectNotification.cs b/src/Umbraco.Core/Events/CancelableEnumerableObjectNotification.cs similarity index 85% rename from src/Umbraco.Infrastructure/Services/Notifications/CancelableEnumerableObjectNotification.cs rename to src/Umbraco.Core/Events/CancelableEnumerableObjectNotification.cs index 3875967530..1db0380af5 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/CancelableEnumerableObjectNotification.cs +++ b/src/Umbraco.Core/Events/CancelableEnumerableObjectNotification.cs @@ -2,9 +2,8 @@ // See LICENSE for more details. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class CancelableEnumerableObjectNotification : CancelableObjectNotification> { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/CancelableObjectNotification.cs b/src/Umbraco.Core/Events/CancelableObjectNotification.cs similarity index 86% rename from src/Umbraco.Infrastructure/Services/Notifications/CancelableObjectNotification.cs rename to src/Umbraco.Core/Events/CancelableObjectNotification.cs index 603ee9efbb..a976ff51d9 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/CancelableObjectNotification.cs +++ b/src/Umbraco.Core/Events/CancelableObjectNotification.cs @@ -1,9 +1,7 @@ // Copyright (c) Umbraco. // See LICENSE for more details. -using Umbraco.Cms.Core.Events; - -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class CancelableObjectNotification : ObjectNotification, ICancelableNotification where T : class { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/CopiedNotification.cs b/src/Umbraco.Core/Events/CopiedNotification.cs similarity index 86% rename from src/Umbraco.Infrastructure/Services/Notifications/CopiedNotification.cs rename to src/Umbraco.Core/Events/CopiedNotification.cs index 653f3913aa..807ffa1282 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/CopiedNotification.cs +++ b/src/Umbraco.Core/Events/CopiedNotification.cs @@ -1,9 +1,7 @@ // Copyright (c) Umbraco. // See LICENSE for more details. -using Umbraco.Cms.Core.Events; - -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class CopiedNotification : ObjectNotification where T : class { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/CopyingNotification.cs b/src/Umbraco.Core/Events/CopyingNotification.cs similarity index 84% rename from src/Umbraco.Infrastructure/Services/Notifications/CopyingNotification.cs rename to src/Umbraco.Core/Events/CopyingNotification.cs index ae658577b6..c89146b494 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/CopyingNotification.cs +++ b/src/Umbraco.Core/Events/CopyingNotification.cs @@ -1,9 +1,7 @@ // Copyright (c) Umbraco. // See LICENSE for more details. -using Umbraco.Cms.Core.Events; - -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class CopyingNotification : CancelableObjectNotification where T : class { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/DeletedNotification.cs b/src/Umbraco.Core/Events/DeletedNotification.cs similarity index 80% rename from src/Umbraco.Infrastructure/Services/Notifications/DeletedNotification.cs rename to src/Umbraco.Core/Events/DeletedNotification.cs index 99016ca073..6645059428 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/DeletedNotification.cs +++ b/src/Umbraco.Core/Events/DeletedNotification.cs @@ -2,9 +2,8 @@ // See LICENSE for more details. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class DeletedNotification : EnumerableObjectNotification { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/EnumerableObjectNotification.cs b/src/Umbraco.Core/Events/EnumerableObjectNotification.cs similarity index 84% rename from src/Umbraco.Infrastructure/Services/Notifications/EnumerableObjectNotification.cs rename to src/Umbraco.Core/Events/EnumerableObjectNotification.cs index 0201040ecf..a80b8bf7ee 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/EnumerableObjectNotification.cs +++ b/src/Umbraco.Core/Events/EnumerableObjectNotification.cs @@ -2,9 +2,8 @@ // See LICENSE for more details. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class EnumerableObjectNotification : ObjectNotification> { diff --git a/src/Umbraco.Core/Events/EventAggregator.cs b/src/Umbraco.Core/Events/EventAggregator.cs index edb97c1672..f4602039dd 100644 --- a/src/Umbraco.Core/Events/EventAggregator.cs +++ b/src/Umbraco.Core/Events/EventAggregator.cs @@ -55,6 +55,30 @@ namespace Umbraco.Cms.Core.Events PublishNotification(notification); Task.WaitAll(PublishNotificationAsync(notification)); } + + public bool PublishCancelable(TCancelableNotification notification) + where TCancelableNotification : ICancelableNotification + { + if (notification == null) + { + throw new ArgumentNullException(nameof(notification)); + } + + Publish(notification); + return notification.Cancel; + } + + public async Task PublishCancelableAsync(TCancelableNotification notification) + where TCancelableNotification : ICancelableNotification + { + if (notification == null) + { + throw new ArgumentNullException(nameof(notification)); + } + + await PublishAsync(notification); + return notification.Cancel; + } } /// diff --git a/src/Umbraco.Core/Events/IEventAggregator.cs b/src/Umbraco.Core/Events/IEventAggregator.cs index 82cc1a68ca..d59f75b040 100644 --- a/src/Umbraco.Core/Events/IEventAggregator.cs +++ b/src/Umbraco.Core/Events/IEventAggregator.cs @@ -29,5 +29,23 @@ namespace Umbraco.Cms.Core.Events /// The notification object. void Publish(TNotification notification) where TNotification : INotification; + + /// + /// Publishes a cancelable notification to the notification subscribers + /// + /// The type of notification being handled. + /// + /// True if the notification was cancelled by a subscriber, false otherwise + bool PublishCancelable(TCancelableNotification notification) + where TCancelableNotification : ICancelableNotification; + + /// + /// Publishes a cancelable notification async to the notification subscribers + /// + /// The type of notification being handled. + /// + /// True if the notification was cancelled by a subscriber, false otherwise + Task PublishCancelableAsync(TCancelableNotification notification) + where TCancelableNotification : ICancelableNotification; } } diff --git a/src/Umbraco.Core/Events/ImportedPackageNotification.cs b/src/Umbraco.Core/Events/ImportedPackageNotification.cs index 7052a0f234..f4f0bac1b4 100644 --- a/src/Umbraco.Core/Events/ImportedPackageNotification.cs +++ b/src/Umbraco.Core/Events/ImportedPackageNotification.cs @@ -3,7 +3,7 @@ using Umbraco.Cms.Core.Packaging; namespace Umbraco.Cms.Core.Events { - public class ImportedPackageNotification : INotification + public class ImportedPackageNotification : StatefulNotification { public ImportedPackageNotification(InstallationSummary installationSummary, IPackageInfo packageMetaData) diff --git a/src/Umbraco.Core/Events/ImportingPackageNotification.cs b/src/Umbraco.Core/Events/ImportingPackageNotification.cs index 836acfb9a4..143fad3ff4 100644 --- a/src/Umbraco.Core/Events/ImportingPackageNotification.cs +++ b/src/Umbraco.Core/Events/ImportingPackageNotification.cs @@ -2,7 +2,7 @@ using Umbraco.Cms.Core.Models.Packaging; namespace Umbraco.Cms.Core.Events { - public class ImportingPackageNotification : ICancelableNotification + public class ImportingPackageNotification : StatefulNotification, ICancelableNotification { public ImportingPackageNotification(string packageName, IPackageInfo packageMetaData) { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/MovedNotification.cs b/src/Umbraco.Core/Events/MovedNotification.cs similarity index 86% rename from src/Umbraco.Infrastructure/Services/Notifications/MovedNotification.cs rename to src/Umbraco.Core/Events/MovedNotification.cs index 35b587f73c..af96e70feb 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/MovedNotification.cs +++ b/src/Umbraco.Core/Events/MovedNotification.cs @@ -2,9 +2,8 @@ // See LICENSE for more details. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class MovedNotification : ObjectNotification>> { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/MovingNotification.cs b/src/Umbraco.Core/Events/MovingNotification.cs similarity index 86% rename from src/Umbraco.Infrastructure/Services/Notifications/MovingNotification.cs rename to src/Umbraco.Core/Events/MovingNotification.cs index 2781c0ba57..917cc283aa 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/MovingNotification.cs +++ b/src/Umbraco.Core/Events/MovingNotification.cs @@ -2,9 +2,8 @@ // See LICENSE for more details. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class MovingNotification : CancelableObjectNotification>> { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/ObjectNotification.cs b/src/Umbraco.Core/Events/ObjectNotification.cs similarity index 81% rename from src/Umbraco.Infrastructure/Services/Notifications/ObjectNotification.cs rename to src/Umbraco.Core/Events/ObjectNotification.cs index 16c57f210e..f1d957d42b 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/ObjectNotification.cs +++ b/src/Umbraco.Core/Events/ObjectNotification.cs @@ -1,9 +1,7 @@ // Copyright (c) Umbraco. // See LICENSE for more details. -using Umbraco.Cms.Core.Events; - -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class ObjectNotification : StatefulNotification where T : class { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/RolledBackNotification.cs b/src/Umbraco.Core/Events/RolledBackNotification.cs similarity index 77% rename from src/Umbraco.Infrastructure/Services/Notifications/RolledBackNotification.cs rename to src/Umbraco.Core/Events/RolledBackNotification.cs index 326b224478..596cb29520 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/RolledBackNotification.cs +++ b/src/Umbraco.Core/Events/RolledBackNotification.cs @@ -1,9 +1,7 @@ // Copyright (c) Umbraco. // See LICENSE for more details. -using Umbraco.Cms.Core.Events; - -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class RolledBackNotification : ObjectNotification where T : class { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/RollingBackNotification.cs b/src/Umbraco.Core/Events/RollingBackNotification.cs similarity index 78% rename from src/Umbraco.Infrastructure/Services/Notifications/RollingBackNotification.cs rename to src/Umbraco.Core/Events/RollingBackNotification.cs index 6afa46d6f1..265669bcdf 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/RollingBackNotification.cs +++ b/src/Umbraco.Core/Events/RollingBackNotification.cs @@ -1,9 +1,7 @@ // Copyright (c) Umbraco. // See LICENSE for more details. -using Umbraco.Cms.Core.Events; - -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class RollingBackNotification : CancelableObjectNotification where T : class { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/SavedNotification.cs b/src/Umbraco.Core/Events/SavedNotification.cs similarity index 84% rename from src/Umbraco.Infrastructure/Services/Notifications/SavedNotification.cs rename to src/Umbraco.Core/Events/SavedNotification.cs index b6a363019f..9ed7afd933 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/SavedNotification.cs +++ b/src/Umbraco.Core/Events/SavedNotification.cs @@ -2,9 +2,8 @@ // See LICENSE for more details. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class SavedNotification : EnumerableObjectNotification { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/SavingNotification.cs b/src/Umbraco.Core/Events/SavingNotification.cs similarity index 84% rename from src/Umbraco.Infrastructure/Services/Notifications/SavingNotification.cs rename to src/Umbraco.Core/Events/SavingNotification.cs index df84628b93..d58607be10 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/SavingNotification.cs +++ b/src/Umbraco.Core/Events/SavingNotification.cs @@ -2,9 +2,8 @@ // See LICENSE for more details. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class SavingNotification : CancelableEnumerableObjectNotification { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/SortedNotification.cs b/src/Umbraco.Core/Events/SortedNotification.cs similarity index 80% rename from src/Umbraco.Infrastructure/Services/Notifications/SortedNotification.cs rename to src/Umbraco.Core/Events/SortedNotification.cs index 88b0e11cf6..11c81f001b 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/SortedNotification.cs +++ b/src/Umbraco.Core/Events/SortedNotification.cs @@ -2,9 +2,8 @@ // See LICENSE for more details. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class SortedNotification : EnumerableObjectNotification { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/SortingNotification.cs b/src/Umbraco.Core/Events/SortingNotification.cs similarity index 81% rename from src/Umbraco.Infrastructure/Services/Notifications/SortingNotification.cs rename to src/Umbraco.Core/Events/SortingNotification.cs index 7d040a8b28..78a301dfb5 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/SortingNotification.cs +++ b/src/Umbraco.Core/Events/SortingNotification.cs @@ -2,9 +2,8 @@ // See LICENSE for more details. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class SortingNotification : CancelableEnumerableObjectNotification { diff --git a/src/Umbraco.Infrastructure/Services/Notifications/StatefulNotification.cs b/src/Umbraco.Core/Events/StatefulNotification.cs similarity index 87% rename from src/Umbraco.Infrastructure/Services/Notifications/StatefulNotification.cs rename to src/Umbraco.Core/Events/StatefulNotification.cs index 07831611fe..4d86262322 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/StatefulNotification.cs +++ b/src/Umbraco.Core/Events/StatefulNotification.cs @@ -1,9 +1,8 @@ // Copyright (c) Umbraco. using System.Collections.Generic; -using Umbraco.Cms.Core.Events; -namespace Umbraco.Cms.Infrastructure.Services.Notifications +namespace Umbraco.Cms.Core.Events { public abstract class StatefulNotification : IStatefulNotification { diff --git a/src/Umbraco.Core/Events/UnattendedInstallNotification.cs b/src/Umbraco.Core/Events/UnattendedInstallNotification.cs index 5bfb64e08f..20373ad15b 100644 --- a/src/Umbraco.Core/Events/UnattendedInstallNotification.cs +++ b/src/Umbraco.Core/Events/UnattendedInstallNotification.cs @@ -1,7 +1,4 @@ -using System; -using Umbraco.Cms.Core.Events; - -namespace Umbraco.Core.Events +namespace Umbraco.Cms.Core.Events { /// /// Used to notify that an Unattended install has completed diff --git a/src/Umbraco.Infrastructure/RuntimeState.cs b/src/Umbraco.Infrastructure/RuntimeState.cs index 02d4375186..76700e2001 100644 --- a/src/Umbraco.Infrastructure/RuntimeState.cs +++ b/src/Umbraco.Infrastructure/RuntimeState.cs @@ -11,7 +11,6 @@ using Umbraco.Cms.Core.Services; using Umbraco.Cms.Infrastructure.Migrations.Install; using Umbraco.Cms.Infrastructure.Migrations.Upgrade; using Umbraco.Cms.Infrastructure.Persistence; -using Umbraco.Core.Events; namespace Umbraco.Cms.Core { diff --git a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs index bf86d965b1..f3b199751e 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -120,13 +120,12 @@ namespace Umbraco.Cms.Core.Services.Implement var compiledPackage = GetCompiledPackageInfo(packageFile); if (compiledPackage == null) throw new InvalidOperationException("Could not read the package file " + packageFile); - // Trigger the Importing Package Notification - var notification = new ImportingPackageNotification(packageFile.Name, compiledPackage); - _eventAggregator.Publish(notification); - - // Stop execution if event/user is cancelling it - if (notification.Cancel) + // Trigger the Importing Package Notification and stop execution if event/user is cancelling it + var importingPackageNotification = new ImportingPackageNotification(packageFile.Name, compiledPackage); + if (_eventAggregator.PublishCancelable(importingPackageNotification)) + { return new InstallationSummary { MetaData = compiledPackage }; + } var summary = _packageInstallation.InstallPackageData(packageDefinition, compiledPackage, userId); @@ -135,7 +134,7 @@ namespace Umbraco.Cms.Core.Services.Implement _auditService.Add(AuditType.PackagerInstall, userId, -1, "Package", $"Package data installed for package '{compiledPackage.Name}'."); // trigger the ImportedPackage event - _eventAggregator.Publish(new ImportedPackageNotification(summary, compiledPackage)); + _eventAggregator.Publish(new ImportedPackageNotification(summary, compiledPackage).WithStateFrom(importingPackageNotification)); return summary; } diff --git a/src/Umbraco.Infrastructure/Services/Notifications/ContentNotificationExtensions.cs b/src/Umbraco.Infrastructure/Services/Notifications/ContentNotificationExtensions.cs index c04a04ef87..426a82240c 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/ContentNotificationExtensions.cs +++ b/src/Umbraco.Infrastructure/Services/Notifications/ContentNotificationExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) Umbraco. // See LICENSE for more details. +using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Models; namespace Umbraco.Cms.Infrastructure.Services.Notifications diff --git a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs index 9a52b0b72a..322fa42291 100644 --- a/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs +++ b/src/Umbraco.Web.Common/DependencyInjection/UmbracoBuilderExtensions.cs @@ -55,8 +55,6 @@ using Umbraco.Cms.Web.Common.Routing; using Umbraco.Cms.Web.Common.Security; using Umbraco.Cms.Web.Common.Templates; using Umbraco.Cms.Web.Common.UmbracoContext; -using Umbraco.Core.Events; -using static Umbraco.Cms.Core.Cache.HttpContextRequestAppCache; using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment; namespace Umbraco.Extensions diff --git a/src/Umbraco.Web.Common/Install/CreateUnattendedUserNotificationHandler.cs b/src/Umbraco.Web.Common/Install/CreateUnattendedUserNotificationHandler.cs index 7017298a8e..73b1fc3c36 100644 --- a/src/Umbraco.Web.Common/Install/CreateUnattendedUserNotificationHandler.cs +++ b/src/Umbraco.Web.Common/Install/CreateUnattendedUserNotificationHandler.cs @@ -9,7 +9,6 @@ using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Models.Membership; using Umbraco.Cms.Core.Security; using Umbraco.Cms.Core.Services; -using Umbraco.Core.Events; using Umbraco.Extensions; namespace Umbraco.Cms.Web.Common.Install