Loading src/infrastructure/Contexts/AuditSaveChangesInterceptor.cs +20 −15 Original line number Diff line number Diff line using System.Text.Json; using Cals.Visor.Application; using Cals.Visor.Application.Identity; using Cals.Visor.Models; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; Loading @@ -7,18 +9,20 @@ namespace Cals.Visor.Infrastructure.Contexts; public class AuditSaveChangesInterceptor : SaveChangesInterceptor { private readonly IHttpContextAccessor _httpContextAccessor; private readonly ICurrentUser _currentUser; private readonly ICorrelationIdAccessor _correlationIdAccessor; public AuditSaveChangesInterceptor(IHttpContextAccessor httpContextAccessor) public AuditSaveChangesInterceptor(ICurrentUser currentUser, ICorrelationIdAccessor correlationIdAccessor) { _httpContextAccessor = httpContextAccessor; _currentUser = currentUser; _correlationIdAccessor = correlationIdAccessor; } private string? GetUserId() => _httpContextAccessor.HttpContext?.User?.FindFirst("sub")?.Value; private long? GetUserId() => _currentUser.CurrentUser?.Id; private string? GetCorrelationId() => _httpContextAccessor.HttpContext?.Items["CorrelationId"]?.ToString(); => _correlationIdAccessor.CorrelationId; public override ValueTask<InterceptionResult<int>> SavingChangesAsync( DbContextEventData eventData, Loading @@ -40,6 +44,7 @@ public class AuditSaveChangesInterceptor : SaveChangesInterceptor { var entries = context.ChangeTracker.Entries() .Where(e => e.Entity is not AuditLog && e.Entity is IAuditableEntity && e.State is EntityState.Added or EntityState.Modified or EntityState.Deleted) .ToList(); Loading Loading @@ -87,19 +92,19 @@ public class AuditSaveChangesInterceptor : SaveChangesInterceptor TableName = tableName, EntityId = keyValue, Action = entry.State.ToString().ToUpper(), UserId = userId, AppUserId = userId, CorrelationId = correlationId, TimestampUtc = DateTime.UtcNow, CreatedAt = DateTime.UtcNow, ChangesJson = JsonSerializer.Serialize(changes) }); // Update AuditableEntity fields if (entry.Entity is AuditableEntity auditable && entry.State == EntityState.Modified) { auditable.LastModifiedBy = userId; auditable.LastModifiedUtc = DateTime.UtcNow; } //// Update AuditableEntity fields //if (entry.Entity is AuditableEntity auditable && // entry.State == EntityState.Modified) //{ // auditable.LastModifiedBy = userId; // auditable.LastModifiedUtc = DateTime.UtcNow; //} } return logs; Loading src/models/AuditLog.cs +2 −3 Original line number Diff line number Diff line using System.Text.Json.Serialization; using Cals.Visor.Models.Identity; namespace Cals.Visor.Models; public class AuditLog { public long Id { get; set; } public long? AuditLogId { get; set; } public string TableName { get; set; } = default!; Loading @@ -16,7 +15,7 @@ public class AuditLog public long? AppUserId { get; set; } [JsonIgnore] public virtual AppUser User { get;; set; } public virtual AppUser User { get; set; } public string? CorrelationId { get; set; } Loading src/models/IAuditableEntity.cs 0 → 100644 +8 −0 Original line number Diff line number Diff line namespace Cals.Visor.Models; /// <summary> /// Interface for defining an entity that should be audit logged /// </summary> public interface IAuditableEntity { } Loading
src/infrastructure/Contexts/AuditSaveChangesInterceptor.cs +20 −15 Original line number Diff line number Diff line using System.Text.Json; using Cals.Visor.Application; using Cals.Visor.Application.Identity; using Cals.Visor.Models; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; Loading @@ -7,18 +9,20 @@ namespace Cals.Visor.Infrastructure.Contexts; public class AuditSaveChangesInterceptor : SaveChangesInterceptor { private readonly IHttpContextAccessor _httpContextAccessor; private readonly ICurrentUser _currentUser; private readonly ICorrelationIdAccessor _correlationIdAccessor; public AuditSaveChangesInterceptor(IHttpContextAccessor httpContextAccessor) public AuditSaveChangesInterceptor(ICurrentUser currentUser, ICorrelationIdAccessor correlationIdAccessor) { _httpContextAccessor = httpContextAccessor; _currentUser = currentUser; _correlationIdAccessor = correlationIdAccessor; } private string? GetUserId() => _httpContextAccessor.HttpContext?.User?.FindFirst("sub")?.Value; private long? GetUserId() => _currentUser.CurrentUser?.Id; private string? GetCorrelationId() => _httpContextAccessor.HttpContext?.Items["CorrelationId"]?.ToString(); => _correlationIdAccessor.CorrelationId; public override ValueTask<InterceptionResult<int>> SavingChangesAsync( DbContextEventData eventData, Loading @@ -40,6 +44,7 @@ public class AuditSaveChangesInterceptor : SaveChangesInterceptor { var entries = context.ChangeTracker.Entries() .Where(e => e.Entity is not AuditLog && e.Entity is IAuditableEntity && e.State is EntityState.Added or EntityState.Modified or EntityState.Deleted) .ToList(); Loading Loading @@ -87,19 +92,19 @@ public class AuditSaveChangesInterceptor : SaveChangesInterceptor TableName = tableName, EntityId = keyValue, Action = entry.State.ToString().ToUpper(), UserId = userId, AppUserId = userId, CorrelationId = correlationId, TimestampUtc = DateTime.UtcNow, CreatedAt = DateTime.UtcNow, ChangesJson = JsonSerializer.Serialize(changes) }); // Update AuditableEntity fields if (entry.Entity is AuditableEntity auditable && entry.State == EntityState.Modified) { auditable.LastModifiedBy = userId; auditable.LastModifiedUtc = DateTime.UtcNow; } //// Update AuditableEntity fields //if (entry.Entity is AuditableEntity auditable && // entry.State == EntityState.Modified) //{ // auditable.LastModifiedBy = userId; // auditable.LastModifiedUtc = DateTime.UtcNow; //} } return logs; Loading
src/models/AuditLog.cs +2 −3 Original line number Diff line number Diff line using System.Text.Json.Serialization; using Cals.Visor.Models.Identity; namespace Cals.Visor.Models; public class AuditLog { public long Id { get; set; } public long? AuditLogId { get; set; } public string TableName { get; set; } = default!; Loading @@ -16,7 +15,7 @@ public class AuditLog public long? AppUserId { get; set; } [JsonIgnore] public virtual AppUser User { get;; set; } public virtual AppUser User { get; set; } public string? CorrelationId { get; set; } Loading
src/models/IAuditableEntity.cs 0 → 100644 +8 −0 Original line number Diff line number Diff line namespace Cals.Visor.Models; /// <summary> /// Interface for defining an entity that should be audit logged /// </summary> public interface IAuditableEntity { }