Browse Source

Support tenant place holder in `IsRedirectAllowedUrlAsync` method.

pull/18492/head
maliming 3 years ago
parent
commit
3a8d06b238
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 17
      framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/Urls/AppUrlProvider.cs
  2. 4
      framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/Urls/IAppUrlProvider.cs
  3. 29
      framework/test/Volo.Abp.UI.Navigation.Tests/Volo/Abp/Ui/Navigation/AppUrlProvider_Tests.cs

17
framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/Urls/AppUrlProvider.cs

@ -43,9 +43,14 @@ public class AppUrlProvider : IAppUrlProvider, ITransientDependency
);
}
public bool IsRedirectAllowedUrl(string url)
public virtual async Task<bool> IsRedirectAllowedUrlAsync(string url)
{
var allow = Options.RedirectAllowedUrls.Any(x => url.StartsWith(x, StringComparison.CurrentCultureIgnoreCase));
var redirectAllowedUrls = new List<string>();
foreach (var redirectAllowedUrl in Options.RedirectAllowedUrls)
{
redirectAllowedUrls.Add((await NormalizeUrlAsync(redirectAllowedUrl))!);
}
var allow = redirectAllowedUrls.Any(x => url.StartsWith(x, StringComparison.CurrentCultureIgnoreCase));
if (!allow)
{
Logger.LogError($"Invalid RedirectUrl: {url}, Use {nameof(AppUrlProvider)} to configure it!");
@ -59,7 +64,7 @@ public class AppUrlProvider : IAppUrlProvider, ITransientDependency
{
return url;
}
return await ReplacePlaceHoldersAsync(url!);
}
@ -117,15 +122,15 @@ public class AppUrlProvider : IAppUrlProvider, ITransientDependency
return url;
}
private async Task<string> GetCurrentTenantNameAsync()
protected virtual async Task<string?> GetCurrentTenantNameAsync()
{
if (CurrentTenant.Id.HasValue && CurrentTenant.Name.IsNullOrEmpty())
{
var tenantConfiguration = await TenantStore.FindAsync(CurrentTenant.Id.Value);
return tenantConfiguration!.Name;
return tenantConfiguration?.Name;
}
return CurrentTenant.Name!;
return CurrentTenant.Name;
}
public Task<string?> GetUrlOrNullAsync([NotNull] string appName, string? urlName = null)

4
framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/Urls/IAppUrlProvider.cs

@ -9,7 +9,7 @@ public interface IAppUrlProvider
Task<string?> GetUrlOrNullAsync([NotNull] string appName, string? urlName = null);
bool IsRedirectAllowedUrl(string url);
Task<bool> IsRedirectAllowedUrlAsync(string url);
Task<string?> NormalizeUrlAsync(string? url);
}

29
framework/test/Volo.Abp.UI.Navigation.Tests/Volo/Abp/Ui/Navigation/AppUrlProvider_Tests.cs

@ -35,7 +35,9 @@ public class AppUrlProvider_Tests : AbpIntegratedTest<AbpUiNavigationTestModule>
options.RedirectAllowedUrls.AddRange(new List<string>()
{
"https://wwww.volosoft.com",
"https://wwww.aspnetzero.com"
"https://wwww.aspnetzero.com",
"https://{{tenantName}}.abp.io",
"https://{{tenantId}}.abp.io"
});
options.Applications["BLAZOR"].RootUrl = "https://{{tenantId}}.abp.io";
@ -87,9 +89,28 @@ public class AppUrlProvider_Tests : AbpIntegratedTest<AbpUiNavigationTestModule>
}
[Fact]
public void IsRedirectAllowedUrl()
public async Task IsRedirectAllowedUrlAsync()
{
_appUrlProvider.IsRedirectAllowedUrl("https://community.abp.io").ShouldBeFalse();
_appUrlProvider.IsRedirectAllowedUrl("https://wwww.volosoft.com").ShouldBeTrue();
(await _appUrlProvider.IsRedirectAllowedUrlAsync("https://community.abp.io")).ShouldBeFalse();
(await _appUrlProvider.IsRedirectAllowedUrlAsync("https://wwww.volosoft.com")).ShouldBeTrue();
using (_currentTenant.Change(null))
{
(await _appUrlProvider.IsRedirectAllowedUrlAsync("https://www.abp.io")).ShouldBeFalse();
(await _appUrlProvider.IsRedirectAllowedUrlAsync("https://abp.io")).ShouldBeTrue();
}
using (_currentTenant.Change(Guid.NewGuid(), "community"))
{
(await _appUrlProvider.IsRedirectAllowedUrlAsync("https://community.abp.io")).ShouldBeTrue();
(await _appUrlProvider.IsRedirectAllowedUrlAsync("https://community2.abp.io")).ShouldBeFalse();
}
var tenantId = Guid.NewGuid();
using (_currentTenant.Change(tenantId))
{
(await _appUrlProvider.IsRedirectAllowedUrlAsync($"https://{tenantId}.abp.io")).ShouldBeTrue();
(await _appUrlProvider.IsRedirectAllowedUrlAsync($"https://{Guid.NewGuid()}.abp.io")).ShouldBeFalse();
}
}
}

Loading…
Cancel
Save