Skip to content

Commit

Permalink
Merge pull request #9539 from abpframework/auto-merge/rel-4-4/475
Browse files Browse the repository at this point in the history
Merge branch dev with rel-4.4
  • Loading branch information
maliming committed Jul 8, 2021
2 parents ebea052 + 9044bc0 commit 248da43
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,14 +18,17 @@ public class MultiTenancyMiddleware : IMiddleware, ITransientDependency
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;
}

Expand All @@ -49,6 +49,12 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Volo.Abp.MultiTenancy;

Expand All @@ -12,9 +13,23 @@ public class QueryStringTenantResolveContributor : HttpTenantResolveContributorB

protected override Task<string> GetTenantIdOrNameFromHttpContextOrNullAsync(ITenantResolveContext context, HttpContext httpContext)
{
return Task.FromResult(httpContext.Request.QueryString.HasValue
? httpContext.Request.Query[context.GetAbpAspNetCoreMultiTenancyOptions().TenantKey].ToString()
: null);
if (httpContext.Request.QueryString.HasValue)
{
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ public async Task OnGetAsync()

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)
Expand All @@ -58,17 +55,10 @@ public async Task OnPostAsync()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class ResetPasswordDto
{
public Guid UserId { get; set; }

public Guid? TenantId { get; set; }

[Required]
public string ResetToken { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,16 @@ public virtual async Task SendPasswordResetCodeAsync(SendPasswordResetCodeDto in

public virtual async Task ResetPasswordAsync(ResetPasswordDto input)
{
using (CurrentTenant.Change(input.TenantId))
{
await IdentityOptions.SetAsync();
await IdentityOptions.SetAsync();

var user = await UserManager.GetByIdAsync(input.UserId);
(await UserManager.ResetPasswordAsync(user, input.ResetToken, input.Password)).CheckErrors();
var user = await UserManager.GetByIdAsync(input.UserId);
(await UserManager.ResetPasswordAsync(user, input.ResetToken, input.Password)).CheckErrors();

await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext
{
Identity = IdentitySecurityLogIdentityConsts.Identity,
Action = IdentitySecurityLogActionConsts.ChangePassword
});
}
await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext
{
Identity = IdentitySecurityLogIdentityConsts.Identity,
Action = IdentitySecurityLogActionConsts.ChangePassword
});
}

protected virtual async Task<IdentityUser> GetUserByEmail(string email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public class AccountEmailer : IAccountEmailer, ITransientDependency

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())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand All @@ -22,7 +21,6 @@ namespace Volo.Abp.Account.Web
typeof(AbpIdentityAspNetCoreModule),
typeof(AbpAutoMapperModule),
typeof(AbpAspNetCoreMvcUiThemeSharedModule),
typeof(AbpAspNetCoreMultiTenancyModule),
typeof(AbpExceptionHandlingModule)
)]
public class AbpAccountWebModule : AbpModule
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,50 +21,12 @@ public abstract class AccountPageModel : AbpPageModel
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,13 +9,8 @@
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)]
Expand Down Expand Up @@ -51,11 +45,6 @@ public class ResetPasswordModel : AccountPageModel

public virtual Task<IActionResult> OnGetAsync()
{
if (SwitchTenant(TenantId))
{
return Task.FromResult<IActionResult>(Redirect(HttpContext.Request.GetEncodedUrl()));
}

return Task.FromResult<IActionResult>(Page());
}

Expand All @@ -70,8 +59,7 @@ public virtual async Task<IActionResult> OnPostAsync()
{
UserId = UserId,
ResetToken = ResetToken,
Password = Password,
TenantId = TenantId
Password = Password
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit 248da43

Please sign in to comment.