From 06bee7baec35e652610ee9381ff60daaadb619b5 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 4 Mar 2022 10:17:20 +0800 Subject: [PATCH] Add `AlwaysLogSelectors` to `AuditingOptions` --- .../SignalR/Auditing/AbpAuditHubFilter.cs | 12 ++++++++++-- .../AspNetCore/Auditing/AbpAuditingMiddleware.cs | 12 ++++++++++-- .../Volo/Abp/Auditing/AbpAuditingOptions.cs | 4 ++++ .../Volo/Abp/Auditing/AuditingInterceptor.cs | 13 +++++++++++-- .../Mvc/Auditing/AuditTestController_Tests.cs | 10 ++++++++++ .../AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs | 9 +++++++++ 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs index fd5567396b..c29afb171e 100644 --- a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs @@ -50,7 +50,7 @@ public class AbpAuditHubFilter : IHubFilter } finally { - if (ShouldWriteAuditLog(invocationContext.ServiceProvider, hasError)) + if (await ShouldWriteAuditLogAsync(auditingManager.Current.Log, invocationContext.ServiceProvider, hasError)) { var unitOfWorkManager = invocationContext.ServiceProvider.GetRequiredService(); if (unitOfWorkManager.Current != null) @@ -76,7 +76,7 @@ public class AbpAuditHubFilter : IHubFilter } } - private bool ShouldWriteAuditLog(IServiceProvider serviceProvider, bool hasError) + private async Task ShouldWriteAuditLogAsync(AuditLogInfo auditLogInfo, IServiceProvider serviceProvider, bool hasError) { var options = serviceProvider.GetRequiredService>().Value; if (options.AlwaysLogOnException && hasError) @@ -84,6 +84,14 @@ public class AbpAuditHubFilter : IHubFilter return true; } + foreach (var selector in options.AlwaysLogSelectors) + { + if (await selector(auditLogInfo)) + { + return true; + } + } + if (!options.IsEnabledForAnonymousUsers && !serviceProvider.GetRequiredService().IsAuthenticated) { return false; diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs index 4ff49012cf..9966542cc6 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs @@ -69,7 +69,7 @@ public class AbpAuditingMiddleware : IMiddleware, ITransientDependency } finally { - if (ShouldWriteAuditLog(context, hasError)) + if (await ShouldWriteAuditLogAsync(_auditingManager.Current.Log, context, hasError)) { if (UnitOfWorkManager.Current != null) { @@ -98,13 +98,21 @@ public class AbpAuditingMiddleware : IMiddleware, ITransientDependency AspNetCoreAuditingOptions.IgnoredUrls.Any(x => context.Request.Path.Value.StartsWith(x)); } - private bool ShouldWriteAuditLog(HttpContext httpContext, bool hasError) + private async Task ShouldWriteAuditLogAsync(AuditLogInfo auditLogInfo, HttpContext httpContext, bool hasError) { if (AuditingOptions.AlwaysLogOnException && hasError) { return true; } + foreach (var selector in AuditingOptions.AlwaysLogSelectors) + { + if (await selector(auditLogInfo)) + { + return true; + } + } + if (!AuditingOptions.IsEnabledForAnonymousUsers && !CurrentUser.IsAuthenticated) { return false; diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs index b7c5150376..b3c6aeb051 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq.Expressions; +using System.Threading.Tasks; namespace Volo.Abp.Auditing; @@ -37,6 +38,8 @@ public class AbpAuditingOptions /// public bool AlwaysLogOnException { get; set; } + public List>> AlwaysLogSelectors { get; } + public List Contributors { get; } public List IgnoredTypes { get; } @@ -55,6 +58,7 @@ public class AbpAuditingOptions IsEnabledForAnonymousUsers = true; HideErrors = true; AlwaysLogOnException = true; + AlwaysLogSelectors = new List>>(); Contributors = new List(); diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs index 5521c28d1e..bba7fe11dd 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs @@ -130,7 +130,7 @@ public class AuditingInterceptor : AbpInterceptor, ITransientDependency } finally { - if (ShouldWriteAuditLog(invocation, options, currentUser, hasError)) + if (await ShouldWriteAuditLogAsync(invocation, auditingManager.Current.Log, options, currentUser, hasError)) { if (unitOfWorkManager.Current != null) { @@ -153,8 +153,9 @@ public class AuditingInterceptor : AbpInterceptor, ITransientDependency } } - private bool ShouldWriteAuditLog( + private async Task ShouldWriteAuditLogAsync( IAbpMethodInvocation invocation, + AuditLogInfo auditLogInfo, AbpAuditingOptions options, ICurrentUser currentUser, bool hasError) @@ -164,6 +165,14 @@ public class AuditingInterceptor : AbpInterceptor, ITransientDependency return true; } + foreach (var selector in options.AlwaysLogSelectors) + { + if (await selector(auditLogInfo)) + { + return true; + } + } + if (!options.IsEnabledForAnonymousUsers && !currentUser.IsAuthenticated) { return false; diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs index e038239fe0..d36b5a5723 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs @@ -49,6 +49,16 @@ public class AuditTestController_Tests : AspNetCoreMvcTestBase await _auditingStore.Received().SaveAsync(Arg.Any()); } + + [Fact] + public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_Specified_Requests() + { + _options.AlwaysLogOnException = false; + _options.AlwaysLogSelectors.Add(info => Task.FromResult(info.Url.Contains("api/audit-test/audit-success"))); + await GetResponseAsync("api/audit-test/audit-success"); + await _auditingStore.Received().SaveAsync(Arg.Any()); + } + [Fact] public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() { diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs index f4a48fce65..0099202cd2 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs @@ -48,6 +48,15 @@ public class AuditTestPage_Tests : AspNetCoreMvcTestBase await _auditingStore.Received().SaveAsync(Arg.Any()); } + [Fact] + public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_Specified_Requests() + { + _options.AlwaysLogOnException = false; + _options.AlwaysLogSelectors.Add(info => Task.FromResult(info.Url.Contains("api/audit-test/audit-success"))); + await GetResponseAsync("api/audit-test/audit-success"); + await _auditingStore.Received().SaveAsync(Arg.Any()); + } + [Fact] public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() {