Browse Source

Fixed the bug that the controller was not logged & Filter the controller in AbpAuditingMiddleware.

Resolve #2819
pull/2880/head
maliming 7 years ago
parent
commit
adeeb0d5a1
  1. 6
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs
  2. 16
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs
  3. 1
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs
  4. 43
      framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs
  5. 6
      modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs
  6. 2
      modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs
  7. 2
      modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs
  8. 2
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs

6
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs

@ -84,12 +84,6 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing
return false; return false;
} }
//TODO: This is partially duplication of AuditHelper.ShouldSaveAudit method. Check why it does not work for controllers
if (!AuditingInterceptorRegistrar.ShouldAuditTypeByDefault(context.Controller.GetType()))
{
return false;
}
auditLog = auditLogScope.Log; auditLog = auditLogScope.Log;
auditLogAction = _auditingHelper.CreateAuditLogAction( auditLogAction = _auditingHelper.CreateAuditLogAction(
auditLog, auditLog,

16
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs

@ -1,7 +1,9 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.Auditing; using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -62,6 +64,18 @@ namespace Volo.Abp.AspNetCore.Auditing
return false; return false;
} }
var endpoint = httpContext.GetEndpoint();
if (endpoint == null)
{
return false;
}
var actionDescriptor = endpoint.Metadata.GetMetadata<ActionDescriptor>();
if (actionDescriptor != null && !(actionDescriptor is ControllerActionDescriptor))
{
return false;
}
if (Options.AlwaysLogOnException && hasError) if (Options.AlwaysLogOnException && hasError)
{ {
return true; return true;

1
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs

@ -5,7 +5,6 @@ using Volo.Abp.Auditing;
namespace Volo.Abp.AspNetCore.Mvc.Auditing namespace Volo.Abp.AspNetCore.Mvc.Auditing
{ {
[Route("api/audit-test")] [Route("api/audit-test")]
[Audited]
public class AuditTestController : AbpController public class AuditTestController : AbpController
{ {
private readonly AbpAuditingOptions _options; private readonly AbpAuditingOptions _options;

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

@ -34,8 +34,8 @@ namespace Volo.Abp.Auditing
using (var scope = _auditingManager.BeginScope()) using (var scope = _auditingManager.BeginScope())
{ {
await myAuditedObject1.DoItAsync(new InputObject { Value1 = "forty-two", Value2 = 42 }).ConfigureAwait(false); await myAuditedObject1.DoItAsync(new InputObject { Value1 = "forty-two", Value2 = 42 });
await scope.SaveAsync().ConfigureAwait(false); await scope.SaveAsync();
} }
#pragma warning disable 4014 #pragma warning disable 4014
@ -80,8 +80,8 @@ namespace Volo.Abp.Auditing
using (var scope = _auditingManager.BeginScope()) using (var scope = _auditingManager.BeginScope())
{ {
var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithAudited, Guid>>(); var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithAudited, Guid>>();
await repository.InsertAsync(new AppEntityWithAudited(Guid.NewGuid(), "test name")).ConfigureAwait(false); await repository.InsertAsync(new AppEntityWithAudited(Guid.NewGuid(), "test name"));
await scope.SaveAsync().ConfigureAwait(false); await scope.SaveAsync();
} }
#pragma warning disable 4014 #pragma warning disable 4014
@ -95,8 +95,8 @@ namespace Volo.Abp.Auditing
using (var scope = _auditingManager.BeginScope()) using (var scope = _auditingManager.BeginScope())
{ {
var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithAuditedAndPropertyHasDisableAuditing, Guid>>(); var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithAuditedAndPropertyHasDisableAuditing, Guid>>();
await repository.InsertAsync(new AppEntityWithAuditedAndPropertyHasDisableAuditing(Guid.NewGuid(), "test name", "test name2")).ConfigureAwait(false); await repository.InsertAsync(new AppEntityWithAuditedAndPropertyHasDisableAuditing(Guid.NewGuid(), "test name", "test name2"));
await scope.SaveAsync().ConfigureAwait(false); await scope.SaveAsync();
} }
#pragma warning disable 4014 #pragma warning disable 4014
@ -113,8 +113,8 @@ namespace Volo.Abp.Auditing
using (var scope = _auditingManager.BeginScope()) using (var scope = _auditingManager.BeginScope())
{ {
var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithDisableAuditing, Guid>>(); var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithDisableAuditing, Guid>>();
await repository.InsertAsync(new AppEntityWithDisableAuditing(Guid.NewGuid(), "test name")).ConfigureAwait(false); await repository.InsertAsync(new AppEntityWithDisableAuditing(Guid.NewGuid(), "test name"));
await scope.SaveAsync().ConfigureAwait(false); await scope.SaveAsync();
} }
#pragma warning disable 4014 #pragma warning disable 4014
@ -128,8 +128,8 @@ namespace Volo.Abp.Auditing
using (var scope = _auditingManager.BeginScope()) using (var scope = _auditingManager.BeginScope())
{ {
var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithSelector, Guid>>(); var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithSelector, Guid>>();
await repository.InsertAsync(new AppEntityWithSelector(Guid.NewGuid(), "test name")).ConfigureAwait(false); await repository.InsertAsync(new AppEntityWithSelector(Guid.NewGuid(), "test name"));
await scope.SaveAsync().ConfigureAwait(false); await scope.SaveAsync();
} }
#pragma warning disable 4014 #pragma warning disable 4014
@ -143,8 +143,8 @@ namespace Volo.Abp.Auditing
using (var scope = _auditingManager.BeginScope()) using (var scope = _auditingManager.BeginScope())
{ {
var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithPropertyHasAudited, Guid>>(); var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithPropertyHasAudited, Guid>>();
await repository.InsertAsync(new AppEntityWithPropertyHasAudited(Guid.NewGuid(), "test name")).ConfigureAwait(false); await repository.InsertAsync(new AppEntityWithPropertyHasAudited(Guid.NewGuid(), "test name"));
await scope.SaveAsync().ConfigureAwait(false); await scope.SaveAsync();
} }
#pragma warning disable 4014 #pragma warning disable 4014
@ -158,8 +158,8 @@ namespace Volo.Abp.Auditing
using (var scope = _auditingManager.BeginScope()) using (var scope = _auditingManager.BeginScope())
{ {
var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithDisableAuditingAndPropertyHasAudited, Guid>>(); var repository = ServiceProvider.GetRequiredService<IBasicRepository<AppEntityWithDisableAuditingAndPropertyHasAudited, Guid>>();
await repository.InsertAsync(new AppEntityWithDisableAuditingAndPropertyHasAudited(Guid.NewGuid(), "test name", "test name2")).ConfigureAwait(false); await repository.InsertAsync(new AppEntityWithDisableAuditingAndPropertyHasAudited(Guid.NewGuid(), "test name", "test name2"));
await scope.SaveAsync().ConfigureAwait(false); await scope.SaveAsync();
} }
#pragma warning disable 4014 #pragma warning disable 4014
@ -169,5 +169,20 @@ namespace Volo.Abp.Auditing
nameof(AppEntityWithDisableAuditingAndPropertyHasAudited.Name))); nameof(AppEntityWithDisableAuditingAndPropertyHasAudited.Name)));
#pragma warning restore 4014 #pragma warning restore 4014
} }
[Fact]
public virtual async Task Should_Not_Write_AuditLog_If_There_No_Action_And_No_EntityChanges()
{
using (var scope = _auditingManager.BeginScope())
{
await scope.SaveAsync();
}
#pragma warning disable 4014
_auditingStore.DidNotReceive().SaveAsync(Arg.Any<AuditLogInfo>());
#pragma warning restore 4014
}
} }
} }

