Browse Source

Refactored.

pull/189/head
Halil İbrahim Kalkan 9 years ago
parent
commit
b5f5a62c3e
  1. 17
      docs/Multi-Tenancy.md
  2. 20
      src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs
  3. 6
      src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs
  4. 2
      src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/CookieTenantResolver.cs
  5. 2
      src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/HeaderTenantResolver.cs
  6. 2
      src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/QueryStringTenantResolver.cs
  7. 2
      src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/RouteTenantResolver.cs
  8. 8
      test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Tests.cs

17
docs/Multi-Tenancy.md

@ -314,10 +314,19 @@ namespace MyCompany.MyProject
Volo.Abp.AspNetCore.MultiTenancy package adds following tenant resolvers to determine current tenant from current web request (ordered by priority). These resolvers are added and work out of the box:
* **QueryStringTenantResolver**: Tries to find current tenant id from query string parameter. Parameter name is "__tenantId" by default.
* **RouteTenantResolver**: Tries to find current tenant id from route (URL path). Variable name is "__tenantId" by default. So, if you defined a route with this variable, then it can determine the current tenant from the route.
* **HeaderTenantResolver**: Tries to find current tenant id from HTTP header. Header name is "__tenantId" by default.
* **CookieTenantResolver**: Tries to find current tenant id from cookie values. Cookie name is "__tenantId" by default.
* **QueryStringTenantResolver**: Tries to find current tenant id from query string parameter. Parameter name is "__tenant" by default.
* **RouteTenantResolver**: Tries to find current tenant id from route (URL path). Variable name is "__tenant" by default. So, if you defined a route with this variable, then it can determine the current tenant from the route.
* **HeaderTenantResolver**: Tries to find current tenant id from HTTP header. Header name is "__tenant" by default.
* **CookieTenantResolver**: Tries to find current tenant id from cookie values. Cookie name is "__tenant" by default.
"__tenant" parameter name can be changed using AspNetCoreMultiTenancyOptions. Example:
````C#
services.Configure<AspNetCoreMultiTenancyOptions>(options =>
{
options.TenantKey = "MyTenantKey";
});
````
##### Domain Tenant Resolver

20
src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs

@ -13,6 +13,7 @@ using Volo.Abp;
using Volo.Abp.Account.Web;
using Volo.Abp.AspNetCore.EmbeddedFiles;
using Volo.Abp.AspNetCore.Modularity;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap;
@ -41,7 +42,8 @@ namespace AbpDesk.Web.Mvc
typeof(AbpAccountWebModule),
typeof(AbpAutofacModule),
typeof(AbpIdentityServerDomainModule),
typeof(AbpIdentityServerEntityFrameworkCoreModule)
typeof(AbpIdentityServerEntityFrameworkCoreModule),
typeof(AbpAspNetCoreMultiTenancyModule)
)]
public class AbpDeskWebMvcModule : AbpModule //TODO: Rename to AbpDeskWebModule, change default namespace to AbpDesk.Web
{
@ -65,7 +67,21 @@ namespace AbpDesk.Web.Mvc
AbpDeskDbConfigurer.Configure(services, configuration);
services.Configure<ConfigurationTenantStoreOptions>(configuration);
//TODO: Getting from appsettings.json didn't worked somehow.
services.Configure<ConfigurationTenantStoreOptions>(options =>
{
options.Tenants = new[]
{
new TenantInformation(
Guid.Parse("446a5211-3d72-4339-9adc-845151f8ada0"),
"acme"
),
new TenantInformation(
Guid.Parse("25388015-ef1c-4355-9c18-f6b6ddbaf89d"),
"volosoft"
)
};
});
services.Configure<NavigationOptions>(options =>
{

6
src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs

@ -3,13 +3,13 @@
public class AspNetCoreMultiTenancyOptions
{
/// <summary>
/// Default: "__tenantId".
/// Default: "__tenant".
/// </summary>
public string TenantIdKey { get; set; }
public string TenantKey { get; set; }
public AspNetCoreMultiTenancyOptions()
{
TenantIdKey = "__tenantId";
TenantKey = "__tenant";
}
}
}

2
src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/CookieTenantResolver.cs

@ -7,7 +7,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
{
protected override string GetTenantIdOrNameFromHttpContextOrNull(ITenantResolveContext context, HttpContext httpContext)
{
return httpContext.Request?.Cookies[context.GetAspNetCoreMultiTenancyOptions().TenantIdKey];
return httpContext.Request?.Cookies[context.GetAspNetCoreMultiTenancyOptions().TenantKey];
}
}
}

2
src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/HeaderTenantResolver.cs

@ -17,7 +17,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
return null;
}
var tenantIdKey = context.GetAspNetCoreMultiTenancyOptions().TenantIdKey;
var tenantIdKey = context.GetAspNetCoreMultiTenancyOptions().TenantKey;
var tenantIdHeader = httpContext.Request.Headers[tenantIdKey];
if (tenantIdHeader == string.Empty || tenantIdHeader.Count < 1)

