From a5fd8d219591c3a26dcfb49f4349d4d9f0043b48 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 18 Jun 2021 13:41:16 +0800 Subject: [PATCH 1/4] Change the current tenant via querystrings. --- .../AbpAccountWebModule.cs | 2 + .../Pages/Account/AccountPageModel.cs | 8 ---- .../Pages/Account/ResetPassword.cshtml.cs | 42 ++++++++++++++++++- .../Volo.Abp.Account.Web.csproj | 1 + 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web/AbpAccountWebModule.cs b/modules/account/src/Volo.Abp.Account.Web/AbpAccountWebModule.cs index 4a806b9ae2..f845b7f273 100644 --- a/modules/account/src/Volo.Abp.Account.Web/AbpAccountWebModule.cs +++ b/modules/account/src/Volo.Abp.Account.Web/AbpAccountWebModule.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Account.Localization; using Volo.Abp.Account.Web.Pages.Account; using Volo.Abp.Account.Web.ProfileManagement; +using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc.Localization; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; @@ -21,6 +22,7 @@ namespace Volo.Abp.Account.Web typeof(AbpIdentityAspNetCoreModule), typeof(AbpAutoMapperModule), typeof(AbpAspNetCoreMvcUiThemeSharedModule), + typeof(AbpAspNetCoreMultiTenancyModule), typeof(AbpExceptionHandlingModule) )] public class AbpAccountWebModule : AbpModule diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs index c4461bd512..f7a3e373c5 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs @@ -37,14 +37,6 @@ namespace Volo.Abp.Account.Web.Pages.Account //identityResult.CheckErrors(LocalizationManager); //TODO: Get from old Abp } - protected virtual void CheckCurrentTenant(Guid? tenantId) - { - if (CurrentTenant.Id != tenantId) - { - throw new ApplicationException($"Current tenant is different than given tenant. CurrentTenant.Id: {CurrentTenant.Id}, given tenantId: {tenantId}"); - } - } - protected virtual string GetLocalizeExceptionMessage(Exception exception) { if (exception is ILocalizeErrorMessage || exception is IHasErrorCode) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs index 44317eaaa5..508f2d1447 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs @@ -1,7 +1,11 @@ using System; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.Auditing; using Volo.Abp.Identity; using Volo.Abp.MultiTenancy; @@ -51,13 +55,49 @@ namespace Volo.Abp.Account.Web.Pages.Account protected virtual ITenantResolveResultAccessor TenantResolveResultAccessor { get; } - public ResetPasswordModel(ITenantResolveResultAccessor tenantResolveResultAccessor) + protected virtual AbpAspNetCoreMultiTenancyOptions AspNetCoreMultiTenancyOptions { get; } + + protected virtual AbpMultiTenancyOptions MultiTenancyOptions { get; } + + public ResetPasswordModel( + ITenantResolveResultAccessor tenantResolveResultAccessor, + IOptions aspNetCoreMultiTenancyOptions, + IOptions multiTenancyOptions) { TenantResolveResultAccessor = tenantResolveResultAccessor; + AspNetCoreMultiTenancyOptions = aspNetCoreMultiTenancyOptions.Value; + MultiTenancyOptions = multiTenancyOptions.Value; } public virtual Task OnGetAsync() { + if (MultiTenancyOptions.IsEnabled && + TenantResolveResultAccessor.Result?.AppliedResolvers?.Contains(CookieTenantResolveContributor.ContributorName) == true) + { + if (CurrentTenant.Id != TenantId) + { + if (TenantId != null) + { + Response.Cookies.Append( + AspNetCoreMultiTenancyOptions.TenantKey, + TenantId.ToString(), + new CookieOptions + { + Path = "/", + HttpOnly = false, + Expires = DateTimeOffset.Now.AddYears(10) + } + ); + } + else + { + Response.Cookies.Delete(AspNetCoreMultiTenancyOptions.TenantKey); + } + + return Task.FromResult(Redirect(HttpContext.Request.GetEncodedUrl())); + } + } + return Task.FromResult(Page()); } diff --git a/modules/account/src/Volo.Abp.Account.Web/Volo.Abp.Account.Web.csproj b/modules/account/src/Volo.Abp.Account.Web/Volo.Abp.Account.Web.csproj index ac59aa33ba..da95a01a8a 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Volo.Abp.Account.Web.csproj +++ b/modules/account/src/Volo.Abp.Account.Web/Volo.Abp.Account.Web.csproj @@ -35,6 +35,7 @@ + From 89e560edfa552f06e8803cdba2b7ebecc45636de Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 18 Jun 2021 13:58:46 +0800 Subject: [PATCH 2/4] Add SwitchTenant method to AccountPageModel. --- .../Pages/Account/AccountPageModel.cs | 44 ++++++++++++++++++ .../Pages/Account/ResetPassword.cshtml.cs | 46 +------------------ 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs index f7a3e373c5..17cee43c5d 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs @@ -1,13 +1,19 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Volo.Abp.Account.Localization; using Volo.Abp.AspNetCore.ExceptionHandling; +using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; using Volo.Abp.ExceptionHandling; using Volo.Abp.Identity; +using Volo.Abp.MultiTenancy; using IdentityUser = Volo.Abp.Identity.IdentityUser; namespace Volo.Abp.Account.Web.Pages.Account @@ -21,12 +27,50 @@ namespace Volo.Abp.Account.Web.Pages.Account public IOptions IdentityOptions { get; set; } public IExceptionToErrorInfoConverter ExceptionToErrorInfoConverter { get; set; } + public ITenantResolveResultAccessor TenantResolveResultAccessor { get; set; } + + public IOptions AspNetCoreMultiTenancyOptions { get; set; } + + public IOptions MultiTenancyOptions { get; set; } + protected AccountPageModel() { LocalizationResourceType = typeof(AccountResource); ObjectMapperContext = typeof(AbpAccountWebModule); } + protected virtual bool SwitchTenant(Guid? tenantId) + { + if (MultiTenancyOptions.Value.IsEnabled && + TenantResolveResultAccessor.Result?.AppliedResolvers?.Contains(CookieTenantResolveContributor.ContributorName) == true) + { + if (CurrentTenant.Id != tenantId) + { + if (tenantId != null) + { + Response.Cookies.Append( + AspNetCoreMultiTenancyOptions.Value.TenantKey, + tenantId.ToString(), + new CookieOptions + { + Path = "/", + HttpOnly = false, + Expires = DateTimeOffset.Now.AddYears(10) + } + ); + } + else + { + Response.Cookies.Delete(AspNetCoreMultiTenancyOptions.Value.TenantKey); + } + + return true; + } + } + + return false; + } + protected virtual void CheckIdentityErrors(IdentityResult identityResult) { if (!identityResult.Succeeded) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs index 508f2d1447..7067e186ef 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs @@ -1,14 +1,10 @@ using System; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; -using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.Auditing; using Volo.Abp.Identity; -using Volo.Abp.MultiTenancy; using Volo.Abp.Validation; namespace Volo.Abp.Account.Web.Pages.Account @@ -53,49 +49,11 @@ namespace Volo.Abp.Account.Web.Pages.Account [DisableAuditing] public string ConfirmPassword { get; set; } - protected virtual ITenantResolveResultAccessor TenantResolveResultAccessor { get; } - - protected virtual AbpAspNetCoreMultiTenancyOptions AspNetCoreMultiTenancyOptions { get; } - - protected virtual AbpMultiTenancyOptions MultiTenancyOptions { get; } - - public ResetPasswordModel( - ITenantResolveResultAccessor tenantResolveResultAccessor, - IOptions aspNetCoreMultiTenancyOptions, - IOptions multiTenancyOptions) - { - TenantResolveResultAccessor = tenantResolveResultAccessor; - AspNetCoreMultiTenancyOptions = aspNetCoreMultiTenancyOptions.Value; - MultiTenancyOptions = multiTenancyOptions.Value; - } - public virtual Task OnGetAsync() { - if (MultiTenancyOptions.IsEnabled && - TenantResolveResultAccessor.Result?.AppliedResolvers?.Contains(CookieTenantResolveContributor.ContributorName) == true) + if (SwitchTenant(TenantId)) { - if (CurrentTenant.Id != TenantId) - { - if (TenantId != null) - { - Response.Cookies.Append( - AspNetCoreMultiTenancyOptions.TenantKey, - TenantId.ToString(), - new CookieOptions - { - Path = "/", - HttpOnly = false, - Expires = DateTimeOffset.Now.AddYears(10) - } - ); - } - else - { - Response.Cookies.Delete(AspNetCoreMultiTenancyOptions.TenantKey); - } - - return Task.FromResult(Redirect(HttpContext.Request.GetEncodedUrl())); - } + return Task.FromResult(Redirect(HttpContext.Request.GetEncodedUrl())); } return Task.FromResult(Page()); From 8d4bd07a45f7fea9c288b9791713b33af86c42b7 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 18 Jun 2021 14:00:10 +0800 Subject: [PATCH 3/4] Remove spaces. --- .../Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs index 17cee43c5d..8e12f4648a 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs @@ -29,9 +29,9 @@ namespace Volo.Abp.Account.Web.Pages.Account public ITenantResolveResultAccessor TenantResolveResultAccessor { get; set; } - public IOptions AspNetCoreMultiTenancyOptions { get; set; } + public IOptions AspNetCoreMultiTenancyOptions { get; set; } - public IOptions MultiTenancyOptions { get; set; } + public IOptions MultiTenancyOptions { get; set; } protected AccountPageModel() { From 848aa2ecfac51d4deef47377733bc3bc4e87abed Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 18 Jun 2021 14:52:14 +0800 Subject: [PATCH 4/4] Add CheckCurrentTenant. --- .../Pages/Account/AccountPageModel.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs index 8e12f4648a..32f2398629 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs @@ -71,6 +71,14 @@ namespace Volo.Abp.Account.Web.Pages.Account return false; } + protected virtual void CheckCurrentTenant(Guid? tenantId) + { + if (CurrentTenant.Id != tenantId) + { + throw new ApplicationException($"Current tenant is different than given tenant. CurrentTenant.Id: {CurrentTenant.Id}, given tenantId: {tenantId}"); + } + } + protected virtual void CheckIdentityErrors(IdentityResult identityResult) { if (!identityResult.Succeeded)