Commit ee08ac4f authored by Eric Dieckman's avatar Eric Dieckman
Browse files

Add auditlog

parent 1995e802
Loading
Loading
Loading
Loading
+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;
@@ -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,
@@ -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();

@@ -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;
+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!;

@@ -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; }

+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
{
}