Browse Source

Merge pull request #20293 from abpframework/WebAssemblyMultiTenantUrlProvider

Add `WebAssemblyMultiTenantUrlProvider`.
pull/20361/head
Engincan VESKE 2 years ago
committed by GitHub
parent
commit
fa8973b81d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 6
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlOptions.cs
  2. 49
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlProvider.cs

6
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!;
}

49
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<WebAssemblyMultiTenantUrlOptions> Options { get; }
public WebAssemblyMultiTenantUrlProvider(
ICurrentTenant currentTenant,
ITenantStore tenantStore,
NavigationManager navigationManager,
IOptions<WebAssemblyMultiTenantUrlOptions> options)
: base(currentTenant, tenantStore)
{
NavigationManager = navigationManager;
Options = options;
}
public async override Task<string> 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);
}
}
Loading…
Cancel
Save