feature: RBAC + Clinical Audit Logging

This commit is contained in:
voltsrage
2026-06-21 15:46:55 +08:00
parent a43db52813
commit 5af6ab490e
83 changed files with 4281 additions and 70 deletions
@@ -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();
}
}