Skip to content

Commit

Permalink
Add WebAssemblyMultiTenantUrlProvider.
Browse files Browse the repository at this point in the history
Resolve #20292
  • Loading branch information
maliming committed Jul 26, 2024
1 parent e458e4f commit 91c6ee0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Volo.Abp.AspNetCore.Components.WebAssembly.MultiTenant;

public class WebAssemblyMultiTenantUrlOptions
{
public string DomainFormat { get; set; } = default!;
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 91c6ee0

Please sign in to comment.