Skip to content

Commit

Permalink
Add helpful ShellFeaturesManager extensions (#16191)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored May 30, 2024
1 parent 08e5f6e commit 265e586
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore.Data.Migration;
Expand All @@ -21,35 +20,23 @@ public int Create()
// Email service and have SmtpSettings.
ShellScope.AddDeferredTask(async scope =>
{
var siteService = scope.ServiceProvider.GetRequiredService<ISiteService>();
var featuresManager = scope.ServiceProvider.GetRequiredService<IShellFeaturesManager>();
var enabledFeatures = await featuresManager.GetEnabledFeaturesAsync();
if (enabledFeatures.Any(feature => feature.Id == SmtpFeatureId))
if (await featuresManager.IsFeatureEnabledAsync(SmtpFeatureId))
{
return;
}
var siteService = scope.ServiceProvider.GetRequiredService<ISiteService>();
var site = await siteService.GetSiteSettingsAsync();
var smtpSettings = site.As<SmtpSettings>();
if (!string.IsNullOrEmpty(smtpSettings.DefaultSender)
|| scope.ServiceProvider.GetService<IOptions<SmtpOptions>>()?.Value.ConfigurationExists() == true)
if (!string.IsNullOrEmpty(smtpSettings.DefaultSender) ||
scope.ServiceProvider.GetService<IOptions<SmtpOptions>>()?.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);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OrchardCore.Environment.Extensions.Features;

Expand All @@ -16,9 +18,26 @@ public static async Task<IEnumerable<IFeatureInfo>> EnableFeaturesAsync(this ISh
IEnumerable<IFeatureInfo> 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);

if (featureIds.Length == 0)
{
return;
}

var availableFeatures = await shellFeaturesManager.GetAvailableFeaturesAsync();

var featuresToEnable = availableFeatures.Where(feature => featureIds.Contains(feature.Id));

await shellFeaturesManager.EnableFeaturesAsync(featuresToEnable, force: false);
}

public static Task<IEnumerable<IFeatureInfo>> DisableFeaturesAsync(this IShellFeaturesManager shellFeaturesManager,
IEnumerable<IFeatureInfo> features)
{
Expand All @@ -29,7 +48,17 @@ public static async Task<IEnumerable<IFeatureInfo>> DisableFeaturesAsync(this IS
IEnumerable<IFeatureInfo> features, bool force)
{
var (featuresToDisable, _) = await shellFeaturesManager.UpdateFeaturesAsync(features, [], force);

return featuresToDisable;
}

public static async Task<bool> IsFeatureEnabledAsync(this IShellFeaturesManager shellFeaturesManager, string featureId)
{
ArgumentException.ThrowIfNullOrEmpty(featureId);

var enabledFeatures = await shellFeaturesManager.GetEnabledFeaturesAsync();

return enabledFeatures.Any(feature => feature.Id == featureId);
}
}
}

0 comments on commit 265e586

Please sign in to comment.