6
modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs

@ -29,7 +29,7 @@ namespace Volo.Abp.Account
(await UserManager.CreateAsync(user, input.Password)).CheckErrors(); (await UserManager.CreateAsync(user, input.Password)).CheckErrors();
await UserManager.SetEmailAsync(user,input.EmailAddress).ConfigureAwait(false); await UserManager.SetEmailAsync(user,input.EmailAddress);
await SetDefaultRolesAsync(user); await SetDefaultRolesAsync(user);
@ -38,9 +38,9 @@ namespace Volo.Abp.Account
protected virtual async Task SetDefaultRolesAsync(IdentityUser user) protected virtual async Task SetDefaultRolesAsync(IdentityUser user)
{ {
var defaultRoles = await _roleRepository.GetDefaultOnesAsync().ConfigureAwait(false); var defaultRoles = await _roleRepository.GetDefaultOnesAsync();
await UserManager.SetRolesAsync(user, defaultRoles.Select(r => r.Name)).ConfigureAwait(false); await UserManager.SetRolesAsync(user, defaultRoles.Select(r => r.Name));
} }
protected virtual async Task CheckSelfRegistrationAsync() protected virtual async Task CheckSelfRegistrationAsync()

2
modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs

@ -145,7 +145,7 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers
private async Task CheckLocalLoginAsync() private async Task CheckLocalLoginAsync()
{ {
if (!await _settingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin).ConfigureAwait(false)) if (!await _settingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin))
{ {
throw new UserFriendlyException(L["LocalLoginDisabledMessage"]); throw new UserFriendlyException(L["LocalLoginDisabledMessage"]);
} }

2
modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs

@ -240,7 +240,7 @@ namespace Volo.Abp.Account.Web.Pages.Account
protected virtual async Task CheckLocalLoginAsync() protected virtual async Task CheckLocalLoginAsync()
{ {
if (!await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin).ConfigureAwait(false)) if (!await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin))
{ {
throw new UserFriendlyException(L["LocalLoginDisabledMessage"]); throw new UserFriendlyException(L["LocalLoginDisabledMessage"]);
} }

2
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs

@ -39,7 +39,7 @@ namespace Volo.Abp.Identity
[Fact] [Fact]
public async Task GetDefaultOnesAsync() public async Task GetDefaultOnesAsync()
{ {
var roles = await RoleRepository.GetDefaultOnesAsync().ConfigureAwait(false); var roles = await RoleRepository.GetDefaultOnesAsync();
foreach (var role in roles) foreach (var role in roles)
{ {

Loading…
Cancel
Save