mirror of https://github.com/abpframework/abp.git
5 changed files with 135 additions and 51 deletions
@ -0,0 +1,43 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Reflection; |
||||
|
using Newtonsoft.Json; |
||||
|
using Newtonsoft.Json.Serialization; |
||||
|
|
||||
|
namespace Volo.Abp.Auditing |
||||
|
{ |
||||
|
public class AuditingContractResolver : CamelCasePropertyNamesContractResolver |
||||
|
{ |
||||
|
private readonly List<Type> _ignoredTypes; |
||||
|
|
||||
|
public AuditingContractResolver(List<Type> ignoredTypes) |
||||
|
{ |
||||
|
_ignoredTypes = ignoredTypes; |
||||
|
} |
||||
|
|
||||
|
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) |
||||
|
{ |
||||
|
var property = base.CreateProperty(member, memberSerialization); |
||||
|
|
||||
|
if (_ignoredTypes.Any(ignoredType => ignoredType.GetTypeInfo().IsAssignableFrom(property.PropertyType))) |
||||
|
{ |
||||
|
property.ShouldSerialize = instance => false; |
||||
|
return property; |
||||
|
} |
||||
|
|
||||
|
if (member.DeclaringType != null && (member.DeclaringType.IsDefined(typeof(DisableAuditingAttribute)) || member.DeclaringType.IsDefined(typeof(JsonIgnoreAttribute)))) |
||||
|
{ |
||||
|
property.ShouldSerialize = instance => false; |
||||
|
return property; |
||||
|
} |
||||
|
|
||||
|
if (member.IsDefined(typeof(DisableAuditingAttribute)) || member.IsDefined(typeof(JsonIgnoreAttribute))) |
||||
|
{ |
||||
|
property.ShouldSerialize = instance => false; |
||||
|
} |
||||
|
|
||||
|
return property; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,45 +0,0 @@ |
|||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Linq; |
|
||||
using System.Text.Json; |
|
||||
using System.Text.Json.Serialization; |
|
||||
|
|
||||
namespace Volo.Abp.Auditing |
|
||||
{ |
|
||||
public class AuditingRuntimeIgnoreConverter : JsonConverter<Dictionary<string, object>> |
|
||||
{ |
|
||||
private readonly List<Type> _ignoredTypes; |
|
||||
|
|
||||
public AuditingRuntimeIgnoreConverter(List<Type> ignoredTypes) |
|
||||
{ |
|
||||
_ignoredTypes = ignoredTypes; |
|
||||
} |
|
||||
|
|
||||
public override Dictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
|
||||
{ |
|
||||
throw new JsonException(); |
|
||||
} |
|
||||
|
|
||||
public override void Write(Utf8JsonWriter writer, Dictionary<string, object> value, JsonSerializerOptions options) |
|
||||
{ |
|
||||
var newDictionary = new Dictionary<string, object>(); |
|
||||
foreach (var item in value.Where(x => x.Value != null)) |
|
||||
{ |
|
||||
if (item.GetType().IsDefined(typeof(DisableAuditingAttribute), true) || |
|
||||
item.GetType().IsDefined(typeof(JsonIgnoreAttribute), true)) |
|
||||
{ |
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
if (_ignoredTypes.Any(x => x.IsInstanceOfType(item.Value))) |
|
||||
{ |
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
newDictionary[item.Key] = item.Value; |
|
||||
} |
|
||||
|
|
||||
JsonSerializer.Serialize(writer, newDictionary); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,67 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Auditing |
||||
|
{ |
||||
|
public class JsonNetAuditSerializer_Test : AbpAuditingTestBase |
||||
|
{ |
||||
|
private readonly JsonNetAuditSerializer _jsonNetAuditSerializer; |
||||
|
|
||||
|
public JsonNetAuditSerializer_Test() |
||||
|
{ |
||||
|
_jsonNetAuditSerializer = GetRequiredService<JsonNetAuditSerializer>(); |
||||
|
} |
||||
|
|
||||
|
protected override void AfterAddApplication(IServiceCollection services) |
||||
|
{ |
||||
|
services.Configure<AbpAuditingOptions>(options => |
||||
|
{ |
||||
|
options.IgnoredTypes.Add(typeof(DateTime)); |
||||
|
}); |
||||
|
|
||||
|
base.AfterAddApplication(services); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Serialize_Test() |
||||
|
{ |
||||
|
var arguments = new Dictionary<string, object> |
||||
|
{ |
||||
|
{"input", new InputDto {PersonData = "IdCard:123123"}}, |
||||
|
{"input2", new Input2Dto {UserName = "admin", Password = "1q2w3E*", Birthday = DateTime.Now}} |
||||
|
}; |
||||
|
|
||||
|
var str = _jsonNetAuditSerializer.Serialize(arguments); |
||||
|
|
||||
|
str.ShouldNotContain("IdCard"); |
||||
|
str.ShouldNotContain("1q2w3E*"); |
||||
|
str.ShouldNotContain("Birthday"); |
||||
|
|
||||
|
str.ShouldContain("UserName"); |
||||
|
str.ShouldContain("admin"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[DisableAuditing] |
||||
|
class InputDto |
||||
|
{ |
||||
|
public string PersonData { get; set; } |
||||
|
} |
||||
|
|
||||
|
class Input2Dto |
||||
|
{ |
||||
|
public string UserName { get; set; } |
||||
|
|
||||
|
[DisableAuditing] |
||||
|
public string Password { get; set; } |
||||
|
|
||||
|
[Newtonsoft.Json.JsonIgnore] |
||||
|
public string PrivateEmail { get; set; } |
||||
|
|
||||
|
public DateTime Birthday { get; set; } |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue