diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs index 33932137cb..0ee62f9b65 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs +++ b/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; } - //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; auditLogAction = _auditingHelper.CreateAuditLogAction( auditLog, 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 f59c5b6b3a..ceff8c5029 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 @@ -1,7 +1,9 @@ -using System; +using System; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.Options; using Volo.Abp.Auditing; using Volo.Abp.DependencyInjection; @@ -62,6 +64,18 @@ namespace Volo.Abp.AspNetCore.Auditing return false; } + var endpoint = httpContext.GetEndpoint(); + if (endpoint == null) + { + return false; + } + + var actionDescriptor = endpoint.Metadata.GetMetadata(); + if (actionDescriptor != null && !(actionDescriptor is ControllerActionDescriptor)) + { + return false; + } + if (Options.AlwaysLogOnException && hasError) { return true; diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs index 3c8d5dc4db..49dfef9db8 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs +++ b/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 { [Route("api/audit-test")] - [Audited] public class AuditTestController : AbpController { private readonly AbpAuditingOptions _options; diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs index bc41f3bcde..9837ca3ac2 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs +++ b/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()) { - await myAuditedObject1.DoItAsync(new InputObject { Value1 = "forty-two", Value2 = 42 }).ConfigureAwait(false); - await scope.SaveAsync().ConfigureAwait(false); + await myAuditedObject1.DoItAsync(new InputObject { Value1 = "forty-two", Value2 = 42 }); + await scope.SaveAsync(); } #pragma warning disable 4014 @@ -80,8 +80,8 @@ namespace Volo.Abp.Auditing using (var scope = _auditingManager.BeginScope()) { var repository = ServiceProvider.GetRequiredService>(); - await repository.InsertAsync(new AppEntityWithAudited(Guid.NewGuid(), "test name")).ConfigureAwait(false); - await scope.SaveAsync().ConfigureAwait(false); + await repository.InsertAsync(new AppEntityWithAudited(Guid.NewGuid(), "test name")); + await scope.SaveAsync(); } #pragma warning disable 4014 @@ -95,8 +95,8 @@ namespace Volo.Abp.Auditing using (var scope = _auditingManager.BeginScope()) { var repository = ServiceProvider.GetRequiredService>(); - await repository.InsertAsync(new AppEntityWithAuditedAndPropertyHasDisableAuditing(Guid.NewGuid(), "test name", "test name2")).ConfigureAwait(false); - await scope.SaveAsync().ConfigureAwait(false); + await repository.InsertAsync(new AppEntityWithAuditedAndPropertyHasDisableAuditing(Guid.NewGuid(), "test name", "test name2")); + await scope.SaveAsync(); } #pragma warning disable 4014 @@ -113,8 +113,8 @@ namespace Volo.Abp.Auditing using (var scope = _auditingManager.BeginScope()) { var repository = ServiceProvider.GetRequiredService>(); - await repository.InsertAsync(new AppEntityWithDisableAuditing(Guid.NewGuid(), "test name")).ConfigureAwait(false); - await scope.SaveAsync().ConfigureAwait(false); + await repository.InsertAsync(new AppEntityWithDisableAuditing(Guid.NewGuid(), "test name")); + await scope.SaveAsync(); } #pragma warning disable 4014 @@ -128,8 +128,8 @@ namespace Volo.Abp.Auditing using (var scope = _auditingManager.BeginScope()) { var repository = ServiceProvider.GetRequiredService>(); - await repository.InsertAsync(new AppEntityWithSelector(Guid.NewGuid(), "test name")).ConfigureAwait(false); - await scope.SaveAsync().ConfigureAwait(false); + await repository.InsertAsync(new AppEntityWithSelector(Guid.NewGuid(), "test name")); + await scope.SaveAsync(); } #pragma warning disable 4014 @@ -143,8 +143,8 @@ namespace Volo.Abp.Auditing using (var scope = _auditingManager.BeginScope()) { var repository = ServiceProvider.GetRequiredService>(); - await repository.InsertAsync(new AppEntityWithPropertyHasAudited(Guid.NewGuid(), "test name")).ConfigureAwait(false); - await scope.SaveAsync().ConfigureAwait(false); + await repository.InsertAsync(new AppEntityWithPropertyHasAudited(Guid.NewGuid(), "test name")); + await scope.SaveAsync(); } #pragma warning disable 4014 @@ -158,8 +158,8 @@ namespace Volo.Abp.Auditing using (var scope = _auditingManager.BeginScope()) { var repository = ServiceProvider.GetRequiredService>(); - await repository.InsertAsync(new AppEntityWithDisableAuditingAndPropertyHasAudited(Guid.NewGuid(), "test name", "test name2")).ConfigureAwait(false); - await scope.SaveAsync().ConfigureAwait(false); + await repository.InsertAsync(new AppEntityWithDisableAuditingAndPropertyHasAudited(Guid.NewGuid(), "test name", "test name2")); + await scope.SaveAsync(); } #pragma warning disable 4014 @@ -169,5 +169,20 @@ namespace Volo.Abp.Auditing nameof(AppEntityWithDisableAuditingAndPropertyHasAudited.Name))); #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()); +#pragma warning restore 4014 + } + } } diff --git a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs b/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs index 695d9b233f..31de74efde 100644 --- a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs +++ b/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.SetEmailAsync(user,input.EmailAddress).ConfigureAwait(false); + await UserManager.SetEmailAsync(user,input.EmailAddress); await SetDefaultRolesAsync(user); @@ -38,9 +38,9 @@ namespace Volo.Abp.Account 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() diff --git a/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs b/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs index 48b8da5c38..d916368c39 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountController.cs +++ b/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() { - if (!await _settingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin).ConfigureAwait(false)) + if (!await _settingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin)) { throw new UserFriendlyException(L["LocalLoginDisabledMessage"]); } diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs index 53954fd9e4..7857537acb 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs +++ b/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() { - if (!await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin).ConfigureAwait(false)) + if (!await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin)) { throw new UserFriendlyException(L["LocalLoginDisabledMessage"]); } diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs index eb22de0600..1f65dd821f 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs @@ -39,7 +39,7 @@ namespace Volo.Abp.Identity [Fact] public async Task GetDefaultOnesAsync() { - var roles = await RoleRepository.GetDefaultOnesAsync().ConfigureAwait(false); + var roles = await RoleRepository.GetDefaultOnesAsync(); foreach (var role in roles) {