Add IAuditingHelper.IsEntityHistoryEnabled

pull/3509/head
Halil İbrahim Kalkan 5 years ago
parent 5ef90a5e9a
commit 77eb20734c

@ -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

@ -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(

@ -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; }
/// <summary>
/// Gets a pre-defined permission group.
/// Throws <see cref="AbpException"/> if can not find the given group.

@ -0,0 +1,34 @@
using JetBrains.Annotations;
namespace Volo.Abp.Authorization.Permissions
{
public static class PermissionDefinitionContextExtensions
{
/// <summary>
/// Finds and disables a permission with the given <paramref name="name"/>.
/// Returns false if given permission was not found.
/// </summary>
/// <param name="context">Permission definition context</param>
/// <param name="name">Name of the permission</param>
/// <returns>
/// Returns true if given permission was found.
/// Returns false if given permission was not found.
/// </returns>
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;
}
}
}

@ -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<AbpAuditingOptions> options,
IClock clock,
IJsonSerializer jsonSerializer)
IJsonSerializer jsonSerializer,
IAuditingHelper auditingHelper)
{
_clock = clock;
AuditingStore = auditingStore;
JsonSerializer = jsonSerializer;
AuditingHelper = auditingHelper;
Options = options.Value;
Logger = NullLogger<EntityHistoryHelper>.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;
}

Loading…
Cancel
Save