Skip to content

Commit

Permalink
DRY feature operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Piedone committed Apr 2, 2024
1 parent 861d8dc commit 3615945
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Lombiq.HelpfulLibraries.OrchardCore/Docs/Environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

## Extensions

- `FeatureInfoEnumerableExtensions`: Shortcuts for `IEnumerable<IFeatureInfo>`, like `Any(featureId)`.
- `OrchardCoreBuilderExtensions`: Shortcuts that can be used when initializing Orchard with `OrchardCoreBuilder`, e.g. `AddOrchardCms()`.
- `ShellFeaturesManagerExtensions`: Shortcuts for `IShellFeaturesManager`, like `IsFeatureEnabledAsync()`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using OrchardCore.Environment.Extensions.Features;
using System.Collections.Generic;
using System.Linq;

namespace OrchardCore.Environment.Shell;

/// <summary>
/// Shortcuts for <see cref="IEnumerable{IFeatureInfo}"/>.
/// </summary>
public static class FeatureInfoEnumerableExtensions
{
/// <summary>
/// Checks whether the given <see cref="IEnumerable{IFeatureInfo}"/> contains a feature with the given technical ID.
/// </summary>
/// <param name="featureId">Technical ID of the module feature.</param>
public static bool Any(this IEnumerable<IFeatureInfo> featureInfos, string featureId) =>
featureInfos.Any(feature => feature.Id == featureId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading.Tasks;

namespace OrchardCore.Environment.Shell;

/// <summary>
/// Shortcuts for <see cref="IShellFeaturesManager"/>.
/// </summary>
public static class ShellFeaturesManagerExtensions
{
/// <summary>
/// Checks whether the given module feature is enabled for the current shell.
/// </summary>
/// <param name="featureId">Technical ID of the module feature.</param>
public static async Task<bool> IsFeatureEnabledAsync(this IShellFeaturesManager shellFeaturesManager, string featureId) =>
(await shellFeaturesManager.GetEnabledFeaturesAsync()).Any(featureId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public async ValueTask UpdateAsync(IDictionary<string, string> securityPolicies,
var shellFeaturesManager = context.RequestServices.GetRequiredService<IShellFeaturesManager>();
var enabledFeatures = await shellFeaturesManager.GetEnabledFeaturesAsync();

if (enabledFeatures.Any(feature => feature.Id == "OrchardCore.Microsoft.Authentication.AzureAD"))
if (enabledFeatures.Any("OrchardCore.Microsoft.Authentication.AzureAD"))
{
CspHelper.MergeValues(securityPolicies, FormAction, "login.microsoftonline.com"); // #spell-check-ignore-line
}

if (enabledFeatures.Any(feature => feature.Id == "OrchardCore.GitHub.Authentication"))
if (enabledFeatures.Any("OrchardCore.GitHub.Authentication"))
{
CspHelper.MergeValues(securityPolicies, FormAction, "github.com");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Environment.Shell;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using static Lombiq.HelpfulLibraries.AspNetCore.Security.ContentSecurityPolicyDirectives;
Expand All @@ -21,8 +20,7 @@ public async ValueTask UpdateAsync(IDictionary<string, string> securityPolicies,
if (!googleAnalyticsIsEnabled)
{
var shellFeaturesManager = context.RequestServices.GetRequiredService<IShellFeaturesManager>();
googleAnalyticsIsEnabled = (await shellFeaturesManager.GetEnabledFeaturesAsync())
.Any(feature => feature.Id == "OrchardCore.Google.Analytics");
googleAnalyticsIsEnabled = await shellFeaturesManager.IsFeatureEnabledAsync("OrchardCore.Google.Analytics");
}

if (googleAnalyticsIsEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Environment.Shell;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using static Lombiq.HelpfulLibraries.AspNetCore.Security.ContentSecurityPolicyDirectives;
Expand All @@ -15,10 +14,8 @@ internal sealed class ReCaptchaContentSecurityPolicyProvider : IContentSecurityP
public async ValueTask UpdateAsync(IDictionary<string, string> securityPolicies, HttpContext context)
{
var shellFeaturesManager = context.RequestServices.GetRequiredService<IShellFeaturesManager>();
var reCaptchaIsEnabled = (await shellFeaturesManager.GetEnabledFeaturesAsync())
.Any(feature => feature.Id == "OrchardCore.ReCaptcha");

if (reCaptchaIsEnabled)
if (await shellFeaturesManager.IsFeatureEnabledAsync("OrchardCore.ReCaptcha"))
{
CspHelper.MergeValues(securityPolicies, ScriptSrc, "www.google.com", "www.gstatic.com");
CspHelper.MergeValues(securityPolicies, FrameSrc, "www.google.com");
Expand Down

0 comments on commit 3615945

Please sign in to comment.