zeroframework/Services/Identity/ZeroFramework.IdentityServer.API/Tenants/CurrentTenant.cs
2023-12-05 17:22:48 +08:00

36 lines
1.1 KiB
C#

namespace ZeroFramework.IdentityServer.API.Tenants
{
public class CurrentTenant(ICurrentTenantAccessor currentTenantAccessor) : ICurrentTenant
{
private readonly ICurrentTenantAccessor _currentTenantAccessor = currentTenantAccessor;
public virtual bool IsAvailable => Id.HasValue;
public virtual Guid? Id => _currentTenantAccessor.Current?.TenantId;
public string? Name => _currentTenantAccessor.Current?.Name;
public IDisposable Change(Guid? id, string? name = null)
{
var parentScope = _currentTenantAccessor.Current;
_currentTenantAccessor.Current = new TenantInfo(id, name);
return new DisposeAction(() =>
{
_currentTenantAccessor.Current = parentScope;
});
}
public class DisposeAction(Action action) : IDisposable
{
private readonly Action _action = action;
void IDisposable.Dispose()
{
_action();
GC.SuppressFinalize(this);
}
}
}
}