Added tenant resolvers.

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent 776c0ca498
commit bc0165d892

@ -0,0 +1,8 @@
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public static class AbpAspNetCoreMultiTenancyConsts
{
//TODO: Get from an option instead of a constant!
public const string TenantIdKey = "__tenantId";
}
}

@ -11,14 +11,11 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
{
services.Configure<MultiTenancyOptions>(options =>
{
//TODO: domain/subdomain (not added by default) as first resolver!
options.TenantResolvers.Insert(0, new QueryStringTenantResolver());
//TODO: Web tenant resolvers:
// values can be tenant id or name, should be validated first by an abstraction (say, ITenantStore over ITenantManager)!
//- domain/subdomain (not added by default)
//- route {tenantId}
//+ querystring
//- header
//- cookie
options.TenantResolvers.Insert(1, new RouteTenantResolver());
options.TenantResolvers.Insert(2, new HeaderTenantResolver());
options.TenantResolvers.Insert(3, new CookieTenantResolver());
});
services.AddAssemblyOf<AbpAspNetMultiTenancyModule>();

@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Http;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public class CookieTenantResolver : HttpTenantResolverBase
{
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext)
{
return httpContext.Request.Cookies[AbpAspNetCoreMultiTenancyConsts.TenantIdKey];
}
}
}

@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Http;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public class HeaderTenantResolver : HttpTenantResolverBase
{
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext)
{
//TODO: Get first one if provided multiple values and write a log
return httpContext.Request.Headers[AbpAspNetCoreMultiTenancyConsts.TenantIdKey];
}
}
}

@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Http;
using Volo.Abp.MultiTenancy;
using Volo.ExtensionMethods;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public abstract class HttpTenantResolverBase : ITenantResolver
{
public virtual void Resolve(ITenantResolveContext context)
{
var httpContext = context.GetHttpContext();
if (httpContext == null)
{
return;
}
var tenantId = GetTenantIdFromHttpContextOrNull(httpContext);
if (!tenantId.IsNullOrEmpty())
{
context.Tenant = new TenantInfo(tenantId);
}
}
protected abstract string GetTenantIdFromHttpContextOrNull(HttpContext httpContext);
}
}

@ -1,30 +1,17 @@
using Volo.Abp.MultiTenancy;
using Volo.ExtensionMethods.Collections.Generic;
using Microsoft.AspNetCore.Http;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public class QueryStringTenantResolver : ITenantResolver
public class QueryStringTenantResolver : HttpTenantResolverBase
{
public const string TenantIdKey = "__tenantId";
public void Resolve(ITenantResolveContext context)
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext)
{
var httpContext = context.GetHttpContext();
if (httpContext == null)
{
return;
}
if (!httpContext.Request.QueryString.HasValue)
{
return;
return null;
}
var tenantId = httpContext.Request.Query[TenantIdKey];
if (!tenantId.IsNullOrEmpty())
{
context.Tenant = new TenantInfo(tenantId);
}
return httpContext.Request.Query[AbpAspNetCoreMultiTenancyConsts.TenantIdKey];
}
}
}

@ -0,0 +1,20 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public class RouteTenantResolver : HttpTenantResolverBase
{
protected override string GetTenantIdFromHttpContextOrNull(HttpContext httpContext)
{
var tenantId = httpContext.GetRouteValue(AbpAspNetCoreMultiTenancyConsts.TenantIdKey);
if (tenantId == null)
{
return null;
}
return Convert.ToString(tenantId);
}
}
}

@ -48,6 +48,9 @@ namespace Volo.Abp.MultiTenancy
foreach (var tenantResolver in _options.TenantResolvers)
{
tenantResolver.Resolve(context);
//TODO: Validate TenantId by a TenantStore (TenantId can be TenancyName, so also normalize it by tenant store)
if (context.IsHandled())
{
break;

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Net.Http.Headers;
using Shouldly;
using Volo.Abp.AspNetCore.App;
using Xunit;
@ -20,7 +21,29 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
{
const string testTenantId = "42";
var result = await GetResponseAsObjectAsync<Dictionary<string, string>>($"http://abp.io?{QueryStringTenantResolver.TenantIdKey}={testTenantId}");
var result = await GetResponseAsObjectAsync<Dictionary<string, string>>($"http://abp.io?{AbpAspNetCoreMultiTenancyConsts.TenantIdKey}={testTenantId}");
result["TenantId"].ShouldBe(testTenantId);
}
[Fact]
public async Task Should_Use_Header_Tenant_Id_If_Specified()
{
const string testTenantId = "42";
Client.DefaultRequestHeaders.Add(AbpAspNetCoreMultiTenancyConsts.TenantIdKey, testTenantId);
var result = await GetResponseAsObjectAsync<Dictionary<string, string>>("http://abp.io");
result["TenantId"].ShouldBe(testTenantId);
}
[Fact]
public async Task Should_Use_Cookie_Tenant_Id_If_Specified()
{
const string testTenantId = "42";
Client.DefaultRequestHeaders.Add("Cookie", new CookieHeaderValue(AbpAspNetCoreMultiTenancyConsts.TenantIdKey, testTenantId).ToString());
var result = await GetResponseAsObjectAsync<Dictionary<string, string>>("http://abp.io");
result["TenantId"].ShouldBe(testTenantId);
}
}

Loading…
Cancel
Save