Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpful ShellFeaturesManager extensions #16191

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check if the array is empty too

Copy link
Member Author

@MikeAlhayek MikeAlhayek May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would do nothing when array is empty as this is handled by the underlying method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO know need to call GetAvailableFeaturesAsync() if the featureIds is empty

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hishamco done. anything else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second time you can request a review rather than mention


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);
}
}
}