-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20293 from abpframework/WebAssemblyMultiTenantUrl…
…Provider Add `WebAssemblyMultiTenantUrlProvider`.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
...olo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!; | ||
} |
49 changes: 49 additions & 0 deletions
49
...lo/Abp/AspNetCore/Components/WebAssembly/MultiTenant/WebAssemblyMultiTenantUrlProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |