Skip to content

Commit 02414f7

Browse files
committed
🆕 (entity) 添加数据审计时可以忽略某些不需要审计或者敏感的实体属性的功能 #143 #64
1 parent f3621c9 commit 02414f7

File tree

9 files changed

+47
-11
lines changed

9 files changed

+47
-11
lines changed

samples/web/Liuliu.Demo.Web/Controllers/IdentityController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public async Task<AjaxResult> Login(LoginDto dto)
202202
/// <returns>JSON操作结果</returns>
203203
[HttpPost]
204204
[ModuleInfo]
205-
[Description("JwtToken")]
205+
[Description("Token")]
206206
public async Task<AjaxResult> Token(TokenDto dto)
207207
{
208208
string grantType = dto.GrantType?.UpperToLowerAndSplit("_");

samples/web/ui/ng-alain8/src/app/routes/passport/login/login.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
</nz-col>
6767
</nz-form-item>
6868
<nz-form-item>
69-
<button nz-button type="submit" nzType="primary" nzSize="large" acl="Root.Site.Identity.Jwtoken" [nzLoading]="http.loading" nzBlock [disabled]="http.loading">
69+
<button nz-button type="submit" nzType="primary" nzSize="large" [nzLoading]="http.loading" nzBlock [disabled]="http.loading">
7070
{{ 'app.login.login' | translate }}
7171
</button>
7272
</nz-form-item>

src/OSharp.AspNetCore/Mvc/Filters/AuditOperationAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public override void OnResultExecuted(ResultExecutedContext context)
8080
IUnitOfWork unitOfWork = provider.GetUnitOfWork<Function, Guid>();
8181
//回滚之前业务处理中的未提交事务,防止审计信息保存时误提交
8282
unitOfWork?.Rollback();
83-
83+
84+
//移除当前功能,使保存审计信息的时候不再获取记录变更,审计信息不需要再审计
85+
dict.Function = null;
8486
IAuditStore store = provider.GetService<IAuditStore>();
8587
store?.Save(dict.AuditOperation);
8688
unitOfWork?.Commit();

src/OSharp.EntityFrameworkCore/AuditEntityProvider.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using OSharp.Authorization.Functions;
2020
using OSharp.Collections;
2121
using OSharp.Dependency;
22+
using OSharp.Reflection;
2223

2324

2425
namespace OSharp.Entity
@@ -100,6 +101,12 @@ private static AuditEntityEntry GetAuditEntity(EntityEntry entry, IEntityInfo en
100101
{
101102
continue;
102103
}
104+
105+
if (property.PropertyInfo == null || property.PropertyInfo.HasAttribute<AuditIgnoreAttribute>())
106+
{
107+
continue;
108+
}
109+
103110
string name = property.Name;
104111
if (property.IsPrimaryKey())
105112
{
@@ -110,7 +117,7 @@ private static AuditEntityEntry GetAuditEntity(EntityEntry entry, IEntityInfo en
110117
AuditPropertyEntry auditProperty = new AuditPropertyEntry()
111118
{
112119
FieldName = name,
113-
DisplayName = entityProperties.First(m => m.Name == name)?.Display ?? name,
120+
DisplayName = entityProperties.FirstOrDefault(m => m.Name == name)?.Display ?? name,
114121
DataType = property.ClrType.ToString()
115122
};
116123
if (entry.State == EntityState.Added)

src/OSharp.Identity/Identity/Entities/RoleBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.ComponentModel;
1212
using System.ComponentModel.DataAnnotations;
1313

14+
using OSharp.Audits;
1415
using OSharp.Entity;
1516

1617

@@ -41,13 +42,13 @@ protected RoleBase()
4142
/// <summary>
4243
/// 获取或设置 标准化角色名称
4344
/// </summary>
44-
[Required, DisplayName("标准化角色名称")]
45+
[Required, DisplayName("标准化角色名称"), AuditIgnore]
4546
public string NormalizedName { get; set; }
4647

4748
/// <summary>
4849
/// 获取或设置 一个随机值,每当某个角色被保存到存储区时,该值将发生变化。
4950
/// </summary>
50-
[DisplayName("版本标识")]
51+
[DisplayName("版本标识"), AuditIgnore]
5152
public string ConcurrencyStamp { get; set; } = Guid.NewGuid().ToString();
5253

5354
/// <summary>

src/OSharp.Identity/Identity/Entities/UserBase.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.ComponentModel;
1212
using System.ComponentModel.DataAnnotations;
1313

14+
using OSharp.Audits;
1415
using OSharp.Entity;
1516

1617

@@ -41,7 +42,7 @@ protected UserBase()
4142
/// <summary>
4243
/// 获取或设置 标准化的用户名
4344
/// </summary>
44-
[Required, DisplayName("标准化的用户名")]
45+
[Required, DisplayName("标准化的用户名"), AuditIgnore]
4546
public string NormalizedUserName { get; set; }
4647

4748
/// <summary>
@@ -59,7 +60,7 @@ protected UserBase()
5960
/// <summary>
6061
/// 获取或设置 标准化的电子邮箱
6162
/// </summary>
62-
[DisplayName("标准化的电子邮箱"), DataType(DataType.EmailAddress)]
63+
[DisplayName("标准化的电子邮箱"), DataType(DataType.EmailAddress), AuditIgnore]
6364
public string NormalizeEmail { get; set; }
6465

6566
/// <summary>
@@ -71,7 +72,7 @@ protected UserBase()
7172
/// <summary>
7273
/// 获取或设置 密码哈希值
7374
/// </summary>
74-
[DisplayName("密码哈希值")]
75+
[DisplayName("密码哈希值"), AuditIgnore]
7576
public string PasswordHash { get; set; }
7677

7778
/// <summary>
@@ -83,13 +84,13 @@ protected UserBase()
8384
/// <summary>
8485
/// 获取或设置 每当用户凭据发生变化(密码更改、登录删除)时必须更改的随机值。
8586
/// </summary>
86-
[DisplayName("安全标识")]
87+
[DisplayName("安全标识"), AuditIgnore]
8788
public string SecurityStamp { get; set; }
8889

8990
/// <summary>
9091
/// 获取或设置 一个随机值,必须在用户持续存储时更改。
9192
/// </summary>
92-
[DisplayName("版本标识")]
93+
[DisplayName("版本标识"), AuditIgnore]
9394
public string ConcurrencyStamp { get; set; } = Guid.NewGuid().ToString();
9495

9596
/// <summary>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="AuditIgnoreAttribute.cs" company="OSharp开源团队">
3+
// Copyright (c) 2014-2020 OSharp. All rights reserved.
4+
// </copyright>
5+
// <site>http://www.osharp.org</site>
6+
// <last-editor>郭明锋</last-editor>
7+
// <last-date>2020-03-26 15:06</last-date>
8+
// -----------------------------------------------------------------------
9+
10+
using System;
11+
12+
13+
namespace OSharp.Audits
14+
{
15+
/// <summary>
16+
/// 标记在审计中忽略的属性
17+
/// </summary>
18+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
19+
public sealed class AuditIgnoreAttribute : Attribute
20+
{ }
21+
}

src/OSharp/Entity/ICreatedTime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// -----------------------------------------------------------------------
99

1010
using System;
11+
using System.ComponentModel;
1112

1213

1314
namespace OSharp.Entity
@@ -20,6 +21,7 @@ public interface ICreatedTime
2021
/// <summary>
2122
/// 获取或设置 创建时间
2223
/// </summary>
24+
[DisplayName("创建时间")]
2325
DateTime CreatedTime { get; set; }
2426
}
2527
}

src/OSharp/Entity/ISoftDeletable.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// -----------------------------------------------------------------------
99

1010
using System;
11+
using System.ComponentModel;
1112

1213

1314
namespace OSharp.Entity
@@ -20,6 +21,7 @@ public interface ISoftDeletable
2021
/// <summary>
2122
/// 获取或设置 数据逻辑删除时间,为null表示正常数据,有值表示已逻辑删除,同时删除时间每次不同也能保证索引唯一性
2223
/// </summary>
24+
[DisplayName("删除时间")]
2325
DateTime? DeletedTime { get; set; }
2426
}
2527
}

0 commit comments

Comments
 (0)