From 91c6ee0b5abfd2969750472f9b28b9414b28cc18 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 26 Jul 2024 15:43:25 +0800 Subject: [PATCH] Add `WebAssemblyMultiTenantUrlProvider`. Resolve #20292 --- .../WebAssemblyMultiTenantUrlOptions.cs | 6 +++ .../WebAssemblyMultiTenantUrlProvider.cs | 49 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlOptions.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlProvider.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlOptions.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlOptions.cs new file mode 100644 index 00000000000..b957e11dfba --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlOptions.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.AspNetCore.Components.WebAssembly.MultiTenant; + +public class WebAssemblyMultiTenantUrlOptions +{ + public string DomainFormat { get; set; } = default!; +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlProvider.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlProvider.cs new file mode 100644 index 00000000000..b8ed7519cbc --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlProvider.cs @@ -0,0 +1,49 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Text.Formatting; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.MultiTenant; + +[Dependency(ReplaceServices = true)] +[ExposeServices(typeof(IMultiTenantUrlProvider), typeof(MultiTenantUrlProvider))] +public class WebAssemblyMultiTenantUrlProvider : MultiTenantUrlProvider +{ + private readonly static string[] ProtocolPrefixes = ["http://", "https://"]; + + protected NavigationManager NavigationManager { get; } + protected IOptions Options { get; } + + public WebAssemblyMultiTenantUrlProvider( + ICurrentTenant currentTenant, + ITenantStore tenantStore, + NavigationManager navigationManager, + IOptions options) + : base(currentTenant, tenantStore) + { + NavigationManager = navigationManager; + Options = options; + } + + public async override Task GetUrlAsync(string templateUrl) + { + if (!Options.Value.DomainFormat.IsNullOrEmpty() && !CurrentTenant.IsAvailable) + { + // If the domain format is configured and the tenant is not available + // try to extract the tenant name from the current blazor URL. + var url = NavigationManager.ToAbsoluteUri(NavigationManager.Uri).Authority; + var domainFormat = Options.Value.DomainFormat.RemovePreFix(ProtocolPrefixes).RemovePostFix("/"); + var extractResult = FormattedStringValueExtracter.Extract(url, domainFormat, ignoreCase: true); + if (extractResult.IsMatch) + { + var tenant = extractResult.Matches[0].Value; + return templateUrl.Replace(TenantPlaceHolder, tenant).Replace(TenantIdPlaceHolder, tenant).Replace(TenantNamePlaceHolder, tenant); + } + } + + return await base.GetUrlAsync(templateUrl); + } +}