Switch to the tenant if __tenant querystring parameter is sent.

Resolve #9383
pull/9510/head
maliming 4 years ago
parent 6471b541b7
commit 43db5d683a

@ -0,0 +1,32 @@
using System;
using Microsoft.AspNetCore.Http;
namespace Volo.Abp.AspNetCore.MultiTenancy
{
public static class AbpMultiTenancyCookieHelper
{
public static void SetTenantCookie(
HttpContext context,
Guid? tenantId,
string tenantKey)
{
if (tenantId != null)
{
context.Response.Cookies.Append(
tenantKey,
tenantId.ToString(),
new CookieOptions
{
Path = "/",
HttpOnly = false,
Expires = DateTimeOffset.Now.AddYears(10)
}
);
}
else
{
context.Response.Cookies.Delete(tenantKey);
}
}
}
}

@ -1,9 +1,6 @@
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.RequestLocalization;
@ -21,14 +18,17 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
private readonly ITenantConfigurationProvider _tenantConfigurationProvider;
private readonly ICurrentTenant _currentTenant;
private readonly AbpAspNetCoreMultiTenancyOptions _options;
private readonly ITenantResolveResultAccessor _tenantResolveResultAccessor;
public MultiTenancyMiddleware(
ITenantConfigurationProvider tenantConfigurationProvider,
ICurrentTenant currentTenant,
IOptions<AbpAspNetCoreMultiTenancyOptions> options)
IOptions<AbpAspNetCoreMultiTenancyOptions> options,
ITenantResolveResultAccessor tenantResolveResultAccessor)
{
_tenantConfigurationProvider = tenantConfigurationProvider;
_currentTenant = currentTenant;
_tenantResolveResultAccessor = tenantResolveResultAccessor;
_options = options.Value;
}
@ -49,6 +49,12 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
{
using (_currentTenant.Change(tenant?.Id, tenant?.Name))
{
if (_tenantResolveResultAccessor.Result != null &&
_tenantResolveResultAccessor.Result.AppliedResolvers.Contains(QueryStringTenantResolveContributor.ContributorName))
{
AbpMultiTenancyCookieHelper.SetTenantCookie(context, _currentTenant.Id, _options.TenantKey);
}
var requestCulture = await TryGetRequestCultureAsync(context);
if (requestCulture != null)
{

@ -41,11 +41,8 @@ namespace Pages.Abp.MultiTenancy
public async Task OnPostAsync()
{
if (Input.Name.IsNullOrEmpty())
{
Response.Cookies.Delete(Options.TenantKey);
}
else
Guid? tenantId = null;
if (!Input.Name.IsNullOrEmpty())
{
var tenant = await TenantStore.FindAsync(Input.Name);
if (tenant == null)
@ -58,17 +55,10 @@ namespace Pages.Abp.MultiTenancy
throw new UserFriendlyException(L["GivenTenantIsNotAvailable", Input.Name]);
}
Response.Cookies.Append(
Options.TenantKey,
tenant.Id.ToString(),
new CookieOptions
{
Path = "/",
HttpOnly = false,
Expires = DateTimeOffset.Now.AddYears(10)
}
);
tenantId = tenant.Id;
}
AbpMultiTenancyCookieHelper.SetTenantCookie(HttpContext, tenantId, Options.TenantKey);
}
public class TenantInfoModel

@ -8,8 +8,6 @@ namespace Volo.Abp.Account
{
public Guid UserId { get; set; }
public Guid? TenantId { get; set; }
[Required]
public string ResetToken { get; set; }

@ -61,8 +61,6 @@ namespace Volo.Abp.Account
}
public virtual async Task ResetPasswordAsync(ResetPasswordDto input)
{
using (CurrentTenant.Change(input.TenantId))
{
await IdentityOptions.SetAsync();
@ -75,7 +73,6 @@ namespace Volo.Abp.Account
Action = IdentitySecurityLogActionConsts.ChangePassword
});
}
}
protected virtual async Task<IdentityUser> GetUserByEmail(string email)
{

@ -48,7 +48,8 @@ namespace Volo.Abp.Account.Emailing
var url = await AppUrlProvider.GetResetPasswordUrlAsync(appName);
var link = $"{url}?userId={user.Id}&tenantId={user.TenantId}&resetToken={UrlEncoder.Default.Encode(resetToken)}";
//TODO: Use AbpAspNetCoreMultiTenancyOptions to get the key
var link = $"{url}?userId={user.Id}&{TenantResolverConsts.DefaultTenantKey}={user.TenantId}&resetToken={UrlEncoder.Default.Encode(resetToken)}";
if (!returnUrl.IsNullOrEmpty())
{

@ -3,7 +3,6 @@ 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;
@ -22,7 +21,6 @@ namespace Volo.Abp.Account.Web
typeof(AbpIdentityAspNetCoreModule),
typeof(AbpAutoMapperModule),
typeof(AbpAspNetCoreMvcUiThemeSharedModule),
typeof(AbpAspNetCoreMultiTenancyModule),
typeof(AbpExceptionHandlingModule)
)]
public class AbpAccountWebModule : AbpModule

