1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-23 17:07:42 +01:00

abstract context building to overrideable SetContextAsync (#766)

* abstract context building to overrideable SetContextAsync

* update method calls
This commit is contained in:
Kyle Spearrin 2020-06-04 14:14:43 -04:00 committed by GitHub
parent d36c403e6b
commit 4bd3e01a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View File

@ -26,7 +26,7 @@ namespace Bit.Core
public virtual Guid? InstallationId { get; set; }
public virtual Guid? OrganizationId { get; set; }
public void Build(HttpContext httpContext, GlobalSettings globalSettings)
public async virtual Task BuildAsync(HttpContext httpContext, GlobalSettings globalSettings)
{
if (_builtHttpContext)
{
@ -35,7 +35,7 @@ namespace Bit.Core
_builtHttpContext = true;
HttpContext = httpContext;
Build(httpContext.User, globalSettings);
await BuildAsync(httpContext.User, globalSettings);
if (DeviceIdentifier == null && httpContext.Request.Headers.ContainsKey("Device-Identifier"))
{
@ -49,7 +49,7 @@ namespace Bit.Core
}
}
public void Build(ClaimsPrincipal user, GlobalSettings globalSettings)
public async virtual Task BuildAsync(ClaimsPrincipal user, GlobalSettings globalSettings)
{
if (_builtClaimsPrincipal)
{
@ -58,9 +58,14 @@ namespace Bit.Core
_builtClaimsPrincipal = true;
IpAddress = HttpContext.GetIpAddress(globalSettings);
await SetContextAsync(user);
}
public virtual Task SetContextAsync(ClaimsPrincipal user)
{
if (user == null || !user.Claims.Any())
{
return;
return Task.FromResult(0);
}
var claimsDict = user.Claims.GroupBy(c => c.Type).ToDictionary(c => c.Key, c => c.Select(v => v));
@ -143,6 +148,7 @@ namespace Bit.Core
Type = OrganizationUserType.Manager
}));
}
return Task.FromResult(0);
}
public bool OrganizationUser(Guid orgId)

View File

@ -14,7 +14,7 @@ namespace Bit.Core.Utilities
public async Task Invoke(HttpContext httpContext, CurrentContext currentContext, GlobalSettings globalSettings)
{
currentContext.Build(httpContext, globalSettings);
await currentContext.BuildAsync(httpContext, globalSettings);
await _next.Invoke(httpContext);
}
}

View File

@ -20,7 +20,7 @@ namespace Bit.Notifications
public override async Task OnConnectedAsync()
{
var currentContext = new CurrentContext();
currentContext.Build(Context.User, _globalSettings);
await currentContext.BuildAsync(Context.User, _globalSettings);
if (currentContext.Organizations != null)
{
foreach (var org in currentContext.Organizations)
@ -35,7 +35,7 @@ namespace Bit.Notifications
public override async Task OnDisconnectedAsync(Exception exception)
{
var currentContext = new CurrentContext();
currentContext.Build(Context.User, _globalSettings);
await currentContext.BuildAsync(Context.User, _globalSettings);
if (currentContext.Organizations != null)
{
foreach (var org in currentContext.Organizations)