Merge pull request #9510 from abpframework/maliming/__tenant

Switch to the tenant if __tenant querystring parameter is sent.
pull/9539/head
Halil İbrahim Kalkan 4 years ago committed by GitHub
commit 9044bc0cee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -1,4 +1,5 @@
using System.Threading.Tasks; using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
@ -12,9 +13,23 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
protected override Task<string> GetTenantIdOrNameFromHttpContextOrNullAsync(ITenantResolveContext context, HttpContext httpContext) protected override Task<string> GetTenantIdOrNameFromHttpContextOrNullAsync(ITenantResolveContext context, HttpContext httpContext)
{ {
return Task.FromResult(httpContext.Request.QueryString.HasValue if (httpContext.Request.QueryString.HasValue)
? httpContext.Request.Query[context.GetAbpAspNetCoreMultiTenancyOptions().TenantKey].ToString() {
: null); var tenantKey = context.GetAbpAspNetCoreMultiTenancyOptions().TenantKey;
if (httpContext.Request.Query.ContainsKey(tenantKey))
{
var tenantValue = httpContext.Request.Query[tenantKey].ToString();
if (tenantValue.IsNullOrWhiteSpace())
{
context.Handled = true;
return Task.FromResult<string>(null);
}
return Task.FromResult(tenantValue);
}
}
return Task.FromResult<string>(null);
} }
} }
} }

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

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

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

@ -48,7 +48,8 @@ namespace Volo.Abp.Account.Emailing
var url = await AppUrlProvider.GetResetPasswordUrlAsync(appName); 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()) if (!returnUrl.IsNullOrEmpty())
{ {

@ -3,7 +3,6 @@ using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Account.Localization; using Volo.Abp.Account.Localization;
using Volo.Abp.Account.Web.Pages.Account; using Volo.Abp.Account.Web.Pages.Account;
using Volo.Abp.Account.Web.ProfileManagement; using Volo.Abp.Account.Web.ProfileManagement;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc.Localization; using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling; using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
@ -22,7 +21,6 @@ namespace Volo.Abp.Account.Web
typeof(AbpIdentityAspNetCoreModule), typeof(AbpIdentityAspNetCoreModule),
typeof(AbpAutoMapperModule), typeof(AbpAutoMapperModule),
typeof(AbpAspNetCoreMvcUiThemeSharedModule), typeof(AbpAspNetCoreMvcUiThemeSharedModule),
typeof(AbpAspNetCoreMultiTenancyModule),
typeof(AbpExceptionHandlingModule) typeof(AbpExceptionHandlingModule)
)] )]
public class AbpAccountWebModule : AbpModule public class AbpAccountWebModule : AbpModule

@ -1,19 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.Account.Localization; using Volo.Abp.Account.Localization;
using Volo.Abp.AspNetCore.ExceptionHandling; using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Volo.Abp.ExceptionHandling; using Volo.Abp.ExceptionHandling;
using Volo.Abp.Identity; using Volo.Abp.Identity;
using Volo.Abp.MultiTenancy;
using IdentityUser = Volo.Abp.Identity.IdentityUser; using IdentityUser = Volo.Abp.Identity.IdentityUser;
namespace Volo.Abp.Account.Web.Pages.Account 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 IOptions<IdentityOptions> IdentityOptions { get; set; }
public IExceptionToErrorInfoConverter ExceptionToErrorInfoConverter { 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() protected AccountPageModel()
{ {
LocalizationResourceType = typeof(AccountResource); LocalizationResourceType = typeof(AccountResource);
ObjectMapperContext = typeof(AbpAccountWebModule); 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) protected virtual void CheckCurrentTenant(Guid? tenantId)
{ {
if (CurrentTenant.Id != tenantId) if (CurrentTenant.Id != tenantId)

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

@ -35,7 +35,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\identity\src\Volo.Abp.Identity.AspNetCore\Volo.Abp.Identity.AspNetCore.csproj" /> <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.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="..\..\..\..\framework\src\Volo.Abp.AutoMapper\Volo.Abp.AutoMapper.csproj" />
<ProjectReference Include="..\Volo.Abp.Account.HttpApi\Volo.Abp.Account.HttpApi.csproj" /> <ProjectReference Include="..\Volo.Abp.Account.HttpApi\Volo.Abp.Account.HttpApi.csproj" />
</ItemGroup> </ItemGroup>

@ -57,7 +57,8 @@
<abp-row> <abp-row>
<abp-column class="col mx-auto" style="max-width: 440px"> <abp-column class="col mx-auto" style="max-width: 440px">
@if (MultiTenancyOptions.Value.IsEnabled && @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 shadow-sm rounded mb-3">
<div class="card-body px-5"> <div class="card-body px-5">

Loading…
Cancel
Save