2
src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/QueryStringTenantResolver.cs

@ -12,7 +12,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
return null;
}
return httpContext.Request.Query[context.GetAspNetCoreMultiTenancyOptions().TenantIdKey];
return httpContext.Request.Query[context.GetAspNetCoreMultiTenancyOptions().TenantKey];
}
}
}

2
src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/RouteTenantResolver.cs

@ -9,7 +9,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
{
protected override string GetTenantIdOrNameFromHttpContextOrNull(ITenantResolveContext context, HttpContext httpContext)
{
var tenantId = httpContext.GetRouteValue(context.GetAspNetCoreMultiTenancyOptions().TenantIdKey);
var tenantId = httpContext.GetRouteValue(context.GetAspNetCoreMultiTenancyOptions().TenantKey);
if (tenantId == null)
{
return null;

8
test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Tests.cs

@ -48,14 +48,14 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
public async Task Should_Use_QueryString_Tenant_Id_If_Specified()
{
var result = await GetResponseAsObjectAsync<Dictionary<string, string>>($"http://abp.io?{_options.TenantIdKey}={_testTenantName}");
var result = await GetResponseAsObjectAsync<Dictionary<string, string>>($"http://abp.io?{_options.TenantKey}={_testTenantName}");
result["TenantId"].ShouldBe(_testTenantId.ToString());
}
[Fact]
public async Task Should_Use_Header_Tenant_Id_If_Specified()
{
Client.DefaultRequestHeaders.Add(_options.TenantIdKey, _testTenantId.ToString());
Client.DefaultRequestHeaders.Add(_options.TenantKey, _testTenantId.ToString());
var result = await GetResponseAsObjectAsync<Dictionary<string, string>>("http://abp.io");
result["TenantId"].ShouldBe(_testTenantId.ToString());
@ -71,7 +71,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
[Fact]
public async Task Should_Use_Domain_As_First_Priority_If_Specified()
{
Client.DefaultRequestHeaders.Add(_options.TenantIdKey, Guid.NewGuid().ToString());
Client.DefaultRequestHeaders.Add(_options.TenantKey, Guid.NewGuid().ToString());
var result = await GetResponseAsObjectAsync<Dictionary<string, string>>("http://acme.abp.io");
result["TenantId"].ShouldBe(_testTenantId.ToString());
@ -80,7 +80,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
[Fact]
public async Task Should_Use_Cookie_Tenant_Id_If_Specified()
{
Client.DefaultRequestHeaders.Add("Cookie", new CookieHeaderValue(_options.TenantIdKey, _testTenantId.ToString()).ToString());
Client.DefaultRequestHeaders.Add("Cookie", new CookieHeaderValue(_options.TenantKey, _testTenantId.ToString()).ToString());
var result = await GetResponseAsObjectAsync<Dictionary<string, string>>("http://abp.io");
result["TenantId"].ShouldBe(_testTenantId.ToString());

Loading…
Cancel
Save