From b5f5a62c3e0d8290443fb1a71869bc193bcbe934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 5 Jan 2018 15:31:03 +0300 Subject: [PATCH] Refactored. --- docs/Multi-Tenancy.md | 17 ++++++++++++---- .../AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs | 20 +++++++++++++++++-- .../AspNetCoreMultiTenancyOptions.cs | 6 +++--- .../MultiTenancy/CookieTenantResolver.cs | 2 +- .../MultiTenancy/HeaderTenantResolver.cs | 2 +- .../MultiTenancy/QueryStringTenantResolver.cs | 2 +- .../MultiTenancy/RouteTenantResolver.cs | 2 +- .../AspNetCoreMultiTenancy_Tests.cs | 8 ++++---- 8 files changed, 42 insertions(+), 17 deletions(-) diff --git a/docs/Multi-Tenancy.md b/docs/Multi-Tenancy.md index 5e3d834b45..fd7c344092 100644 --- a/docs/Multi-Tenancy.md +++ b/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(options => +{ + options.TenantKey = "MyTenantKey"; +}); +```` ##### Domain Tenant Resolver diff --git a/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs b/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs index 919ffda513..97278ed865 100644 --- a/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs +++ b/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(configuration); + //TODO: Getting from appsettings.json didn't worked somehow. + services.Configure(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(options => { diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs index db4423f921..fda1971eb1 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs +++ b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs @@ -3,13 +3,13 @@ public class AspNetCoreMultiTenancyOptions { /// - /// Default: "__tenantId". + /// Default: "__tenant". /// - public string TenantIdKey { get; set; } + public string TenantKey { get; set; } public AspNetCoreMultiTenancyOptions() { - TenantIdKey = "__tenantId"; + TenantKey = "__tenant"; } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/CookieTenantResolver.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/CookieTenantResolver.cs index 9837180c47..2282cd9c96 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/CookieTenantResolver.cs +++ b/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]; } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/HeaderTenantResolver.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/HeaderTenantResolver.cs index 3ca1c3623d..7b6931a6a0 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/HeaderTenantResolver.cs +++ b/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) diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/QueryStringTenantResolver.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/QueryStringTenantResolver.cs index 93eae8d345..c0361ac6be 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/QueryStringTenantResolver.cs +++ b/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]; } } } diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/RouteTenantResolver.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/RouteTenantResolver.cs index d730e834ac..4e3c0897d6 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/RouteTenantResolver.cs +++ b/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; diff --git a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Tests.cs b/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Tests.cs index b193574662..ad54e1bf19 100644 --- a/test/Volo.Abp.AspNetCore.MultiTenancy.Tests/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancy_Tests.cs +++ b/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>($"http://abp.io?{_options.TenantIdKey}={_testTenantName}"); + var result = await GetResponseAsObjectAsync>($"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>("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>("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>("http://abp.io"); result["TenantId"].ShouldBe(_testTenantId.ToString());