mirror of https://github.com/abpframework/abp.git
8 changed files with 66 additions and 159 deletions
@ -0,0 +1,24 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.AspNetCore.MultiTenancy; |
|||
|
|||
public class DefaultTenantResolveContributor : TenantResolveContributorBase |
|||
{ |
|||
public const string ContributorName = "Default"; |
|||
|
|||
public override string Name => ContributorName; |
|||
|
|||
public override Task ResolveAsync(ITenantResolveContext context) |
|||
{ |
|||
var defaultTenant = context.GetAbpAspNetCoreMultiTenancyOptions().DefaultTenant; |
|||
if (!string.IsNullOrWhiteSpace(defaultTenant)) |
|||
{ |
|||
context.TenantIdOrName = defaultTenant; |
|||
context.Handled = true; |
|||
} |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
@ -1,27 +0,0 @@ |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Hosting; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.Abp.MultiTenancy; |
|||
|
|||
public class ConfigurationTenantResolveContributor : TenantResolveContributorBase |
|||
{ |
|||
public const string ContributorName = "Configuration"; |
|||
public override string Name => ContributorName; |
|||
|
|||
public override async Task ResolveAsync(ITenantResolveContext context) |
|||
{ |
|||
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>(); |
|||
var tenantIdOrName = configuration["MultiTenancy:Tenant"]; |
|||
|
|||
if (!tenantIdOrName.IsNullOrEmpty()) |
|||
{ |
|||
context.TenantIdOrName = tenantIdOrName; |
|||
} |
|||
|
|||
await Task.CompletedTask; |
|||
} |
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Volo.Abp.MultiTenancy; |
|||
|
|||
public static class ConfigurationTenantResolveOptionsExtensions |
|||
{ |
|||
public static void AddConfigurationTenantResolver( |
|||
this AbpTenantResolveOptions options) |
|||
{ |
|||
options.TenantResolvers.InsertAfter( |
|||
r => r is CurrentUserTenantResolveContributor, |
|||
new ConfigurationTenantResolveContributor() |
|||
); |
|||
} |
|||
} |
|||
@ -1,94 +0,0 @@ |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.FileProviders; |
|||
using Microsoft.Extensions.Hosting; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using System; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Xunit; |
|||
using Shouldly; |
|||
|
|||
public class ConfigurationTenantResolveContributor_Tests : MultiTenancyTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Resolve_Tenant_From_Configuration() |
|||
{ |
|||
var context = CreateContext("acme"); |
|||
|
|||
var contributor = new ConfigurationTenantResolveContributor(); |
|||
|
|||
await contributor.ResolveAsync(context); |
|||
|
|||
context.TenantIdOrName.ShouldBe("acme"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Resolve_If_Configuration_Is_Missing() |
|||
{ |
|||
var context = CreateContext(null); // No tenant set
|
|||
|
|||
var contributor = new ConfigurationTenantResolveContributor(); |
|||
|
|||
await contributor.ResolveAsync(context); |
|||
|
|||
context.TenantIdOrName.ShouldBeNull(); |
|||
} |
|||
|
|||
// Reusable setup
|
|||
private static ITenantResolveContext CreateContext(string? tenantName) |
|||
{ |
|||
var services = new ServiceCollection(); |
|||
|
|||
services.AddSingleton<IHostEnvironment>(new FakeHostEnvironment()); |
|||
var configBuilder = new ConfigurationBuilder(); |
|||
|
|||
if (!string.IsNullOrWhiteSpace(tenantName)) |
|||
{ |
|||
configBuilder.AddInMemoryCollection( |
|||
[ |
|||
new KeyValuePair<string, string>("MultiTenancy:Tenant", tenantName) |
|||
]); |
|||
} |
|||
|
|||
services.AddSingleton<IConfiguration>(configBuilder.Build()); |
|||
|
|||
var provider = services.BuildServiceProvider(); |
|||
return new FakeTenantResolveContext(provider); |
|||
} |
|||
|
|||
// Fake context
|
|||
private class FakeTenantResolveContext : ITenantResolveContext |
|||
{ |
|||
public IServiceProvider ServiceProvider { get; } |
|||
|
|||
public string? TenantIdOrName { get; set; } |
|||
|
|||
public bool Handled { get; set; } |
|||
|
|||
public FakeTenantResolveContext(IServiceProvider serviceProvider) |
|||
{ |
|||
ServiceProvider = serviceProvider; |
|||
} |
|||
|
|||
public bool HasResolvedTenantOrHost() |
|||
{ |
|||
return Handled || !string.IsNullOrWhiteSpace(TenantIdOrName); |
|||
} |
|||
} |
|||
|
|||
private class FakeHostEnvironment : IHostEnvironment |
|||
{ |
|||
public string EnvironmentName { get; set; } = Environments.Development; |
|||
public string ApplicationName { get; set; } = "TestApp"; |
|||
public string ContentRootPath { get; set; } = ""; |
|||
public IFileProvider ContentRootFileProvider { get; set; } = new NullFileProvider(); |
|||
} |
|||
|
|||
private class NullFileProvider : IFileProvider |
|||
{ |
|||
public IDirectoryContents GetDirectoryContents(string subpath) => new NotFoundDirectoryContents(); |
|||
public IFileInfo GetFileInfo(string subpath) => new NotFoundFileInfo(subpath); |
|||
public Microsoft.Extensions.Primitives.IChangeToken Watch(string filter) => NullChangeToken.Singleton; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue