Browse Source

Merge pull request #24446 from abpframework/AppEntityWithNavigationsAndDisableAuditing

Skip auditing for navigations with DisableAuditing attribute
pull/24449/head
Engincan VESKE 2 months ago
committed by GitHub
parent
commit
a332f9de1c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 7
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs
  2. 33
      framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithNavigationsAndDisableAuditing.cs
  3. 2
      framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs
  4. 34
      framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs

7
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs

@ -198,6 +198,13 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency
{
foreach (var (navigationEntry, index) in entityEntry.Navigations.Select((value, i) => ( value, i )))
{
var propertyInfo = navigationEntry.Metadata.PropertyInfo;
if (propertyInfo != null &&
propertyInfo.IsDefined(typeof(DisableAuditingAttribute), true))
{
continue;
}
if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index))
{
var abpNavigationEntry = AbpEfCoreNavigationHelper.GetNavigationEntry(entityEntry, index);

33
framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithNavigationsAndDisableAuditing.cs

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using Volo.Abp.Domain.Entities;
namespace Volo.Abp.Auditing.App.Entities;
[Audited]
public class AppEntityWithNavigationsAndDisableAuditing : AggregateRoot<Guid>
{
protected AppEntityWithNavigationsAndDisableAuditing()
{
}
public AppEntityWithNavigationsAndDisableAuditing(Guid id, string name)
: base(id)
{
Name = name;
}
public string Name { get; set; }
[DisableAuditing]
public virtual List<AppEntityWithNavigationsAndDisableAuditingChildOneToMany> OneToMany { get; set; }
}
public class AppEntityWithNavigationsAndDisableAuditingChildOneToMany : Entity<Guid>
{
public Guid AppEntityWithNavigationsAndDisableAuditingId { get; set; }
public string ChildName { get; set; }
}

2
framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs

@ -29,6 +29,8 @@ public class AbpAuditingTestDbContext : AbpDbContext<AbpAuditingTestDbContext>
public DbSet<AppEntityWithNavigations> AppEntityWithNavigations { get; set; }
public DbSet<AppEntityWithNavigationChildOneToMany> AppEntityWithNavigationChildOneToMany { get; set; }
public DbSet<AppEntityWithNavigationsAndDisableAuditing> AppEntityWithNavigationsAndDisableAuditing { get; set; }
public AbpAuditingTestDbContext(DbContextOptions<AbpAuditingTestDbContext> options)
: base(options)
{

34
framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs

@ -1025,6 +1025,40 @@ public class Auditing_SaveEntityHistoryWhenNavigationChanges_Tests : AbpAuditing
x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithNavigationChildManyToMany.ChildName) &&
x.EntityChanges[0].PropertyChanges[0].PropertyTypeFullName == typeof(string).FullName));
#pragma warning restore 4014
}
[Fact]
public virtual async Task Should_Not_Write_AuditLog_For_Navigation_Changes_With_DisableAuditing()
{
using (var scope = _auditingManager.BeginScope())
{
var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithNavigationsAndDisableAuditing, Guid>>();
var entity = new AppEntityWithNavigationsAndDisableAuditing(Guid.NewGuid(), "test name");
entity.OneToMany = new List<AppEntityWithNavigationsAndDisableAuditingChildOneToMany>
{
new AppEntityWithNavigationsAndDisableAuditingChildOneToMany
{
AppEntityWithNavigationsAndDisableAuditingId = entity.Id,
ChildName = "ChildName1"
}
};
await repository.InsertAsync(entity);
await scope.SaveAsync();
}
#pragma warning disable 4014
AuditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>());
#pragma warning restore 4014
#pragma warning disable 4014
AuditingStore.Received().SaveAsync(Arg.Is<AuditLogInfo>(x => x.EntityChanges.Count == 1 &&
x.EntityChanges[0].ChangeType == EntityChangeType.Created &&
x.EntityChanges[0].EntityTypeFullName == typeof(AppEntityWithNavigationsAndDisableAuditing).FullName &&
x.EntityChanges[0].PropertyChanges.Count == 1 &&
x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithNavigationsAndDisableAuditing.Name) &&
x.EntityChanges[0].PropertyChanges[0].PropertyTypeFullName == typeof(string).FullName));
#pragma warning restore 4014
}
}

Loading…
Cancel
Save