From db0e5adeaf3b0f772193e16b017b932eb3f4f460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20M=C3=A1t=C3=A9?= Date: Mon, 17 Jul 2023 19:39:19 +0200 Subject: [PATCH 1/6] Extending options with custom name-url config --- .../Helpers/TenantUrlHelpers.cs | 24 +++++++++++++++++++ .../UpdateSiteUrlMaintenanceOptions.cs | 3 +++ .../UpdateSiteUrlMaintenanceProvider.cs | 1 + Lombiq.Hosting.Tenants.Maintenance/Readme.md | 23 +++++++++++++++++- 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs index 9da8aaf5..06ba6b78 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs @@ -1,4 +1,5 @@ using OrchardCore.Environment.Shell; +using System.Collections.Generic; namespace Lombiq.Hosting.Tenants.Maintenance.Helpers; @@ -10,6 +11,29 @@ public static string ReplaceTenantName(string url, string tenantName) => url?.Replace("{TenantName}", tenantName.ToLowerInvariant()); #pragma warning restore CA1308 // Normalize strings to uppercase + public static string GetEvaluatedValueForTenant( + string valueForDefaultTenant, + string valueForAnyTenant, + IDictionary valueForTenantByName, + ShellSettings shellSettings) + { + string evaluatedValue = string.Empty; + + if (!string.IsNullOrEmpty(valueForAnyTenant)) + { + evaluatedValue = ReplaceTenantName(valueForAnyTenant, shellSettings.Name); + } + else if (valueForTenantByName.Count > 0) + { + foreach (var pair in valueForTenantByName) + { + if (pair.Key == shellSettings.Name) evaluatedValue = pair.Value; + } + } + + return shellSettings.IsDefaultShell() ? valueForDefaultTenant : evaluatedValue; + } + public static string GetEvaluatedValueForTenant( string valueForDefaultTenant, string valueForAnyTenant, diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceOptions.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceOptions.cs index 21104e88..b9e0a393 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceOptions.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceOptions.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; + namespace Lombiq.Hosting.Tenants.Maintenance.Maintenance.UpdateSiteUrl; public class UpdateSiteUrlMaintenanceOptions @@ -5,4 +7,5 @@ public class UpdateSiteUrlMaintenanceOptions public bool IsEnabled { get; set; } public string DefaultTenantSiteUrl { get; set; } public string SiteUrl { get; set; } + public IDictionary SiteUrlFromTenantName { get; init; } } diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceProvider.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceProvider.cs index 68931684..7855d346 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceProvider.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceProvider.cs @@ -34,6 +34,7 @@ public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) siteSettings.BaseUrl = TenantUrlHelpers.GetEvaluatedValueForTenant( _options.Value.DefaultTenantSiteUrl, _options.Value.SiteUrl, + _options.Value.SiteUrlFromTenantName, _shellSettings); await _siteService.UpdateSiteSettingsAsync(siteSettings); diff --git a/Lombiq.Hosting.Tenants.Maintenance/Readme.md b/Lombiq.Hosting.Tenants.Maintenance/Readme.md index 983dc56d..35f6b3de 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Readme.md +++ b/Lombiq.Hosting.Tenants.Maintenance/Readme.md @@ -62,7 +62,8 @@ The following configuration options are available to set the site URL: "UpdateSiteUrl": { "IsEnabled": true, "SiteUrl": "https://domain.com/{TenantName}", - "DefaultTenantSiteUrl": "https://domain.com" + "DefaultTenantSiteUrl": "https://domain.com", + "SiteUrlFromTenantName": "" } } } @@ -71,6 +72,26 @@ The following configuration options are available to set the site URL: **NOTE**: The `{TenantName}` placeholder will be replaced with the actual tenant name automatically. +Custom URL generation is also an option, in this case, you need to leave the `SiteUrl` property empty and add your tenants' name and URL: + +```json +{ + "OrchardCore": { + "Lombiq_Hosting_Tenants_Maintenance": { + "UpdateSiteUrl": { + "IsEnabled": true, + "SiteUrl": "", + "DefaultTenantSiteUrl": "https://domain.com", + "SiteUrlFromTenantName": { + "Tenant1": "https://domain.com/custom-url", + "Tenant2": "https://custom-domain.com" + } + } + } + } +} +``` + ### `Lombiq.Hosting.Tenants.Maintenance.UpdateShellRequestUrls` It's a maintenance task that updates the shell's request URLs in each tenant's shell settings based on the app configuration. It is available only for the default tenant. From 2fa9aa6aeaf8daf121442ef13bc0a9d5f0f89157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20M=C3=A1t=C3=A9?= Date: Mon, 17 Jul 2023 21:13:54 +0200 Subject: [PATCH 2/6] DRYing and null check --- .../Helpers/TenantUrlHelpers.cs | 18 +++--------------- .../UpdateSiteUrlMaintenanceProvider.cs | 4 ++-- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs index 06ba6b78..2d02894c 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs @@ -14,8 +14,8 @@ public static string ReplaceTenantName(string url, string tenantName) => public static string GetEvaluatedValueForTenant( string valueForDefaultTenant, string valueForAnyTenant, - IDictionary valueForTenantByName, - ShellSettings shellSettings) + ShellSettings shellSettings, + IDictionary valueForTenantByName = null) { string evaluatedValue = string.Empty; @@ -23,7 +23,7 @@ public static string GetEvaluatedValueForTenant( { evaluatedValue = ReplaceTenantName(valueForAnyTenant, shellSettings.Name); } - else if (valueForTenantByName.Count > 0) + else if (valueForTenantByName?.Count > 0) { foreach (var pair in valueForTenantByName) { @@ -33,16 +33,4 @@ public static string GetEvaluatedValueForTenant( return shellSettings.IsDefaultShell() ? valueForDefaultTenant : evaluatedValue; } - - public static string GetEvaluatedValueForTenant( - string valueForDefaultTenant, - string valueForAnyTenant, - ShellSettings shellSettings) - { - var evaluatedValue = !string.IsNullOrEmpty(valueForAnyTenant) - ? ReplaceTenantName(valueForAnyTenant, shellSettings.Name) - : string.Empty; - - return shellSettings.IsDefaultShell() ? valueForDefaultTenant : evaluatedValue; - } } diff --git a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceProvider.cs b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceProvider.cs index 7855d346..f27eca5c 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceProvider.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Maintenance/UpdateSiteUrl/UpdateSiteUrlMaintenanceProvider.cs @@ -34,8 +34,8 @@ public override async Task ExecuteAsync(MaintenanceTaskExecutionContext context) siteSettings.BaseUrl = TenantUrlHelpers.GetEvaluatedValueForTenant( _options.Value.DefaultTenantSiteUrl, _options.Value.SiteUrl, - _options.Value.SiteUrlFromTenantName, - _shellSettings); + _shellSettings, + _options.Value.SiteUrlFromTenantName); await _siteService.UpdateSiteSettingsAsync(siteSettings); } From 7fb43bf017c5f599370d3fc6fc6931f9754a6d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20M=C3=A1t=C3=A9?= Date: Mon, 17 Jul 2023 21:18:30 +0200 Subject: [PATCH 3/6] Better explanation in Readme --- Lombiq.Hosting.Tenants.Maintenance/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Readme.md b/Lombiq.Hosting.Tenants.Maintenance/Readme.md index 35f6b3de..88383bd7 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Readme.md +++ b/Lombiq.Hosting.Tenants.Maintenance/Readme.md @@ -72,7 +72,7 @@ The following configuration options are available to set the site URL: **NOTE**: The `{TenantName}` placeholder will be replaced with the actual tenant name automatically. -Custom URL generation is also an option, in this case, you need to leave the `SiteUrl` property empty and add your tenants' name and URL: +Defining each tenant's URL separately is also an option, in this case, you need to leave the `SiteUrl` property empty and add your tenants' name and URL in `SiteUrlFromTenantName`: ```json { From eecd67427aab297c54f5d85996d020e032c5bd85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20M=C3=A1t=C3=A9?= <99020631+MZole@users.noreply.github.com> Date: Tue, 18 Jul 2023 17:04:25 +0200 Subject: [PATCH 4/6] Update Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs Co-authored-by: Szabolcs Deme <80963259+DemeSzabolcs@users.noreply.github.com> --- Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs index 2d02894c..4198d6d0 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs @@ -17,7 +17,7 @@ public static string GetEvaluatedValueForTenant( ShellSettings shellSettings, IDictionary valueForTenantByName = null) { - string evaluatedValue = string.Empty; + var evaluatedValue = string.Empty; if (!string.IsNullOrEmpty(valueForAnyTenant)) { From 6b7363098fd6c1fb7db4685f72b5e11e2930b08b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20M=C3=A1t=C3=A9?= <99020631+MZole@users.noreply.github.com> Date: Tue, 18 Jul 2023 17:05:03 +0200 Subject: [PATCH 5/6] Update Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs Co-authored-by: Szabolcs Deme <80963259+DemeSzabolcs@users.noreply.github.com> --- Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs index 4198d6d0..b4e7e32f 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs @@ -23,7 +23,7 @@ public static string GetEvaluatedValueForTenant( { evaluatedValue = ReplaceTenantName(valueForAnyTenant, shellSettings.Name); } - else if (valueForTenantByName?.Count > 0) + else if (valueForTenantByName?.Any() == true) { foreach (var pair in valueForTenantByName) { From e7cde37aced4d6ffe5a58fc07472253427aba77d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20M=C3=A1t=C3=A9?= Date: Tue, 18 Jul 2023 17:21:44 +0200 Subject: [PATCH 6/6] Addressing change requests --- .../Helpers/TenantUrlHelpers.cs | 1 + Lombiq.Hosting.Tenants.Maintenance/Readme.md | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs index b4e7e32f..4ab28c92 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs +++ b/Lombiq.Hosting.Tenants.Maintenance/Helpers/TenantUrlHelpers.cs @@ -1,5 +1,6 @@ using OrchardCore.Environment.Shell; using System.Collections.Generic; +using System.Linq; namespace Lombiq.Hosting.Tenants.Maintenance.Helpers; diff --git a/Lombiq.Hosting.Tenants.Maintenance/Readme.md b/Lombiq.Hosting.Tenants.Maintenance/Readme.md index 88383bd7..b05710b8 100644 --- a/Lombiq.Hosting.Tenants.Maintenance/Readme.md +++ b/Lombiq.Hosting.Tenants.Maintenance/Readme.md @@ -62,8 +62,7 @@ The following configuration options are available to set the site URL: "UpdateSiteUrl": { "IsEnabled": true, "SiteUrl": "https://domain.com/{TenantName}", - "DefaultTenantSiteUrl": "https://domain.com", - "SiteUrlFromTenantName": "" + "DefaultTenantSiteUrl": "https://domain.com" } } } @@ -72,7 +71,7 @@ The following configuration options are available to set the site URL: **NOTE**: The `{TenantName}` placeholder will be replaced with the actual tenant name automatically. -Defining each tenant's URL separately is also an option, in this case, you need to leave the `SiteUrl` property empty and add your tenants' name and URL in `SiteUrlFromTenantName`: +Defining each tenant's URL separately is also an option, in this case, you have to use the `SiteUrlFromTenantName` property instead of `SiteUrl` and add your tenants' name and URL: ```json { @@ -80,7 +79,6 @@ Defining each tenant's URL separately is also an option, in this case, you need "Lombiq_Hosting_Tenants_Maintenance": { "UpdateSiteUrl": { "IsEnabled": true, - "SiteUrl": "", "DefaultTenantSiteUrl": "https://domain.com", "SiteUrlFromTenantName": { "Tenant1": "https://domain.com/custom-url",