From 77eb20734c1140cf94648cedb58c8b9e2d00d057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 7 Apr 2020 13:06:20 +0300 Subject: [PATCH] Add IAuditingHelper.IsEntityHistoryEnabled --- .../Volo/Abp/Auditing/AuditingHelper.cs | 38 +++++++++++++++++++ .../Volo/Abp/Auditing/IAuditingHelper.cs | 4 +- .../IPermissionDefinitionContext.cs | 5 ++- .../PermissionDefinitionContextExtensions.cs | 34 +++++++++++++++++ .../EntityHistory/EntityHistoryHelper.cs | 36 +++--------------- 5 files changed, 85 insertions(+), 32 deletions(-) create mode 100644 framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionDefinitionContextExtensions.cs diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingHelper.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingHelper.cs index b333e07bf9..e53ba5b9a6 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingHelper.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingHelper.cs @@ -86,6 +86,44 @@ namespace Volo.Abp.Auditing return defaultValue; } + public virtual bool IsEntityHistoryEnabled(Type entityType, bool defaultValue = false) + { + if (!entityType.IsPublic) + { + return false; + } + + if (Options.IgnoredTypes.Any(t => t.IsAssignableFrom(entityType))) + { + return false; + } + + if (entityType.IsDefined(typeof(AuditedAttribute), true)) + { + return true; + } + + foreach (var propertyInfo in entityType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) + { + if(propertyInfo.IsDefined(typeof(AuditedAttribute))) + { + return true; + } + } + + if (entityType.IsDefined(typeof(DisableAuditingAttribute), true)) + { + return false; + } + + if (Options.EntityHistorySelectors.Any(selector => selector.Predicate(entityType))) + { + return true; + } + + return defaultValue; + } + public virtual AuditLogInfo CreateAuditLogInfo() { var auditInfo = new AuditLogInfo diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditingHelper.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditingHelper.cs index e6992491b8..6ee13a57d1 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditingHelper.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IAuditingHelper.cs @@ -4,11 +4,13 @@ using System.Reflection; namespace Volo.Abp.Auditing { - //TODO: Move ShouldSaveAudit and rename to IAuditingFactory + //TODO: Move ShouldSaveAudit & IsEntityHistoryEnabled and rename to IAuditingFactory public interface IAuditingHelper { bool ShouldSaveAudit(MethodInfo methodInfo, bool defaultValue = false); + bool IsEntityHistoryEnabled(Type entityType, bool defaultValue = false); + AuditLogInfo CreateAuditLogInfo(); AuditLogActionInfo CreateAuditLogAction( diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/IPermissionDefinitionContext.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/IPermissionDefinitionContext.cs index 0b649287ea..3c700dfdf6 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/IPermissionDefinitionContext.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/IPermissionDefinitionContext.cs @@ -1,4 +1,5 @@ -using JetBrains.Annotations; +using System; +using JetBrains.Annotations; using Volo.Abp.Localization; using Volo.Abp.MultiTenancy; @@ -8,6 +9,8 @@ namespace Volo.Abp.Authorization.Permissions { //TODO: Add Get methods to find and modify a permission or group. + IServiceProvider ServiceProvider { get; } + /// /// Gets a pre-defined permission group. /// Throws if can not find the given group. diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionDefinitionContextExtensions.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionDefinitionContextExtensions.cs new file mode 100644 index 0000000000..e1bdcb8057 --- /dev/null +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/PermissionDefinitionContextExtensions.cs @@ -0,0 +1,34 @@ +using JetBrains.Annotations; + +namespace Volo.Abp.Authorization.Permissions +{ + public static class PermissionDefinitionContextExtensions + { + /// + /// Finds and disables a permission with the given . + /// Returns false if given permission was not found. + /// + /// Permission definition context + /// Name of the permission + /// + /// Returns true if given permission was found. + /// Returns false if given permission was not found. + /// + public static bool TryDisablePermission( + [NotNull] this IPermissionDefinitionContext context, + [NotNull] string name) + { + Check.NotNull(context, nameof(context)); + Check.NotNull(name, nameof(name)); + + var permission = context.GetPermissionOrNull(name); + if (permission == null) + { + return false; + } + + permission.IsEnabled = false; + return true; + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index 155e639bff..af728686fd 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; @@ -15,7 +14,6 @@ using Volo.Abp.Json; using Volo.Abp.MultiTenancy; using Volo.Abp.Reflection; using Volo.Abp.Timing; -using Volo.Abp.Uow; namespace Volo.Abp.EntityFrameworkCore.EntityHistory { @@ -26,6 +24,7 @@ namespace Volo.Abp.EntityFrameworkCore.EntityHistory protected IAuditingStore AuditingStore { get; } protected IJsonSerializer JsonSerializer { get; } protected AbpAuditingOptions Options { get; } + protected IAuditingHelper AuditingHelper { get; } private readonly IClock _clock; @@ -33,11 +32,13 @@ namespace Volo.Abp.EntityFrameworkCore.EntityHistory IAuditingStore auditingStore, IOptions options, IClock clock, - IJsonSerializer jsonSerializer) + IJsonSerializer jsonSerializer, + IAuditingHelper auditingHelper) { _clock = clock; AuditingStore = auditingStore; JsonSerializer = jsonSerializer; + AuditingHelper = auditingHelper; Options = options.Value; Logger = NullLogger.Instance; @@ -203,39 +204,14 @@ namespace Volo.Abp.EntityFrameworkCore.EntityHistory return false; } - if (Options.IgnoredTypes.Any(t => t.IsInstanceOfType(entityEntry.Entity))) - { - return false; - } + var entityType = entityEntry.Metadata.ClrType; - var entityType = entityEntry.Entity.GetType(); if (!EntityHelper.IsEntity(entityType)) { return false; } - if (!entityType.IsPublic) - { - return false; - } - - if (entityType.IsDefined(typeof(AuditedAttribute), true)) - { - return true; - } - - if (entityEntry.Metadata.GetProperties() - .Any(p => p.PropertyInfo?.IsDefined(typeof(AuditedAttribute)) ?? false)) - { - return true; - } - - if (entityType.IsDefined(typeof(DisableAuditingAttribute), true)) - { - return false; - } - - if (Options.EntityHistorySelectors.Any(selector => selector.Predicate(entityType))) + if (AuditingHelper.IsEntityHistoryEnabled(entityType)) { return true; }