feat/Update_Controllers_to_Allow_for_Premium_Membership
This commit is contained in:
@@ -10,11 +10,28 @@ public interface ICurrentUserService
|
||||
public sealed class CurrentUserService : ICurrentUserService
|
||||
{
|
||||
private readonly IHttpContextAccessor _http;
|
||||
private string? _backgroundUserId;
|
||||
|
||||
public CurrentUserService(IHttpContextAccessor http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public string? UserId => LocalAuthIdentity.GetRequiredUserId(_http.HttpContext?.User);
|
||||
public string? UserId => _backgroundUserId ?? LocalAuthIdentity.GetRequiredUserId(_http.HttpContext?.User);
|
||||
|
||||
public IDisposable UseBackgroundUser(string userId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(userId)) throw new ArgumentException("A background owner is required.", nameof(userId));
|
||||
if (_http.HttpContext is not null) throw new InvalidOperationException("Background owner scope cannot replace an HTTP request identity.");
|
||||
|
||||
var previous = _backgroundUserId;
|
||||
_backgroundUserId = userId;
|
||||
return new Reset(() => _backgroundUserId = previous);
|
||||
}
|
||||
|
||||
private sealed class Reset(Action reset) : IDisposable
|
||||
{
|
||||
private Action? _reset = reset;
|
||||
public void Dispose() => Interlocked.Exchange(ref _reset, null)?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user