-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from woksin-org/tenancy-context
Tenancy context
- Loading branch information
Showing
9 changed files
with
230 additions
and
24 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Packages/DotNET/Tenancy/Source/AspNetCore/HttpContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) woksin-org. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.AspNetCore.Http; | ||
using Woksin.Extensions.Tenancy.Context; | ||
|
||
namespace Woksin.Extensions.Tenancy.AspNetCore; | ||
|
||
/// <summary> | ||
/// Extension methods for getting <see cref="ITenantContext"/> from the <see cref="HttpContext"/>. | ||
/// </summary> | ||
public static class HttpContextExtensions | ||
{ | ||
public static ITenantContext<TTenant> GetTenantContext<TTenant>(this HttpContext context) | ||
where TTenant : class, ITenantInfo, new() | ||
{ | ||
ITenantContext<TTenant> result = TenantContext<TTenant>.Unresolved(); | ||
if (context.Items.TryGetValue(TenancyMiddleware.TenantContextItemKey, out var tenantContext) && tenantContext is not null) | ||
{ | ||
result = tenantContext as ITenantContext<TTenant> ?? throw new InvalidCastException($"Could not cast tenant context of type {tenantContext.GetType()} to {typeof(ITenantContext<TTenant>)}"); | ||
} | ||
return result; | ||
} | ||
|
||
public static ITenantContext GetTenantContext(this HttpContext context) | ||
{ | ||
ITenantContext result = TenantContext.Unresolved(); | ||
if (context.Items.TryGetValue(TenancyMiddleware.TenantContextItemKey, out var tenantContext) && tenantContext is not null) | ||
{ | ||
result = tenantContext as ITenantContext ?? throw new InvalidCastException($"Could not cast tenant context of type {tenantContext.GetType()} to {typeof(ITenantContext)}"); | ||
} | ||
return result; | ||
} | ||
|
||
public static ITenantContext GetTenantContext(this HttpRequest request) => request.HttpContext.GetTenantContext(); | ||
public static ITenantContext<TTenant> GetTenantContext<TTenant>(this HttpRequest request) | ||
where TTenant : class, ITenantInfo, new() => request.HttpContext.GetTenantContext<TTenant>(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Packages/DotNET/Tenancy/Source/Base/Context/StaticTenantContextAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) woksin-org. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Woksin.Extensions.Tenancy.Context; | ||
|
||
/// <summary> | ||
/// Represents static implementation of <see cref="ITenantContextAccessor{TTenant}"/> and <see cref="ITenantContextAccessor"/> that will be in use if AsyncLocal tenant context is disabled. | ||
/// </summary> | ||
/// <remarks>Unless <see cref="TenancyOptions{TTenant}.StaticTenantId"/> is configured this implementation will always return the 'unresolved' <see cref="ITenantContext"/>.</remarks> | ||
/// <typeparam name="TTenant">The <see cref="Type"/> of the <see cref="ITenantInfo"/>.</typeparam> | ||
public partial class StaticTenantContextAccessor<TTenant> : ITenantContextAccessor<TTenant>, ITenantContextAccessor | ||
where TTenant : class, ITenantInfo, new() | ||
{ | ||
readonly IOptionsMonitor<TenancyOptions<TTenant>> _options; | ||
readonly ILogger<StaticTenantContextAccessor<TTenant>> _logger; | ||
|
||
public StaticTenantContextAccessor(IOptionsMonitor<TenancyOptions<TTenant>> options, ILogger<StaticTenantContextAccessor<TTenant>> logger) | ||
{ | ||
_options = options; | ||
_logger = logger; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public ITenantContext<TTenant> CurrentTenant | ||
{ | ||
get | ||
{ | ||
if (_options.CurrentValue.IsUsingStaticTenant(out var staticTenantId)) | ||
{ | ||
return TenantContext<TTenant>.Static(new TTenant | ||
{ | ||
Id = staticTenantId | ||
}); | ||
} | ||
LogReturningUnresolvedContext(_logger); | ||
return TenantContext<TTenant>.Unresolved(); | ||
} | ||
|
||
// ReSharper disable once ValueParameterNotUsed | ||
set | ||
{ | ||
LogCannotSetContext(_logger); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
ITenantContext ITenantContextAccessor.CurrentTenant | ||
{ | ||
get => CurrentTenant as ITenantContext ?? TenantContext<TTenant>.Unresolved(); | ||
set => CurrentTenant = value as ITenantContext<TTenant> ?? throw new ArgumentNullException(nameof(value)); | ||
} | ||
|
||
[LoggerMessage(LogLevel.Warning, "When tenant context is disabled it is not possible to set the tenant context")] | ||
static partial void LogCannotSetContext(ILogger logger); | ||
|
||
[LoggerMessage(LogLevel.Warning, "When tenant context is disabled and static tenant is not configured the tenant context will always be unresolved")] | ||
static partial void LogReturningUnresolvedContext(ILogger logger); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters