From 852d0e7f96cfe4069f3fe0d89c49385c8e400587 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Wed, 29 May 2024 08:20:33 -0700 Subject: [PATCH 1/2] Add helpful ShellFeaturesManager extenssins --- .../Migrations/EmailMigrations.cs | 23 ++++-------------- .../ShellFeaturesManagerExtensions.cs | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Email/Migrations/EmailMigrations.cs b/src/OrchardCore.Modules/OrchardCore.Email/Migrations/EmailMigrations.cs index 131fc687e3a..6c814c84bf9 100644 --- a/src/OrchardCore.Modules/OrchardCore.Email/Migrations/EmailMigrations.cs +++ b/src/OrchardCore.Modules/OrchardCore.Email/Migrations/EmailMigrations.cs @@ -1,4 +1,3 @@ -using System.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using OrchardCore.Data.Migration; @@ -21,35 +20,23 @@ public int Create() // Email service and have SmtpSettings. ShellScope.AddDeferredTask(async scope => { - var siteService = scope.ServiceProvider.GetRequiredService(); - var featuresManager = scope.ServiceProvider.GetRequiredService(); - var enabledFeatures = await featuresManager.GetEnabledFeaturesAsync(); - - if (enabledFeatures.Any(feature => feature.Id == SmtpFeatureId)) + if (await featuresManager.IsFeatureEnabledAsync(SmtpFeatureId)) { return; } + var siteService = scope.ServiceProvider.GetRequiredService(); var site = await siteService.GetSiteSettingsAsync(); var smtpSettings = site.As(); - if (!string.IsNullOrEmpty(smtpSettings.DefaultSender) - || scope.ServiceProvider.GetService>()?.Value.ConfigurationExists() == true) + if (!string.IsNullOrEmpty(smtpSettings.DefaultSender) || + scope.ServiceProvider.GetService>()?.Value.ConfigurationExists() == true) { // Enable the SMTP feature. - var allFeatures = await featuresManager.GetAvailableFeaturesAsync(); - - var smtpFeature = allFeatures.FirstOrDefault(feature => feature.Id == SmtpFeatureId); - - if (smtpFeature is null) - { - return; - } - - await featuresManager.EnableFeaturesAsync([smtpFeature]); + await featuresManager.EnableFeaturesAsync(SmtpFeatureId); } }); diff --git a/src/OrchardCore/OrchardCore.Abstractions/Shell/Extensions/ShellFeaturesManagerExtensions.cs b/src/OrchardCore/OrchardCore.Abstractions/Shell/Extensions/ShellFeaturesManagerExtensions.cs index da814b112bd..ba2e3ba12bb 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Shell/Extensions/ShellFeaturesManagerExtensions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Shell/Extensions/ShellFeaturesManagerExtensions.cs @@ -1,4 +1,6 @@ +using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using OrchardCore.Environment.Extensions.Features; @@ -16,9 +18,21 @@ public static async Task> EnableFeaturesAsync(this ISh IEnumerable features, bool force) { var (_, featuresToEnable) = await shellFeaturesManager.UpdateFeaturesAsync([], features, force); + return featuresToEnable; } + public static async Task EnableFeaturesAsync(this IShellFeaturesManager shellFeaturesManager, params string[] featureIds) + { + ArgumentNullException.ThrowIfNull(featureIds); + + var availableFeatures = await shellFeaturesManager.GetAvailableFeaturesAsync(); + + var featuresToEnable = availableFeatures.Where(feature => featureIds.Contains(feature.Id)); + + await shellFeaturesManager.EnableFeaturesAsync(featuresToEnable, force: false); + } + public static Task> DisableFeaturesAsync(this IShellFeaturesManager shellFeaturesManager, IEnumerable features) { @@ -29,7 +43,17 @@ public static async Task> DisableFeaturesAsync(this IS IEnumerable features, bool force) { var (featuresToDisable, _) = await shellFeaturesManager.UpdateFeaturesAsync(features, [], force); + return featuresToDisable; } + + public static async Task IsFeatureEnabledAsync(this IShellFeaturesManager shellFeaturesManager, string featureId) + { + ArgumentException.ThrowIfNullOrEmpty(featureId); + + var enabledFeatures = await shellFeaturesManager.GetEnabledFeaturesAsync(); + + return enabledFeatures.Any(feature => feature.Id == featureId); + } } } From 085460e78e3114fbab18aa95dd62dce0d36eb69b Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Wed, 29 May 2024 16:07:22 -0700 Subject: [PATCH 2/2] adding length check --- .../Shell/Extensions/ShellFeaturesManagerExtensions.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/OrchardCore/OrchardCore.Abstractions/Shell/Extensions/ShellFeaturesManagerExtensions.cs b/src/OrchardCore/OrchardCore.Abstractions/Shell/Extensions/ShellFeaturesManagerExtensions.cs index ba2e3ba12bb..b2274655ad7 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Shell/Extensions/ShellFeaturesManagerExtensions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Shell/Extensions/ShellFeaturesManagerExtensions.cs @@ -26,6 +26,11 @@ public static async Task EnableFeaturesAsync(this IShellFeaturesManager shellFea { ArgumentNullException.ThrowIfNull(featureIds); + if (featureIds.Length == 0) + { + return; + } + var availableFeatures = await shellFeaturesManager.GetAvailableFeaturesAsync(); var featuresToEnable = availableFeatures.Where(feature => featureIds.Contains(feature.Id));