Files
Nemera/src/Lumina.DataAccess/Common/Interceptors/UpdateAuditableEntitiesInterceptor.cs
T
BinaryAlley 7253f1507c Centralizes audit column ownership in the auditing interceptor
Removes explicit audit assignments from mappings, handlers, repositories,
seeding, and background jobs, leaving the auditing interceptor as the only
writer of the audit columns for ordinary saves.

Adds a one-time initialization save so setup can attribute the original admin
account and seeded rows to the SYSTEM audit identity, while ordinary anonymous
writes remain unattributed with Guid.Empty.
2026-11-21 12:00:00 +02:00

116 lines
5.5 KiB
C#

#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Common;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Time;
using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.Common.Interceptors;
/// <summary>
/// Interceptor for updating audit fields automatically for entities implementing <see cref="IAuditableEntity"/>.
/// </summary>
public sealed class UpdateAuditableEntitiesInterceptor : SaveChangesInterceptor
{
private readonly ICurrentUserService _currentUserService;
private readonly IDateTimeProvider _dateTimeProvider;
/// <summary>
/// Initializes a new instance of the <see cref="UpdateAuditableEntitiesInterceptor"/> class.
/// </summary>
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
/// <param name="dateTimeProvider">Injected service for time related functionality.</param>
public UpdateAuditableEntitiesInterceptor(ICurrentUserService currentUserService, IDateTimeProvider dateTimeProvider)
{
_currentUserService = currentUserService;
_dateTimeProvider = dateTimeProvider;
}
/// <summary>
/// Intercepts the synchronous SaveChanges operation to update audit fields.
/// </summary>
/// <param name="eventData">The event data containing the <see cref="DbContext"/>.</param>
/// <param name="result">The result of the SaveChanges operation.</param>
/// <returns>The interception result.</returns>
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
{
UpdateAuditableEntities(eventData.Context);
return base.SavingChanges(eventData, result);
}
/// <summary>
/// Intercepts the asynchronous SaveChangesAsync operation to update audit fields.
/// </summary>
/// <param name="eventData">The event data containing the <see cref="DbContext"/>.</param>
/// <param name="result">The result of the SaveChanges operation.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>A <see cref="ValueTask"/> containing the interception result.</returns>
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
{
UpdateAuditableEntities(eventData.Context);
return base.SavingChangesAsync(eventData, result, cancellationToken);
}
/// <summary>
/// Updates the audit fields for all changed entities that implement IAuditableEntity.
/// </summary>
/// <param name="context">The <see cref="DbContext"/> containing the entities to update.</param>
private void UpdateAuditableEntities(DbContext? context)
{
if (context is null)
return;
// Initialization is the one save whose audit identity is provided explicitly, because no user account exists yet.
if (context is LuminaDbContext luminaDbContext && luminaDbContext.IsInitializationSave)
return;
Guid? userId = _currentUserService.UserId;
DateTime utcNow = _dateTimeProvider.UtcNow;
// Retrieve all entities implementing IAuditableEntity that are being tracked and have either Added or Modified state.
foreach (EntityEntry<IAuditableEntity> entry in context.ChangeTracker.Entries<IAuditableEntity>())
{
if (entry.State == EntityState.Added)
SetCreationProperties(entry.Entity, userId, utcNow);
else if(entry.State == EntityState.Modified)
SetModificationProperties(entry, userId, utcNow);
}
}
/// <summary>
/// Sets the creation audit properties for a newly added entity.
/// </summary>
/// <param name="entity">The entity being created.</param>
/// <param name="userId">The unique identifier of the user creating the entity.</param>
/// <param name="utcNow">The current UTC timestamp.</param>
private static void SetCreationProperties(IAuditableEntity entity, Guid? userId, DateTime utcNow)
{
entity.CreatedOnUtc = utcNow;
entity.CreatedBy = userId ?? default;
entity.UpdatedOnUtc = null;
entity.UpdatedBy = null;
}
/// <summary>
/// Sets the modification audit properties for an updated entity while protecting creation properties.
/// </summary>
/// <param name="entry">The entity entry being modified.</param>
/// <param name="userId">The unique identifier of the user modifying the entity.</param>
/// <param name="utcNow">The current UTC timestamp.</param>
private static void SetModificationProperties(EntityEntry<IAuditableEntity> entry, Guid? userId, DateTime utcNow)
{
// Prevent modification of creation properties.
entry.Property(auditableEntity => auditableEntity.CreatedOnUtc).IsModified = false;
entry.Property(auditableEntity => auditableEntity.CreatedBy).IsModified = false;
// Set modification properties.
entry.Entity.UpdatedOnUtc = utcNow;
entry.Entity.UpdatedBy = userId ?? default;
}
}