Skip to content

Commit

Permalink
Fixing EF audit helper to honor the global Configuration.AuditDisabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
thepirat000 committed Jun 24, 2024
1 parent bc76bff commit fe76fa3
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to Audit.NET and its extensions will be documented in this f

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [25.0.6] - 2024-06-24:
- Audit.EntityFramework: Fixing EF audit helper to bypass the audit data collecting when the global Audit.Core.Configuration.AuditDisabled is set to true (#672)
- Audit.EntityFramework: Upgrading EntityFramework package reference from 6.4.4 to 6.5.0

## [25.0.5] - 2024-06-18:
- Audit.NET.SqlServer: Fixing "Schema" configuration in the Fluent Api for SqlServer (#670)

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>25.0.5</Version>
<Version>25.0.6</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.EntityFramework/Audit.EntityFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="EntityFramework" Version="6.5.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
Expand Down
8 changes: 4 additions & 4 deletions src/Audit.EntityFramework/DbContextHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public async Task<EntityFrameworkEvent> SaveChangesGetAuditAsync(IAuditDbContext

private async Task<EntityFrameworkEvent> SaveChangesGetAuditAsyncImpl(IAuditDbContext context, Func<Task<int>> baseSaveChanges, CancellationToken cancellationToken = default)
{
if (context.AuditDisabled)
if (context.AuditDisabled || Core.Configuration.AuditDisabled)
{
return new EntityFrameworkEvent() { Result = await baseSaveChanges() };
}
Expand Down Expand Up @@ -498,7 +498,7 @@ private async Task<EntityFrameworkEvent> SaveChangesGetAuditAsyncImpl(IAuditDbCo

private EntityFrameworkEvent SaveChangesGetAuditImpl(IAuditDbContext context, Func<int> baseSaveChanges)
{
if (context.AuditDisabled)
if (context.AuditDisabled || Core.Configuration.AuditDisabled)
{
return new EntityFrameworkEvent() { Result = baseSaveChanges() };
}
Expand Down Expand Up @@ -530,7 +530,7 @@ private EntityFrameworkEvent SaveChangesGetAuditImpl(IAuditDbContext context, Fu

public IAuditScope BeginSaveChanges(IAuditDbContext context)
{
if (context.AuditDisabled)
if (context.AuditDisabled || Core.Configuration.AuditDisabled)
{
return null;
}
Expand All @@ -549,7 +549,7 @@ public IAuditScope BeginSaveChanges(IAuditDbContext context)

public async Task<IAuditScope> BeginSaveChangesAsync(IAuditDbContext context, CancellationToken cancellationToken = default)
{
if (context.AuditDisabled)
if (context.AuditDisabled || Core.Configuration.AuditDisabled)
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.EntityFramework/IAuditDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IAuditDbContext
/// </summary>
string AuditEventType { get; set; }
/// <summary>
/// Indicates if the Audit is disabled.
/// Indicates if the Audit is disabled for this DbContext instance.
/// Default is false.
/// </summary>
bool AuditDisabled { get; set; }
Expand Down
12 changes: 6 additions & 6 deletions src/Audit.EntityFramework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ The following table shows the entity framework package version used for each .NE

<sub>Target</sub> \ <sup>Library</sup> | `Audit.EntityFramework` / `Audit.EntityFramework.Identity` | `Audit.EntityFramework.Core` / `Audit.EntityFramework.Identity.Core` |
------------ | ---------------- | -------------- |
**.NET 4.6.2** | EntityFramework 6.4.4 | N/C |
**.NET 4.7.2** | EntityFramework 6.4.4 | N/C |
**.NET Standard 2.1** | EntityFramework 6.4.4 | Microsoft.EntityFrameworkCore 5.0.17 |
**.NET 6.0** | EntityFramework 6.4.4 | Microsoft.EntityFrameworkCore 6.0.25 |
**.NET 7.0** | EntityFramework 6.4.4 | Microsoft.EntityFrameworkCore 7.0.14 |
**.NET 8.0** | EntityFramework 6.4.4 | Microsoft.EntityFrameworkCore 8.0.0 |
**.NET 4.6.2** | EntityFramework 6.5.0 | N/C |
**.NET 4.7.2** | EntityFramework 6.5.0 | N/C |
**.NET Standard 2.1** | EntityFramework 6.5.0 | Microsoft.EntityFrameworkCore 5.0.17 |
**.NET 6.0** | EntityFramework 6.5.0 | Microsoft.EntityFrameworkCore 6.0.25 |
**.NET 7.0** | EntityFramework 6.5.0 | Microsoft.EntityFrameworkCore 7.0.14 |
**.NET 8.0** | EntityFramework 6.5.0 | Microsoft.EntityFrameworkCore 8.0.0 |

> N/C: Not Compatible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Setup()
.ForAnyContext().Reset();
Audit.EntityFramework.Configuration.Setup()
.ForContext<DbCommandInterceptContext>().Reset();
Audit.Core.Configuration.ResetCustomActions();
Audit.Core.Configuration.Reset();
Audit.Core.Configuration.CreationPolicy = EventCreationPolicy.InsertOnEnd;
}

Expand Down Expand Up @@ -568,6 +568,43 @@ public void Test_DbCommandInterceptor_AuditDisabledFromAuditDbContext()
Assert.That(replaced.Count, Is.EqualTo(0));
}

[Test]
public void Test_DbCommandInterceptor_AuditDisabledFromGlobalConfig()
{
var inserted = new List<AuditEventCommandEntityFramework>();
var replaced = new List<AuditEventCommandEntityFramework>();

Audit.Core.Configuration.Setup()
.UseDynamicProvider(d => d
.OnInsert(ev => inserted.Add(AuditEvent.FromJson<AuditEventCommandEntityFramework>(ev.ToJson())))
.OnReplace((eventId, ev) =>
replaced.Add(AuditEvent.FromJson<AuditEventCommandEntityFramework>(ev.ToJson()))));

int id = _rnd.Next();

Audit.Core.Configuration.AuditDisabled = true;

var optionsWithInterceptor = new DbContextOptionsBuilder()
.AddInterceptors(new AuditCommandInterceptor())
.Options;
using (var ctx = new DbCommandInterceptContext_InheritingFromAuditDbContext(
opt: optionsWithInterceptor,
dataProvider: null,
auditDisabled: false,
eventType: null,
customFieldValue: null))
{
//NonQueryExecuting
var result = ctx.Database.ExecuteSqlRaw("INSERT INTO DEPARTMENTS (Id, Name, Comments) VALUES (" + id + ", 'test', {0})", "comments");
Assert.That(result, Is.EqualTo(1));
}

Audit.Core.Configuration.AuditDisabled = false;

Assert.That(inserted.Count, Is.EqualTo(0));
Assert.That(replaced.Count, Is.EqualTo(0));
}

[Test]
public async Task Test_DbCommandInterceptor_AuditDisabledFromAuditDbContextAsync()
{
Expand Down Expand Up @@ -601,6 +638,43 @@ public async Task Test_DbCommandInterceptor_AuditDisabledFromAuditDbContextAsync
Assert.That(replaced.Count, Is.EqualTo(0));
}

[Test]
public async Task Test_DbCommandInterceptor_AuditDisabledFromGlobalConfigAsync()
{
var inserted = new List<AuditEventCommandEntityFramework>();
var replaced = new List<AuditEventCommandEntityFramework>();

Audit.Core.Configuration.Setup()
.UseDynamicProvider(d => d
.OnInsert(ev => inserted.Add(AuditEvent.FromJson<AuditEventCommandEntityFramework>(ev.ToJson())))
.OnReplace((eventId, ev) =>
replaced.Add(AuditEvent.FromJson<AuditEventCommandEntityFramework>(ev.ToJson()))));

int id = _rnd.Next();

Audit.Core.Configuration.AuditDisabled = true;

var optionsWithInterceptor = new DbContextOptionsBuilder()
.AddInterceptors(new AuditCommandInterceptor())
.Options;
using (var ctx = new DbCommandInterceptContext_InheritingFromAuditDbContext(
opt: optionsWithInterceptor,
dataProvider: null,
auditDisabled: false,
eventType: null,
customFieldValue: null))
{
//NonQueryExecuting
var result = await ctx.Database.ExecuteSqlRawAsync("INSERT INTO DEPARTMENTS (Id, Name, Comments) VALUES (" + id + ", 'test', {0})", "comments");
Assert.That(result, Is.EqualTo(1));
}

Audit.Core.Configuration.AuditDisabled = false;

Assert.That(inserted.Count, Is.EqualTo(0));
Assert.That(replaced.Count, Is.EqualTo(0));
}

[Test]
public void Test_DbCommandInterceptor_CustomFieldFromAuditDbContext()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="EntityFramework" Version="6.5.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'net472' ">
Expand Down

0 comments on commit fe76fa3

Please sign in to comment.