Browse Source

Add `AlwaysLogSelectors` to `AuditingOptions`

pull/11797/head
maliming 4 years ago
parent
commit
06bee7baec
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 12
      framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs
  2. 12
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs
  3. 4
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs
  4. 13
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs
  5. 10
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs
  6. 9
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs

12
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<IUnitOfWorkManager>();
if (unitOfWorkManager.Current != null)
@ -76,7 +76,7 @@ public class AbpAuditHubFilter : IHubFilter
}
}
private bool ShouldWriteAuditLog(IServiceProvider serviceProvider, bool hasError)
private async Task<bool> ShouldWriteAuditLogAsync(AuditLogInfo auditLogInfo, IServiceProvider serviceProvider, bool hasError)
{
var options = serviceProvider.GetRequiredService<IOptions<AbpAuditingOptions>>().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<ICurrentUser>().IsAuthenticated)
{
return false;

12
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<bool> 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;

4
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
/// </summary>
public bool AlwaysLogOnException { get; set; }
public List<Func<AuditLogInfo, Task<bool>>> AlwaysLogSelectors { get; }
public List<AuditLogContributor> Contributors { get; }
public List<Type> IgnoredTypes { get; }
@ -55,6 +58,7 @@ public class AbpAuditingOptions
IsEnabledForAnonymousUsers = true;
HideErrors = true;
AlwaysLogOnException = true;
AlwaysLogSelectors = new List<Func<AuditLogInfo, Task<bool>>>();
Contributors = new List<AuditLogContributor>();

13
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<bool> 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;

10
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<AuditLogInfo>());
}
[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<AuditLogInfo>());
}
[Fact]
public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always()
{

9
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<AuditLogInfo>());
}
[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<AuditLogInfo>());
}
[Fact]
public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always()
{

Loading…
Cancel
Save