In progress
Some checks failed
Code Coverage / Generate Coverage Report (push) Has been cancelled
Tests / Run Tests (9.0.x) (push) Has been cancelled
Tests / Docker Build Test (push) Has been cancelled
Tests / Test Summary (push) Has been cancelled

This commit is contained in:
Yaojia Wang
2025-11-03 14:00:24 +01:00
parent fe8ad1c1f9
commit 1f66b25f30
74 changed files with 9609 additions and 28 deletions

View File

@@ -0,0 +1,34 @@
using ColaFlow.Modules.Identity.Domain.Aggregates.Tenants;
namespace ColaFlow.Modules.Identity.Infrastructure.Services;
/// <summary>
/// Tenant context interface - provides current request tenant information
/// </summary>
public interface ITenantContext
{
/// <summary>
/// Current tenant ID
/// </summary>
TenantId? TenantId { get; }
/// <summary>
/// Current tenant slug
/// </summary>
string? TenantSlug { get; }
/// <summary>
/// Whether tenant is set
/// </summary>
bool IsSet { get; }
/// <summary>
/// Set current tenant
/// </summary>
void SetTenant(TenantId tenantId, string tenantSlug);
/// <summary>
/// Clear tenant information
/// </summary>
void Clear();
}

View File

@@ -0,0 +1,56 @@
using ColaFlow.Modules.Identity.Domain.Aggregates.Tenants;
using Microsoft.AspNetCore.Http;
namespace ColaFlow.Modules.Identity.Infrastructure.Services;
/// <summary>
/// Tenant context implementation (Scoped lifetime - one instance per request)
/// </summary>
public class TenantContext : ITenantContext
{
private readonly IHttpContextAccessor _httpContextAccessor;
private TenantId? _tenantId;
private string? _tenantSlug;
public TenantContext(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
InitializeFromHttpContext();
}
public TenantId? TenantId => _tenantId;
public string? TenantSlug => _tenantSlug;
public bool IsSet => _tenantId != null;
public void SetTenant(TenantId tenantId, string tenantSlug)
{
_tenantId = tenantId;
_tenantSlug = tenantSlug;
}
public void Clear()
{
_tenantId = null;
_tenantSlug = null;
}
/// <summary>
/// Initialize from HTTP Context (extract tenant info from JWT Claims)
/// </summary>
private void InitializeFromHttpContext()
{
var httpContext = _httpContextAccessor.HttpContext;
if (httpContext?.User?.Identity?.IsAuthenticated == true)
{
// Extract tenant_id from JWT Claims
var tenantIdClaim = httpContext.User.FindFirst("tenant_id");
var tenantSlugClaim = httpContext.User.FindFirst("tenant_slug");
if (tenantIdClaim != null && Guid.TryParse(tenantIdClaim.Value, out var tenantIdGuid))
{
_tenantId = TenantId.Create(tenantIdGuid);
_tenantSlug = tenantSlugClaim?.Value;
}
}
}
}