@ -1,19 +1,13 @@
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
@ -27,50 +21,12 @@ namespace Volo.Abp.Account.Web.Pages.Account
public IOptions<IdentityOptions> IdentityOptions { get; set; }
public IExceptionToErrorInfoConverter ExceptionToErrorInfoConverter { get; set; }
public ITenantResolveResultAccessor TenantResolveResultAccessor { get; set; }
public IOptions<AbpAspNetCoreMultiTenancyOptions> AspNetCoreMultiTenancyOptions { get; set; }
public IOptions<AbpMultiTenancyOptions> 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 CheckCurrentTenant(Guid? tenantId)
{
if (CurrentTenant.Id != tenantId)

@ -1,7 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Auditing;
using Volo.Abp.Identity;
@ -10,13 +9,8 @@ using Volo.Abp.Validation;
namespace Volo.Abp.Account.Web.Pages.Account
{
//TODO: Implement live password complexity check on the razor view!
public class ResetPasswordModel : AccountPageModel
{
[HiddenInput]
[BindProperty(SupportsGet = true)]
public Guid? TenantId { get; set; }
[Required]
[HiddenInput]
[BindProperty(SupportsGet = true)]
@ -51,11 +45,6 @@ namespace Volo.Abp.Account.Web.Pages.Account
public virtual Task<IActionResult> OnGetAsync()
{
if (SwitchTenant(TenantId))
{
return Task.FromResult<IActionResult>(Redirect(HttpContext.Request.GetEncodedUrl()));
}
return Task.FromResult<IActionResult>(Page());
}
@ -70,8 +59,7 @@ namespace Volo.Abp.Account.Web.Pages.Account
{
UserId = UserId,
ResetToken = ResetToken,
Password = Password,
TenantId = TenantId
Password = Password
}
);
}

@ -35,7 +35,6 @@
<ItemGroup>
<ProjectReference Include="..\..\..\identity\src\Volo.Abp.Identity.AspNetCore\Volo.Abp.Identity.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared\Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.AspNetCore.MultiTenancy\Volo.Abp.AspNetCore.MultiTenancy.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.AutoMapper\Volo.Abp.AutoMapper.csproj" />
<ProjectReference Include="..\Volo.Abp.Account.HttpApi\Volo.Abp.Account.HttpApi.csproj" />
</ItemGroup>

@ -57,7 +57,8 @@
<abp-row>
<abp-column class="col mx-auto" style="max-width: 440px">
@if (MultiTenancyOptions.Value.IsEnabled &&
(TenantResolveResultAccessor.Result?.AppliedResolvers?.Contains(CookieTenantResolveContributor.ContributorName) == true))
(TenantResolveResultAccessor.Result?.AppliedResolvers?.Contains(CookieTenantResolveContributor.ContributorName) == true ||
TenantResolveResultAccessor.Result?.AppliedResolvers?.Contains(QueryStringTenantResolveContributor.ContributorName) == true))
{
<div class="card shadow-sm rounded mb-3">
<div class="card-body px-5">

Loading…
Cancel
Save