feature: RBAC + Clinical Audit Logging
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using System.Text.Json;
|
||||
|
||||
public class AuditService : IAuditService
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
private readonly IHttpContextAccessor _http;
|
||||
|
||||
public AuditService(
|
||||
AppDbContext db,
|
||||
ICurrentUserService currentUser,
|
||||
IHttpContextAccessor http)
|
||||
{
|
||||
_db = db;
|
||||
_currentUser = currentUser;
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public async Task WriteAsync(
|
||||
AuditAction action,
|
||||
string entityType,
|
||||
Guid entityId,
|
||||
object? previousValue = null,
|
||||
object? newValue = null,
|
||||
string? reason = null)
|
||||
{
|
||||
var correlationId = _http.HttpContext?.Items["CorrelationId"]?.ToString();
|
||||
|
||||
_db.ClinicalAuditLogs.Add(new ClinicalAuditLog
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Action = action,
|
||||
EntityType = entityType,
|
||||
EntityId = entityId,
|
||||
UserId = _currentUser.UserId,
|
||||
UserDisplayName = _currentUser.DisplayName ?? _currentUser.Username,
|
||||
PreviousValueJson = previousValue is null ? null : JsonSerializer.Serialize(previousValue),
|
||||
NewValueJson = newValue is null ? null : JsonSerializer.Serialize(newValue),
|
||||
Reason = reason,
|
||||
IpAddress = _currentUser.IpAddress,
|
||||
CorrelationId = correlationId,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
});
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user