From 58d9f3fb5cabbc2fad36f5c00d93d282b5cffc33 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Fri, 10 Jan 2020 12:25:19 +0300 Subject: [PATCH 01/22] Updated AuditingMiddleware, updated AuditingManager --- .../Auditing/AbpAuditingMiddleware.cs | 13 ++++- .../Volo/Abp/Auditing/AbpAuditingOptions.cs | 8 +++- .../Volo/Abp/Auditing/AuditingManager.cs | 23 +++++---- .../Volo/Abp/Auditing/IAuditLogSaveHandle.cs | 2 +- .../Mvc/Auditing/AuditTestController.cs | 29 ++++++++++++ .../Mvc/Auditing/AuditTestController_Tests.cs | 47 +++++++++++++++++++ 6 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs create mode 100644 framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs 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 d56c29709a..b651274ed4 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 @@ -39,6 +39,10 @@ namespace Volo.Abp.AspNetCore.Auditing try { await next(context).ConfigureAwait(false); + } + catch (Exception ex) + { + await scope.SaveAsync(ex).ConfigureAwait(false); } finally { @@ -49,6 +53,13 @@ namespace Volo.Abp.AspNetCore.Auditing private bool ShouldWriteAuditLog(HttpContext httpContext) { + // IF selected, save audit logs on exception on GET requests even if audit log is disabled for GET requests. + if (Options.AlwaysLogOnException && + string.Equals(httpContext.Request.Method, HttpMethods.Get, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + if (!Options.IsEnabled) { return false; @@ -59,7 +70,7 @@ namespace Volo.Abp.AspNetCore.Auditing return false; } - if (!Options.IsEnabledForGetRequests && + if (!Options.IsEnabledForGetRequests && string.Equals(httpContext.Request.Method, HttpMethods.Get, StringComparison.OrdinalIgnoreCase)) { 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 da406fa490..5dcae1e097 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs @@ -29,7 +29,13 @@ namespace Volo.Abp.Auditing /// /// Default: true. /// - public bool IsEnabledForAnonymousUsers { get; set; } + public bool IsEnabledForAnonymousUsers { get; set; } = false; + + /// + /// Audit log e xceptions. + /// Default: false. + /// + public bool AlwaysLogOnException { get; set; } public List Contributors { get; } diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs index 25738843c0..1d3b912311 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs @@ -23,8 +23,8 @@ namespace Volo.Abp.Auditing private readonly IAuditingStore _auditingStore; public AuditingManager( - IAmbientScopeProvider ambientScopeProvider, - IAuditingHelper auditingHelper, + IAmbientScopeProvider ambientScopeProvider, + IAuditingHelper auditingHelper, IAuditingStore auditingStore, IServiceProvider serviceProvider, IOptions options) @@ -84,7 +84,7 @@ namespace Volo.Abp.Auditing { var changeGroups = auditLog.EntityChanges .Where(e => e.ChangeType == EntityChangeType.Updated) - .GroupBy(e => new {e.EntityTypeFullName, e.EntityId}) + .GroupBy(e => new { e.EntityTypeFullName, e.EntityId }) .ToList(); foreach (var changeGroup in changeGroups) @@ -141,7 +141,7 @@ namespace Volo.Abp.Auditing public DisposableSaveHandle( AuditingManager auditingManager, IDisposable scope, - AuditLogInfo auditLog, + AuditLogInfo auditLog, Stopwatch stopWatch) { _auditingManager = auditingManager; @@ -150,15 +150,22 @@ namespace Volo.Abp.Auditing StopWatch = stopWatch; } - public async Task SaveAsync() + public async Task SaveAsync(Exception exception = null) { - await _auditingManager.SaveAsync(this).ConfigureAwait(false); + if (exception != null) + { + this.AuditLog.Exceptions.Add(exception); + } + else + { + await _auditingManager.SaveAsync(this).ConfigureAwait(false); + } } - + public void Dispose() { _scope.Dispose(); - } + } } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs index 4709b745d2..6e48f3525c 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs @@ -5,6 +5,6 @@ namespace Volo.Abp.Auditing { public interface IAuditLogSaveHandle : IDisposable { - Task SaveAsync(); + Task SaveAsync(Exception exception = null); } } \ No newline at end of file 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 new file mode 100644 index 0000000000..967f3269d2 --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs @@ -0,0 +1,29 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using Volo.Abp.Auditing; + +namespace Volo.Abp.AspNetCore.Mvc.Auditing +{ + [Route("api/audit-test")] + public class AuditTestController : AbpController + { + private readonly AbpAuditingOptions _options; + + public AuditTestController(IOptions options) + { + _options = options.Value; + } + + [Route("audit-success")] + public IActionResult AuditSuccessForGetRequests() + { + return Ok(); + } + + [Route("audit-fail")] + public IActionResult AuditFailForGetRequests() + { + throw new UserFriendlyException("Exception occurred!"); + } + } +} 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 new file mode 100644 index 0000000000..97b211ba5a --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs @@ -0,0 +1,47 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using NSubstitute; +using System.Threading.Tasks; +using Volo.Abp.Auditing; +using Xunit; + +namespace Volo.Abp.AspNetCore.Mvc.Auditing +{ + public class AuditTestController_Tests : AspNetCoreMvcTestBase + { + private readonly AbpAuditingOptions _options; + private IAuditingStore _auditingStore; + + public AuditTestController_Tests() + { + _options = ServiceProvider.GetRequiredService>().Value; + _auditingStore = ServiceProvider.GetRequiredService(); + } + + protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) + { + _auditingStore = Substitute.For(); + services.Replace(ServiceDescriptor.Singleton(_auditingStore)); + base.ConfigureServices(context, services); + } + + [Fact] + public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_GetRequests() + { + _options.IsEnabledForGetRequests = true; + await GetResponseAsync("api/audit-test/audit-success"); + //await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope + } + + [Fact] + public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() + { + _options.IsEnabled = false; + _options.AlwaysLogOnException = true; + await GetResponseAsync("api/audit-test/audit-fail", System.Net.HttpStatusCode.BadRequest); + //await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope + } + } +} From 73782c1408389289d17123e282f4753cf2183661 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Mon, 13 Jan 2020 12:35:00 +0300 Subject: [PATCH 02/22] Reverted SaveAsync on IAuditLgSaveHandle instead added AddException method. --- .../Auditing/AbpAuditingMiddleware.cs | 2 +- .../Volo/Abp/Auditing/AuditingManager.cs | 17 +++++++---------- .../Volo/Abp/Auditing/IAuditLogSaveHandle.cs | 3 ++- 3 files changed, 10 insertions(+), 12 deletions(-) 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 b651274ed4..f2929e3f5a 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 @@ -42,7 +42,7 @@ namespace Volo.Abp.AspNetCore.Auditing } catch (Exception ex) { - await scope.SaveAsync(ex).ConfigureAwait(false); + scope.AddException(ex); } finally { diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs index 1d3b912311..f2980d1cf5 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs @@ -150,16 +150,13 @@ namespace Volo.Abp.Auditing StopWatch = stopWatch; } - public async Task SaveAsync(Exception exception = null) - { - if (exception != null) - { - this.AuditLog.Exceptions.Add(exception); - } - else - { - await _auditingManager.SaveAsync(this).ConfigureAwait(false); - } + public async Task SaveAsync() + { + await _auditingManager.SaveAsync(this).ConfigureAwait(false); + } + public void AddException(Exception exception) + { + this.AuditLog.Exceptions.Add(exception); } public void Dispose() diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs index 6e48f3525c..a549dcb755 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs @@ -5,6 +5,7 @@ namespace Volo.Abp.Auditing { public interface IAuditLogSaveHandle : IDisposable { - Task SaveAsync(Exception exception = null); + Task SaveAsync(); + void AddException(Exception exception); } } \ No newline at end of file From 62127df0eab23d7f4c252a2b88b8dc4730039794 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Mon, 13 Jan 2020 13:25:31 +0300 Subject: [PATCH 03/22] removed empty space --- .../src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs index f2980d1cf5..409084ac6f 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs @@ -152,7 +152,7 @@ namespace Volo.Abp.Auditing public async Task SaveAsync() { - await _auditingManager.SaveAsync(this).ConfigureAwait(false); + await _auditingManager.SaveAsync(this).ConfigureAwait(false); } public void AddException(Exception exception) { From 5c73a61e1c76f6e97cf49133fd2114c8d03de8bb Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Mon, 13 Jan 2020 16:57:09 +0300 Subject: [PATCH 04/22] updated AuditingMiddleware for AlwaysLogOnExceptions --- .../Auditing/AbpAuditingMiddleware.cs | 27 ++++++++++++------- .../Volo/Abp/Auditing/AbpAuditingOptions.cs | 7 ++--- .../Mvc/Auditing/AuditTestController_Tests.cs | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) 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 f2929e3f5a..cc71797a5b 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 @@ -33,6 +33,23 @@ namespace Volo.Abp.AspNetCore.Auditing await next(context).ConfigureAwait(false); return; } + if (Options.AlwaysLogOnException) + { + using (var scope = _auditingManager.BeginScope()) + { + try + { + await next(context).ConfigureAwait(false); + return; + } + catch (Exception) + { + await scope.SaveAsync().ConfigureAwait(false); + if (!Options.HideErrors) + throw; + } + } + } using (var scope = _auditingManager.BeginScope()) { @@ -40,9 +57,8 @@ namespace Volo.Abp.AspNetCore.Auditing { await next(context).ConfigureAwait(false); } - catch (Exception ex) + catch (Exception) { - scope.AddException(ex); } finally { @@ -53,13 +69,6 @@ namespace Volo.Abp.AspNetCore.Auditing private bool ShouldWriteAuditLog(HttpContext httpContext) { - // IF selected, save audit logs on exception on GET requests even if audit log is disabled for GET requests. - if (Options.AlwaysLogOnException && - string.Equals(httpContext.Request.Method, HttpMethods.Get, StringComparison.OrdinalIgnoreCase)) - { - return true; - } - if (!Options.IsEnabled) { 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 5dcae1e097..db13bdaf74 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AbpAuditingOptions.cs @@ -29,11 +29,11 @@ namespace Volo.Abp.Auditing /// /// Default: true. /// - public bool IsEnabledForAnonymousUsers { get; set; } = false; + public bool IsEnabledForAnonymousUsers { get; set; } /// - /// Audit log e xceptions. - /// Default: false. + /// Audit log on exceptions. + /// Default: true. /// public bool AlwaysLogOnException { get; set; } @@ -54,6 +54,7 @@ namespace Volo.Abp.Auditing IsEnabled = true; IsEnabledForAnonymousUsers = true; HideErrors = true; + AlwaysLogOnException = true; Contributors = new List(); 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 97b211ba5a..b65f8eef1f 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 @@ -39,7 +39,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() { _options.IsEnabled = false; - _options.AlwaysLogOnException = true; + _options.AlwaysLogOnException = false; await GetResponseAsync("api/audit-test/audit-fail", System.Net.HttpStatusCode.BadRequest); //await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope } From ca64497b1f8a3cba727a8877ae3041dab5e437fd Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Mon, 13 Jan 2020 17:32:18 +0300 Subject: [PATCH 05/22] reverted back --- .../Auditing/AbpAuditingMiddleware.cs | 26 +++++++------------ .../Mvc/Auditing/AuditTestController_Tests.cs | 1 + 2 files changed, 10 insertions(+), 17 deletions(-) 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 cc71797a5b..4f0e87f27f 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 @@ -33,23 +33,6 @@ namespace Volo.Abp.AspNetCore.Auditing await next(context).ConfigureAwait(false); return; } - if (Options.AlwaysLogOnException) - { - using (var scope = _auditingManager.BeginScope()) - { - try - { - await next(context).ConfigureAwait(false); - return; - } - catch (Exception) - { - await scope.SaveAsync().ConfigureAwait(false); - if (!Options.HideErrors) - throw; - } - } - } using (var scope = _auditingManager.BeginScope()) { @@ -59,6 +42,10 @@ namespace Volo.Abp.AspNetCore.Auditing } catch (Exception) { + if (!Options.HideErrors) + { + throw; + } } finally { @@ -69,6 +56,11 @@ namespace Volo.Abp.AspNetCore.Auditing private bool ShouldWriteAuditLog(HttpContext httpContext) { + if (Options.AlwaysLogOnException) + { + return true; + } + if (!Options.IsEnabled) { 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 b65f8eef1f..1f81118e03 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 @@ -40,6 +40,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing { _options.IsEnabled = false; _options.AlwaysLogOnException = false; + _options.HideErrors = false; await GetResponseAsync("api/audit-test/audit-fail", System.Net.HttpStatusCode.BadRequest); //await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope } From 1a9fd66be77d9309ee45869c99ccab0e275f5647 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Mon, 13 Jan 2020 19:14:39 +0300 Subject: [PATCH 06/22] updated AbpAuditingMiddleware --- .../Auditing/AbpAuditingMiddleware.cs | 42 ++++++++++--------- .../Mvc/Auditing/AuditTestController_Tests.cs | 6 +-- 2 files changed, 26 insertions(+), 22 deletions(-) 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 4f0e87f27f..621e78630e 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 @@ -33,7 +33,7 @@ namespace Volo.Abp.AspNetCore.Auditing await next(context).ConfigureAwait(false); return; } - + bool hasError = false; using (var scope = _auditingManager.BeginScope()) { try @@ -42,6 +42,7 @@ namespace Volo.Abp.AspNetCore.Auditing } catch (Exception) { + hasError = true; if (!Options.HideErrors) { throw; @@ -49,34 +50,37 @@ namespace Volo.Abp.AspNetCore.Auditing } finally { - await scope.SaveAsync().ConfigureAwait(false); + if (ShouldWriteAuditLog(context, hasError)) + { + await scope.SaveAsync().ConfigureAwait(false); + } } } } - private bool ShouldWriteAuditLog(HttpContext httpContext) + private bool ShouldWriteAuditLog(HttpContext httpContext, bool hasError = false) { - if (Options.AlwaysLogOnException) + if (!Options.IsEnabled) + { + return false; + } + + if (Options.AlwaysLogOnException || hasError) { return true; + } + + if (!Options.IsEnabledForAnonymousUsers && !CurrentUser.IsAuthenticated) + { + return false; } - if (!Options.IsEnabled) - { - return false; - } - - if (!Options.IsEnabledForAnonymousUsers && !CurrentUser.IsAuthenticated) - { - return false; - } - if (!Options.IsEnabledForGetRequests && - string.Equals(httpContext.Request.Method, HttpMethods.Get, StringComparison.OrdinalIgnoreCase)) - { - return false; - } - + string.Equals(httpContext.Request.Method, HttpMethods.Get, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + return true; } } 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 1f81118e03..b411b20ee0 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 @@ -31,6 +31,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_GetRequests() { _options.IsEnabledForGetRequests = true; + _options.AlwaysLogOnException = false; await GetResponseAsync("api/audit-test/audit-success"); //await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope } @@ -38,9 +39,8 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing [Fact] public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() { - _options.IsEnabled = false; - _options.AlwaysLogOnException = false; - _options.HideErrors = false; + _options.IsEnabled = true; + _options.AlwaysLogOnException = true; await GetResponseAsync("api/audit-test/audit-fail", System.Net.HttpStatusCode.BadRequest); //await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope } From cce337439a1db4cf82ac483116b80f7614ed694b Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Thu, 16 Jan 2020 10:54:22 +0300 Subject: [PATCH 07/22] fixed logic error on middleware --- .../Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) 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 621e78630e..eee629e969 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 @@ -28,11 +28,6 @@ namespace Volo.Abp.AspNetCore.Auditing public async Task InvokeAsync(HttpContext context, RequestDelegate next) { - if (!ShouldWriteAuditLog(context)) - { - await next(context).ConfigureAwait(false); - return; - } bool hasError = false; using (var scope = _auditingManager.BeginScope()) { @@ -53,7 +48,7 @@ namespace Volo.Abp.AspNetCore.Auditing if (ShouldWriteAuditLog(context, hasError)) { await scope.SaveAsync().ConfigureAwait(false); - } + } } } } @@ -65,10 +60,10 @@ namespace Volo.Abp.AspNetCore.Auditing return false; } - if (Options.AlwaysLogOnException || hasError) + if (Options.AlwaysLogOnException && hasError) { return true; - } + } if (!Options.IsEnabledForAnonymousUsers && !CurrentUser.IsAuthenticated) { From 3106729f010fa6df3d513a9e73986d392a3750df Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Thu, 13 Feb 2020 10:33:18 +0300 Subject: [PATCH 08/22] ci: add npm install as first step to publish scripts --- npm/preview-publish.ps1 | 2 ++ npm/publish.ps1 | 2 ++ 2 files changed, 4 insertions(+) diff --git a/npm/preview-publish.ps1 b/npm/preview-publish.ps1 index c3a15f356b..f10173226b 100644 --- a/npm/preview-publish.ps1 +++ b/npm/preview-publish.ps1 @@ -2,6 +2,8 @@ param( [string]$Version ) +npm install + $NextVersion = $(node get-version.js) + '-preview' + (Get-Date).tostring(“yyyyMMdd”) $rootFolder = (Get-Item -Path "./" -Verbose).FullName diff --git a/npm/publish.ps1 b/npm/publish.ps1 index 3f0f03bc51..69f9ece982 100644 --- a/npm/publish.ps1 +++ b/npm/publish.ps1 @@ -2,6 +2,8 @@ param( [string]$Version ) +npm install + $NextVersion = $(node get-version.js) $rootFolder = (Get-Item -Path "./" -Verbose).FullName From ef78ff655ba986e949c4d157e452d06afc9ba6b2 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Thu, 13 Feb 2020 11:42:56 +0300 Subject: [PATCH 09/22] ci: remove unnecessary yarn command from publish.ps1 --- npm/preview-publish.ps1 | 1 - npm/publish.ps1 | 1 - 2 files changed, 2 deletions(-) diff --git a/npm/preview-publish.ps1 b/npm/preview-publish.ps1 index f10173226b..da705d07f9 100644 --- a/npm/preview-publish.ps1 +++ b/npm/preview-publish.ps1 @@ -16,7 +16,6 @@ $commands = ( "npm install", "npm run publish-packages -- --nextVersion $Version --preview", "cd ../../", - "yarn", "yarn lerna publish $Version --no-push --yes --no-git-reset --no-commit-hooks --no-git-tag-version --force-publish --dist-tag preview" ) diff --git a/npm/publish.ps1 b/npm/publish.ps1 index 69f9ece982..a510527401 100644 --- a/npm/publish.ps1 +++ b/npm/publish.ps1 @@ -16,7 +16,6 @@ $commands = ( "npm install", "npm run publish-packages -- --nextVersion $Version", "cd ../../", - "yarn", "yarn lerna publish $Version --no-push --yes --no-git-reset --no-commit-hooks --no-git-tag-version --force-publish", "yarn update:templates", "yarn gulp:app", From 67488b9ac24ca00703c70e6f8bbe8f5e4b726145 Mon Sep 17 00:00:00 2001 From: Arkat Erol Date: Thu, 13 Feb 2020 11:48:54 +0300 Subject: [PATCH 10/22] npm publish version test --- npm/preview-publish.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/npm/preview-publish.ps1 b/npm/preview-publish.ps1 index da705d07f9..d448378750 100644 --- a/npm/preview-publish.ps1 +++ b/npm/preview-publish.ps1 @@ -4,7 +4,7 @@ param( npm install -$NextVersion = $(node get-version.js) + '-preview' + (Get-Date).tostring(“yyyyMMdd”) +$NextVersion = $(node get-version.js) + '-preview' + (Get-Date).tostring(“yyyyMMdd”) + '-1' $rootFolder = (Get-Item -Path "./" -Verbose).FullName if(-Not $Version) { From abfb823fc23be0206bd134b2d69724f3e4ee1cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 13 Feb 2020 13:38:43 +0300 Subject: [PATCH 11/22] #2625 remove unused method. --- .../Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs | 5 ----- .../Volo/Abp/Auditing/IAuditLogSaveHandle.cs | 1 - 2 files changed, 6 deletions(-) diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs index 7315c86e79..2a9dd8bf5a 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs @@ -154,11 +154,6 @@ namespace Volo.Abp.Auditing { await _auditingManager.SaveAsync(this); } - - public void AddException(Exception exception) - { - this.AuditLog.Exceptions.Add(exception); - } public void Dispose() { diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs index a549dcb755..4709b745d2 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditLogSaveHandle.cs @@ -6,6 +6,5 @@ namespace Volo.Abp.Auditing public interface IAuditLogSaveHandle : IDisposable { Task SaveAsync(); - void AddException(Exception exception); } } \ No newline at end of file From f47a0c1f5f8e9d224663b2d8c2f829c8b78f91b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 13 Feb 2020 14:18:41 +0300 Subject: [PATCH 12/22] Fix audit log unit tests. --- .../AspNetCore/Auditing/AbpAuditingMiddleware.cs | 5 +---- .../Mvc/Auditing/AuditTestController.cs | 1 + .../Mvc/Auditing/AuditTestController_Tests.cs | 15 +++++++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) 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 b634892029..be84495336 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 @@ -38,10 +38,7 @@ namespace Volo.Abp.AspNetCore.Auditing catch (Exception) { hasError = true; - if (!Options.HideErrors) - { - throw; - } + throw; } finally { 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 967f3269d2..9b4892cea4 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,6 +5,7 @@ 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.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 b411b20ee0..733fab3f49 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 @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using System; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; @@ -33,7 +34,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing _options.IsEnabledForGetRequests = true; _options.AlwaysLogOnException = false; await GetResponseAsync("api/audit-test/audit-success"); - //await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope + await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope } [Fact] @@ -41,8 +42,14 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing { _options.IsEnabled = true; _options.AlwaysLogOnException = true; - await GetResponseAsync("api/audit-test/audit-fail", System.Net.HttpStatusCode.BadRequest); - //await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope + + try + { + await GetResponseAsync("api/audit-test/audit-fail", System.Net.HttpStatusCode.Forbidden); + } + catch { } + + await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope } } } From a601a10d9abadfcb7264784554184788d71f962c Mon Sep 17 00:00:00 2001 From: TheDiaval Date: Thu, 13 Feb 2020 14:27:00 +0300 Subject: [PATCH 13/22] feat(core): add set environment action to config state --- .../core/src/lib/actions/config.actions.ts | 6 ++++++ .../packages/core/src/lib/states/config.state.ts | 14 +++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/core/src/lib/actions/config.actions.ts b/npm/ng-packs/packages/core/src/lib/actions/config.actions.ts index 56d0874ebd..8fd11bd2cb 100644 --- a/npm/ng-packs/packages/core/src/lib/actions/config.actions.ts +++ b/npm/ng-packs/packages/core/src/lib/actions/config.actions.ts @@ -1,4 +1,5 @@ import { ABP } from '../models/common'; +import { Config } from '../models/config'; export class PatchRouteByName { static readonly type = '[Config] Patch Route By Name'; @@ -16,3 +17,8 @@ export class AddRoute { static readonly type = '[Config] Add Route'; constructor(public payload: Omit) {} } + +export class SetEnvironment { + static readonly type = '[Config] Set Environment'; + constructor(public environment: Config.Environment) {} +} diff --git a/npm/ng-packs/packages/core/src/lib/states/config.state.ts b/npm/ng-packs/packages/core/src/lib/states/config.state.ts index debbc9b8a0..ff964cfe92 100644 --- a/npm/ng-packs/packages/core/src/lib/states/config.state.ts +++ b/npm/ng-packs/packages/core/src/lib/states/config.state.ts @@ -2,7 +2,12 @@ import { Action, createSelector, Selector, State, StateContext, Store } from '@n import { of } from 'rxjs'; import { switchMap, tap } from 'rxjs/operators'; import snq from 'snq'; -import { GetAppConfiguration, PatchRouteByName, AddRoute } from '../actions/config.actions'; +import { + GetAppConfiguration, + PatchRouteByName, + AddRoute, + SetEnvironment, +} from '../actions/config.actions'; import { SetLanguage } from '../actions/session.actions'; import { ABP } from '../models/common'; import { Config } from '../models/config'; @@ -291,6 +296,13 @@ export class ConfigState { flattedRoutes, }); } + + @Action(SetEnvironment) + setEnvironment({ patchState }: StateContext, environment: Config.Environment) { + return patchState({ + environment, + }); + } } function patchRouteDeep( From 586047682b7e562a7c932537d8dec74d13656112 Mon Sep 17 00:00:00 2001 From: TheDiaval Date: Thu, 13 Feb 2020 14:27:28 +0300 Subject: [PATCH 14/22] feat(core): add dispatch function for set environment action --- .../core/src/lib/services/config-state.service.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts b/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts index 506b278634..cf97e42b0f 100644 --- a/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts @@ -1,8 +1,12 @@ import { Injectable } from '@angular/core'; import { Store } from '@ngxs/store'; +import { + AddRoute, + GetAppConfiguration, + PatchRouteByName, + SetEnvironment, +} from '../actions/config.actions'; import { ConfigState } from '../states'; -import { GetAppConfiguration, PatchRouteByName, AddRoute } from '../actions/config.actions'; -import { ABP } from '../models'; @Injectable({ providedIn: 'root', @@ -61,4 +65,8 @@ export class ConfigStateService { dispatchAddRoute(...args: ConstructorParameters) { return this.store.dispatch(new AddRoute(...args)); } + + dispatchSetEnvironment(...args: ConstructorParameters) { + return this.store.dispatch(new SetEnvironment(...args)); + } } From 4eb57ba270cd9b22fce284b9f8ee0a4a360a6721 Mon Sep 17 00:00:00 2001 From: TheDiaval Date: Thu, 13 Feb 2020 14:46:16 +0300 Subject: [PATCH 15/22] test(theme-shared): correct directive test --- .../src/lib/tests/table-sort.directive.spec.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tests/table-sort.directive.spec.ts b/npm/ng-packs/packages/theme-shared/src/lib/tests/table-sort.directive.spec.ts index f359518099..231f627fa1 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tests/table-sort.directive.spec.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/tests/table-sort.directive.spec.ts @@ -1,16 +1,20 @@ import { SpectatorDirective, createDirectiveFactory } from '@ngneat/spectator/jest'; import { TableSortDirective } from '../directives/table-sort.directive'; +import { TableComponent } from '../components/table/table.component'; +import { DummyLocalizationPipe } from './table.component.spec'; +import { PaginationComponent } from '../components'; describe('TableSortDirective', () => { let spectator: SpectatorDirective; let directive: TableSortDirective; const createDirective = createDirectiveFactory({ directive: TableSortDirective, + declarations: [TableComponent, DummyLocalizationPipe, PaginationComponent], }); beforeEach(() => { spectator = createDirective( - ``, + ``, ); directive = spectator.directive; }); @@ -21,7 +25,7 @@ describe('TableSortDirective', () => { test('should change table value', () => { expect(directive.value).toEqual([1, 4, 2]); - const table = spectator.query(Table); + const table = spectator.query(TableComponent); expect(table.value).toEqual([1, 2, 4]); }); }); From 1106d9021af2515493132e5ee1bc0d8ffbfe50aa Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Thu, 13 Feb 2020 16:34:06 +0300 Subject: [PATCH 16/22] closes volosoft/volo#926 --- .../Volo/Abp/Cli/Commands/SuiteCommand.cs | 20 +++++++++++++------ .../Cli/Licensing/DeveloperApiKeyResult.cs | 10 ++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs index a47a865617..a13621ea22 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs @@ -54,7 +54,7 @@ namespace Volo.Abp.Cli.Commands private async Task InstallSuiteAsync() { var nugetIndexUrl = await GetNuGetIndexUrlAsync(); - + if (nugetIndexUrl == null) { return; @@ -108,13 +108,21 @@ namespace Volo.Abp.Cli.Commands { var apiKeyResult = await _apiKeyService.GetApiKeyOrNullAsync(); - if (apiKeyResult == null || string.IsNullOrEmpty(apiKeyResult.ApiKey)) + if (apiKeyResult == null) + { + Logger.LogWarning("You are not signed in! Use the CLI command \"abp login \" to sign in, then try again."); + return null; + } + + if (!string.IsNullOrWhiteSpace(apiKeyResult.ErrorMessage)) { - Logger.LogError("Couldn't retrieve your NuGet API key!"); - Logger.LogWarning(File.Exists(CliPaths.AccessToken) - ? "Make sure you have an active session and license on commercial.abp.io. To re-sign in you can use the CLI command \"abp login \"." - : "You are not signed in to commercial.abp.io. Use the CLI command \"abp login \" to sign in."); + Logger.LogWarning(apiKeyResult.ErrorMessage); + return null; + } + if (string.IsNullOrEmpty(apiKeyResult.ApiKey)) + { + Logger.LogError("Couldn't retrieve your NuGet API key! You can re-sign in with the CLI command \"abp login \"."); return null; } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Licensing/DeveloperApiKeyResult.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Licensing/DeveloperApiKeyResult.cs index fb3622f6eb..eb781c27b1 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Licensing/DeveloperApiKeyResult.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Licensing/DeveloperApiKeyResult.cs @@ -10,5 +10,15 @@ namespace Volo.Abp.Cli.Licensing public DateTime? LicenseEndTime { get; set; } public bool CanDownloadSourceCode { get; set; } public string LicenseCode { get; set; } + public string ErrorMessage { get; set; } + public LicenseErrorType? ErrorType { get; set; } + + public enum LicenseErrorType + { + NotAuthenticated = 1, + NotMemberOfAnOrganization = 2, + NoActiveLicense = 3, + NotDeveloperOfTheOrganization = 4 + } } } \ No newline at end of file From e80cb601c1baf9ec6d32c21c9b157904ee6efd15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=87otur?= Date: Thu, 13 Feb 2020 16:44:07 +0300 Subject: [PATCH 17/22] Wrong class name is fixed 'BackgroundJobOptions' is changed to 'AbpBackgroundJobOptions' --- docs/en/Background-Jobs.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/Background-Jobs.md b/docs/en/Background-Jobs.md index 73815bb229..fa98542845 100644 --- a/docs/en/Background-Jobs.md +++ b/docs/en/Background-Jobs.md @@ -108,7 +108,7 @@ Enqueue method gets some optional arguments to control the background job: You may want to disable background job execution for your application. This is generally needed if you want to execute background jobs in another process and disable it for the current process. -Use `BackgroundJobOptions` to configure the job execution: +Use `AbpBackgroundJobOptions` to configure the job execution: ````csharp [DependsOn(typeof(AbpBackgroundJobsModule))] @@ -116,7 +116,7 @@ public class MyModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - Configure(options => + Configure(options => { options.IsJobExecutionEnabled = false; //Disables job execution }); From 2147fe52368ba6dfd62f28f5b004b6573b20c6b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=87otur?= Date: Thu, 13 Feb 2020 16:48:25 +0300 Subject: [PATCH 18/22] Update Background-Jobs.md --- docs/en/Background-Jobs.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/Background-Jobs.md b/docs/en/Background-Jobs.md index fa98542845..70f06bbadb 100644 --- a/docs/en/Background-Jobs.md +++ b/docs/en/Background-Jobs.md @@ -140,7 +140,7 @@ ABP framework includes a simple `IBackgroundJobManager` implementation that; ### Configuration -Use `BackgroundJobWorkerOptions` in your [module class](Module-Development-Basics.md) to configure the default background job manager. The example below changes the timeout duration for background jobs: +Use `AbpBackgroundJobWorkerOptions` in your [module class](Module-Development-Basics.md) to configure the default background job manager. The example below changes the timeout duration for background jobs: ````csharp [DependsOn(typeof(AbpBackgroundJobsModule))] @@ -148,7 +148,7 @@ public class MyModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - Configure(options => + Configure(options => { options.DefaultTimeout = 864000; //10 days (as seconds) }); From e37a53d19426113128e1d7c2aeb8d144278ea533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 13 Feb 2020 17:44:21 +0300 Subject: [PATCH 19/22] Add Data Filtering to docs-nav.json. --- docs/en/docs-nav.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/en/docs-nav.json b/docs/en/docs-nav.json index 6657ac629a..edbe193c3f 100644 --- a/docs/en/docs-nav.json +++ b/docs/en/docs-nav.json @@ -111,6 +111,10 @@ { "text": "Settings", "path": "Settings.md" + }, + { + "text": "Data Filtering", + "path": "Data-Filtering.md" } ] }, From a52fda1133d0f401d2dc51ffa74e65a0f8518099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 13 Feb 2020 17:44:28 +0300 Subject: [PATCH 20/22] Update AspNet-Boilerplate-Migration-Guide.md --- docs/en/AspNet-Boilerplate-Migration-Guide.md | 75 ++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/docs/en/AspNet-Boilerplate-Migration-Guide.md b/docs/en/AspNet-Boilerplate-Migration-Guide.md index b7453b9aff..bbd35d2409 100644 --- a/docs/en/AspNet-Boilerplate-Migration-Guide.md +++ b/docs/en/AspNet-Boilerplate-Migration-Guide.md @@ -277,6 +277,31 @@ ABP Framework doesn't include the ` ICustomValidate ` that does exists in the AS ## The Infrastructure Layer +### Configuration vs Options System + +ASP.NET Boilerplate has its own configuration system to configure the framework and the modules. For example, you could disable the audit logging in the `Initialize` method of your [module](https://aspnetboilerplate.com/Pages/Documents/Module-System): + +````csharp +public override void Initialize() +{ + Configuration.Auditing.IsEnabled = false; +} +```` + +ABP Framework uses [the options pattern](Options.md) to configure the framework and the modules. You typically configure the options in the `ConfigureServices` method of your [module](Module-Development-Basics.md): + +````csharp +public override void ConfigureServices(ServiceConfigurationContext context) +{ + Configure(options => + { + options.IsEnabled = false; + }); +} +```` + +Instead of a central configuration object, there are separated option classes for every module and feature those are defined in the related documents. + ### IAbpSession vs ICurrentUser and ICurrentTenant ASP.NET Boilerplate's `IAbpSession` service is used to obtain the current user and tenant information, like ` UserId ` and `TenantId`. @@ -301,11 +326,57 @@ You inherit from the `AuthorizationProvider` in the ASP.NET Boilerplate to defin ### Unit of Work -TODO +Unit of work system has been designed to work seamlessly. For most of the cases, you don't need to change anything. + +`UnitOfWork` attribute of the ABP Framework doesn't have the `ScopeOption` (type of `TransactionScopeOption`) property. Instead, use `IUnitOfWorkManager.Begin()` method with `requiresNew = true` to create an independent inner transaction in a transaction scope. + +#### Data Filters + +ASP.NET Boilerplate implements the data filtering system as a part of the unit of work. ABP Framework has a separate `IDataFilter` service. + +See the [data filtering document](Data-Filtering.md) to learn how to enable/disable a filter. + +See [the UOW documentation](Unit-Of-Work.md) for more about the UOW system. ### Multi-Tenancy -TODO +#### IMustHaveTenant & IMayHaveTenant vs IMultiTenant + +ASP.NET Boilerplate defines `IMustHaveTenant` and `IMayHaveTenant` interfaces to implement them for your entities. In this way, your entities are automatically filtered according to the current tenant. Because of the design, there was a problem: You had to create a "Default" tenant in the database with "1" as the Id if you want to create a non multi-tenant application (this "Default" tenant was used as the single tenant). + +ABP Framework has a single interface for multi-tenant entities: `IMultiTenant` which defines a nullable `TenantId` property of type `Guid`. If your application is not multi-tenant, then your entities will have null TenantId (instead of a default one). + +On the migration, you need to change the TenantId field type and replace these interfaces with the `IMultiTenant` + +#### Switch Between Tenants + +In some cases you might need to switch to a tenant for a code scope and work with the tenant's data in this scope. + +In ASP.NET Boilerplate, it is done using the `IUnitOfWorkManager` service: + +````csharp +public async Task> GetProducts(int tenantId) +{ + using (_unitOfWorkManager.Current.SetTenantId(tenantId)) + { + return await _productRepository.GetAllListAsync(); + } +} +```` + +In the ABP Framework it is done with the `ICurrentTenant` service: + +````csharp +public async Task> GetProducts(Guid tenantId) +{ + using (_currentTenant.Change(tenantId)) + { + return await _productRepository.GetListAsync(); + } +} +```` + +Pass `null` to the `Change` method to switch to the host side. ## Missing Features From 4f00d1cf8e130f7d23d700a1f3e354d14ac74c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 13 Feb 2020 19:02:58 +0300 Subject: [PATCH 21/22] Update AspNet-Boilerplate-Migration-Guide.md --- docs/en/AspNet-Boilerplate-Migration-Guide.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/docs/en/AspNet-Boilerplate-Migration-Guide.md b/docs/en/AspNet-Boilerplate-Migration-Guide.md index bbd35d2409..cc5bf39228 100644 --- a/docs/en/AspNet-Boilerplate-Migration-Guide.md +++ b/docs/en/AspNet-Boilerplate-Migration-Guide.md @@ -277,6 +277,54 @@ ABP Framework doesn't include the ` ICustomValidate ` that does exists in the AS ## The Infrastructure Layer +### Namespaces + +ASP.NET Boilerplate uses the `Abp.*` namespaces while the ABP Framework uses the `Volo.Abp.*` namespaces for the framework and pre-built fundamental modules. + +In addition, there are also some pre-built application modules (like docs and blog modules) those are using the `Volo.*` namespaces (like `Volo.Blogging.*` and `Volo.Docs.*`). We consider these modules as standalone open source products developed by Volosoft rather than add-ons or generic modules completing the ABP Framework and used in the applications. We've developed them as a module to make them re-usable as a part of a bigger solution. + +### Module System + +Both of the ASP.NET Boilerplate and the ABP Framework have the `AbpModule` while they are a bit different. + +ASP.NET Boilerplate's `AbpModule` class has `PreInitialize`, `Initialize` and `PostInitialize` methods you can override and configure the framework and the depended modules. You can also register and resolve dependencies in these methods. + +ABP Framework's `AbpModule` class has the `ConfigureServices` and `OnApplicationInitialization` methods (and their Pre and Post versions). It is similar to ASP.NET Core's Startup class. You configure other services and register dependencies in the `ConfigureServices`. However, you can now resolve dependencies in that point. You can resolve dependencies and configure the ASP.NET Core pipeline in the `OnApplicationInitialization` method while you can not register dependencies here. So, the new module classes separate dependency registration phase from dependency resolution phase since it follows the ASP.NET Core's approach. + +### Dependency Injection + +#### The DI Framework + +ASP.NET Boilerplate is using the [Castle Windsor](http://www.castleproject.org/projects/windsor/) as the dependency injection framework. This is a fundamental dependency of the ASP.NET Boilerplate framework. We've got a lot of feedback to make the ASP.NET Boilerplate DI framework agnostic, but it was not so easy because of the design. + +ABP Framework is dependency injection framework independent since it uses Microsoft's [Dependency Injection Extensions](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection) library as an abstraction. None of the ABP Framework or module packages depends on any specific library. + +However, ABP Framework doesn't use the Microsoft's base DI library because it has some missing features ABP Framework needs to: Property Injection and Interception. All the startup templates and the samples are using the [Autofac](https://autofac.org/) as the DI library and it is the only [officially integrated](Autofac-Integration.md) library to the ABP Framework. We suggest you to use the Autofac with the ABP Framework if you have not a good reason. If you have a good reason, please create an [issue](https://github.com/abpframework/abp/issues/new) on GitHub to request it or just implement it and send a pull request :) + +#### Registering the Dependencies + +Registering the dependencies are similar and mostly handled by the framework conventionally (like repositories, application services, controllers... etc). Implement the same `ITransientDependency`, `ISingletonDependency` and `IScopedDependency` interfaces for the services not registered by conventions. + +When you need to manually register dependencies, use the `context.Services` in the `ConfigureServices` method of your module. Example: + +````csharp +public class BlogModule : AbpModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) + { + //Register an instance as singleton + context.Services.AddSingleton(new TaxCalculator(taxRatio: 0.18)); + + //Register a factory method that resolves from IServiceProvider + context.Services.AddScoped( + sp => sp.GetRequiredService() + ); + } +} +```` + +See the ABP Framework [dependency injection document](https://docs.abp.io/en/abp/latest/Dependency-Injection) for details. + ### Configuration vs Options System ASP.NET Boilerplate has its own configuration system to configure the framework and the modules. For example, you could disable the audit logging in the `Initialize` method of your [module](https://aspnetboilerplate.com/Pages/Documents/Module-System): From 8efe5efff906152ce32c4eb5980d3e1b457ff863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 13 Feb 2020 19:32:28 +0300 Subject: [PATCH 22/22] Added more sections to the migration guide. --- docs/en/AspNet-Boilerplate-Migration-Guide.md | 89 +++++++++++++++++++ docs/en/Clock.md | 3 + docs/en/Logging.md | 3 +- docs/en/docs-nav.json | 4 + 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 docs/en/Clock.md diff --git a/docs/en/AspNet-Boilerplate-Migration-Guide.md b/docs/en/AspNet-Boilerplate-Migration-Guide.md index cc5bf39228..0a2b3b17b3 100644 --- a/docs/en/AspNet-Boilerplate-Migration-Guide.md +++ b/docs/en/AspNet-Boilerplate-Migration-Guide.md @@ -426,10 +426,99 @@ public async Task> GetProducts(Guid tenantId) Pass `null` to the `Change` method to switch to the host side. +### Caching + +ASP.NET Boilerplate has its [own distributed caching abstraction](https://aspnetboilerplate.com/Pages/Documents/Caching) which has in-memory and Redis implementations. You typically inject the `ICacheManager` service and use its `GetCache(...)` method to obtain a cache, then get and set objects in the cache. + +ABP Framework uses and extends ASP.NET Core's [distributed caching abstraction](Caching.md). It defines the `IDistributedCache` services to inject a cache and get/set objects. + +### Logging + +ASP.NET Boilerplate uses Castle Windsor's [logging facility](http://docs.castleproject.org/Windsor.Logging-Facility.ashx) as an abstraction and supports multiple logging providers including Log4Net (the default one comes with the startup projects) and Serilog. You typically property-inject the logger: + +````csharp +using Castle.Core.Logging; //1: Import Logging namespace + +public class TaskAppService : ITaskAppService +{ + //2: Getting a logger using property injection + public ILogger Logger { get; set; } + + public TaskAppService() + { + //3: Do not write logs if no Logger supplied. + Logger = NullLogger.Instance; + } + + public void CreateTask(CreateTaskInput input) + { + //4: Write logs + Logger.Info("Creating a new task with description: " + input.Description); + //... + } +} +```` + +ABP Framework depends on Microsoft's [logging extensions](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging) library which is also an abstraction and there are many providers implement it. Startup templates are using the Serilog as the pre-configured logging libary while it is easy to change in your project. The usage pattern is similar: + +````csharp +//1: Import the Logging namespaces +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + +public class TaskAppService : ITaskAppService +{ + //2: Getting a logger using property injection + public ILogger Logger { get; set; } + + public TaskAppService() + { + //3: Do not write logs if no Logger supplied. + Logger = NullLogger.Instance; + } + + public void CreateTask(CreateTaskInput input) + { + //4: Write logs + Logger.Info("Creating a new task with description: " + input.Description); + //... + } +} +```` + +You inject the `ILogger` instead of the `ILogger`. + +### Setting Management + +#### Defining the Settings + +In an ASP.NET Boilerplate based application, you create a class deriving from the `SettingProvider` class, implement the `GetSettingDefinitions` method and add your class to the `Configuration.Settings.Providers` list. + +In the ABP Framework, you need to derive your class from the `SettingDefinitionProvider` and implement the `Define` method. You don't need to register your class since the ABP Framework automatically discovers it. + +#### Getting the Setting Values + +ASP.NET Boilerplate provides the `ISettingManager` to read the setting values in the server side and `abp.setting.get(...)` method in the JavaScript side. + +ABP Framework has the `ISettingProvider` service to read the setting values in the server side and `abp.setting.get(...)` method in the JavaScript side. + +#### Setting the Setting Values + +For ASP.NET Boilerplate, you use the same `ISettingManager` service to change the setting values. + +ABP Framework separates it and provides the setting management module (pre-added to the startup projects) which has the ` ISettingManager ` to change the setting values. This separation was introduced to support tiered deployment scenarios (where `ISettingProvider` can also work in the client application while `ISettingManager ` can also work in the server (API) side). + +### Clock + +ASP.NET Boilerplate has a static `Clock` service ([see](https://aspnetboilerplate.com/Pages/Documents/Timing)) which is used to abstract the `DateTime` kind, so you can easily switch between Local and UTC times. You don't inject it, but just use the `Clock.Now` static method to obtain the current time. + +ABP Framework has the `IClock` service ([see](Clock.md)) which has a similar goal, but now you need to inject it whenever you need it. + ## Missing Features The following features are not present for the ABP Framework. Here, a list of major missing features (and the related issue for that feature waiting on the ABP Framework GitHub repository): * [Multi-Lingual Entities](https://aspnetboilerplate.com/Pages/Documents/Multi-Lingual-Entities) ([#1754](https://github.com/abpframework/abp/issues/1754)) +* ...TODO Most of these features will eventually be implemented. However, you can implement them yourself if they are important for you. If you want, you can [contribute](Contribution/Index.md) to the framework by implementing these yourself. \ No newline at end of file diff --git a/docs/en/Clock.md b/docs/en/Clock.md new file mode 100644 index 0000000000..46ef1235e0 --- /dev/null +++ b/docs/en/Clock.md @@ -0,0 +1,3 @@ +# Clock + +TODO \ No newline at end of file diff --git a/docs/en/Logging.md b/docs/en/Logging.md index 9b9c604bed..7fa45c03b9 100644 --- a/docs/en/Logging.md +++ b/docs/en/Logging.md @@ -2,4 +2,5 @@ ABP Framework doesn't implement any logging infrastructure. It uses the [ASP.NET Core's logging system](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging). -> .NET Core's logging system is actually independent from the ASP.NET Core. It is usable in any type of application. \ No newline at end of file +> .NET Core's logging system is actually independent from the ASP.NET Core. It is usable in any type of application. + diff --git a/docs/en/docs-nav.json b/docs/en/docs-nav.json index edbe193c3f..eb4ef13b8d 100644 --- a/docs/en/docs-nav.json +++ b/docs/en/docs-nav.json @@ -104,6 +104,10 @@ "text": "Caching", "path": "Caching.md" }, + { + "text": "Logging", + "path": "Logging.md" + }, { "text": "Audit Logging", "path": "Audit-Logging.md"