From eddcd9bcc97ad00f10b9f0bc8306a58e24e8c35d Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Mon, 12 Dec 2022 18:16:10 +0100 Subject: [PATCH 1/4] Adding ConfigureFeaturesGuardForElasticsearch() function --- .../Extensions/OrchardCoreBuilderExtensions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs b/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs index f56832cf..fc6038ea 100644 --- a/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs +++ b/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs @@ -21,6 +21,23 @@ public static OrchardCoreBuilder ConfigureFeaturesGuardForAzureStorage(this Orch return builder; } + /// + /// Binds Elasticsearch as the conditional feature and Search or Indexing as the condition feature to + /// . + /// + public static OrchardCoreBuilder ConfigureFeaturesGuardForElasticsearch(this OrchardCoreBuilder builder) + { + builder.ConfigureServices((tenantServices, _) => + tenantServices.PostConfigure(options => + options.EnableFeatureIfOtherFeatureIsEnabled = new Dictionary> + { + ["OrchardCore.Search.Elasticsearch"] = + new List { "OrchardCore.Search", "OrchardCore.Indexing" }, + })); + + return builder; + } + /// /// Binds the provided dictionary's keys and values as the conditional and condition features to /// . From 13d5325c9538fe50aa9ba2c94c2570ee03dcd793 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Thu, 15 Dec 2022 16:25:01 +0100 Subject: [PATCH 2/4] Modifying extension methods, so they won't override existing configurations --- .../OrchardCoreBuilderExtensions.cs | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs b/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs index fc6038ea..1a5e5fee 100644 --- a/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs +++ b/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs @@ -1,4 +1,5 @@ using Lombiq.Hosting.Tenants.FeaturesGuard.Models; +using Microsoft.CodeAnalysis.CSharp.Syntax; using System.Collections.Generic; namespace Microsoft.Extensions.DependencyInjection; @@ -13,10 +14,11 @@ public static OrchardCoreBuilder ConfigureFeaturesGuardForAzureStorage(this Orch { builder.ConfigureServices((tenantServices, _) => tenantServices.PostConfigure(options => - options.EnableFeatureIfOtherFeatureIsEnabled = new Dictionary> - { - ["OrchardCore.Media.Azure.Storage"] = new List { "OrchardCore.Media" }, - })); + options.AddDictionaryToEnableFeatureIfOtherFeatureIsEnabled( + new Dictionary> + { + ["OrchardCore.Media.Azure.Storage"] = new List { "OrchardCore.Media" }, + }))); return builder; } @@ -29,11 +31,12 @@ public static OrchardCoreBuilder ConfigureFeaturesGuardForElasticsearch(this Orc { builder.ConfigureServices((tenantServices, _) => tenantServices.PostConfigure(options => - options.EnableFeatureIfOtherFeatureIsEnabled = new Dictionary> - { - ["OrchardCore.Search.Elasticsearch"] = - new List { "OrchardCore.Search", "OrchardCore.Indexing" }, - })); + options.AddDictionaryToEnableFeatureIfOtherFeatureIsEnabled( + new Dictionary> + { + ["OrchardCore.Search.Elasticsearch"] = + new List { "OrchardCore.Search", "OrchardCore.Indexing" }, + }))); return builder; } @@ -51,4 +54,21 @@ public static OrchardCoreBuilder ConfigureFeaturesGuard( return builder; } + + private static ConditionallyEnabledFeaturesOptions AddDictionaryToEnableFeatureIfOtherFeatureIsEnabled( + this ConditionallyEnabledFeaturesOptions options, + Dictionary> dictionary) + { + if (options.EnableFeatureIfOtherFeatureIsEnabled == null) + { + options.EnableFeatureIfOtherFeatureIsEnabled = dictionary; + } + else + { + options.EnableFeatureIfOtherFeatureIsEnabled + .AddRange(dictionary); + } + + return options; + } } From 8bac9db5e108f5f5a0b019ffa294af5be59a7c54 Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Thu, 15 Dec 2022 16:35:27 +0100 Subject: [PATCH 3/4] More DRY --- .../OrchardCoreBuilderExtensions.cs | 69 ++++++++----------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs b/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs index 1a5e5fee..ffd779b4 100644 --- a/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs +++ b/Lombiq.Hosting.Tenants.FeaturesGuard/Extensions/OrchardCoreBuilderExtensions.cs @@ -1,5 +1,4 @@ using Lombiq.Hosting.Tenants.FeaturesGuard.Models; -using Microsoft.CodeAnalysis.CSharp.Syntax; using System.Collections.Generic; namespace Microsoft.Extensions.DependencyInjection; @@ -10,36 +9,24 @@ public static class OrchardCoreBuilderExtensions /// Binds Azure Storage as the conditional feature and Media as the condition feature to /// . /// - public static OrchardCoreBuilder ConfigureFeaturesGuardForAzureStorage(this OrchardCoreBuilder builder) - { - builder.ConfigureServices((tenantServices, _) => - tenantServices.PostConfigure(options => - options.AddDictionaryToEnableFeatureIfOtherFeatureIsEnabled( - new Dictionary> - { - ["OrchardCore.Media.Azure.Storage"] = new List { "OrchardCore.Media" }, - }))); - - return builder; - } + public static OrchardCoreBuilder ConfigureFeaturesGuardForAzureStorage(this OrchardCoreBuilder builder) => + builder.ConfigureFeaturesGuardWithoutOverriding( + new Dictionary> + { + ["OrchardCore.Media.Azure.Storage"] = new List { "OrchardCore.Media" }, + }); /// /// Binds Elasticsearch as the conditional feature and Search or Indexing as the condition feature to /// . /// - public static OrchardCoreBuilder ConfigureFeaturesGuardForElasticsearch(this OrchardCoreBuilder builder) - { - builder.ConfigureServices((tenantServices, _) => - tenantServices.PostConfigure(options => - options.AddDictionaryToEnableFeatureIfOtherFeatureIsEnabled( - new Dictionary> - { - ["OrchardCore.Search.Elasticsearch"] = - new List { "OrchardCore.Search", "OrchardCore.Indexing" }, - }))); - - return builder; - } + public static OrchardCoreBuilder ConfigureFeaturesGuardForElasticsearch(this OrchardCoreBuilder builder) => + builder.ConfigureFeaturesGuardWithoutOverriding( + new Dictionary> + { + ["OrchardCore.Search.Elasticsearch"] = + new List { "OrchardCore.Search", "OrchardCore.Indexing" }, + }); /// /// Binds the provided dictionary's keys and values as the conditional and condition features to @@ -55,20 +42,24 @@ public static OrchardCoreBuilder ConfigureFeaturesGuard( return builder; } - private static ConditionallyEnabledFeaturesOptions AddDictionaryToEnableFeatureIfOtherFeatureIsEnabled( - this ConditionallyEnabledFeaturesOptions options, - Dictionary> dictionary) + public static OrchardCoreBuilder ConfigureFeaturesGuardWithoutOverriding( + this OrchardCoreBuilder builder, + IDictionary> configDictionary) { - if (options.EnableFeatureIfOtherFeatureIsEnabled == null) - { - options.EnableFeatureIfOtherFeatureIsEnabled = dictionary; - } - else - { - options.EnableFeatureIfOtherFeatureIsEnabled - .AddRange(dictionary); - } + builder.ConfigureServices((tenantServices, _) => + tenantServices.PostConfigure(options => + { + if (options.EnableFeatureIfOtherFeatureIsEnabled == null) + { + options.EnableFeatureIfOtherFeatureIsEnabled = configDictionary; + } + else + { + options.EnableFeatureIfOtherFeatureIsEnabled + .AddRange(configDictionary); + } + })); - return options; + return builder; } } From ca2ab616b491607458afb6696ddb4e8b4a8ee48c Mon Sep 17 00:00:00 2001 From: Szabolcs Deme Date: Thu, 15 Dec 2022 17:05:20 +0100 Subject: [PATCH 4/4] Updating Readme.md --- Lombiq.Hosting.Tenants.FeaturesGuard/Readme.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lombiq.Hosting.Tenants.FeaturesGuard/Readme.md b/Lombiq.Hosting.Tenants.FeaturesGuard/Readme.md index 2f509cac..643c7f1e 100644 --- a/Lombiq.Hosting.Tenants.FeaturesGuard/Readme.md +++ b/Lombiq.Hosting.Tenants.FeaturesGuard/Readme.md @@ -11,7 +11,7 @@ A module that makes it possible to conditionally enable and conditionally keep e ### Conditionally enabling features - To use this feature, enable it on both the Default and the user tenant. -- Features that should be conditionally enabled, as well as the features whose status acts as the condition, can be specified either in _appsettings.json_ using `ConditionallyEnabledFeaturesOptions` or in `Program` via the `ConfigureFeaturesGuard()` extension method. +- Features that should be conditionally enabled, as well as the features whose status acts as the condition, can be specified either in _appsettings.json_ using `ConditionallyEnabledFeaturesOptions` or in `Program` via the `ConfigureFeaturesGuard()` or `ConfigureFeaturesGuardWithoutOverriding()` extension method. Conditionally enabled features need to be provided with a singular or multiple condition features, where the status of the condition features determines whether the corresponding conditional feature is enabled or disabled. Example configuration: `Program` config example: @@ -27,7 +27,8 @@ builder.Services })); ``` -- `ConfigureFeaturesGuardForAzureStorage()` is also available to add `Azure Storage` as a conditional feature and `Media` as its condition feature. +- `ConfigureFeaturesGuardForAzureStorage()` is available to add `Azure Storage` as a conditional feature and `Media` as its condition feature. +- `ConfigureFeaturesGuardForElasticsearch()` is available to add `Elasticsearch` as a conditional feature and `Indexing` or `Search` as its condition feature. _appsettings.json_ config example: ```json