😎增加自定义数据审计

This commit is contained in:
zuohuaijun 2024-11-25 21:18:11 +08:00
parent 1b6c00cfa1
commit bbd5100ca4
3 changed files with 59 additions and 6 deletions

View File

@ -0,0 +1,50 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
/// <summary>
/// 自定义数据审计
/// </summary>
public class SqlSugarDataExecuting
{
/// <summary>
/// 缓存数据过滤(内存缓存)
/// </summary>
private static readonly ICache _cache = Cache.Default;
public static void Execute(object oldData, DataFilterModel model)
{
var cacheKey = "db:SqlSugarDataExecuting";
var items = _cache.Get<List<IDataExecuting>>(cacheKey);
if (items == null)
{
items = new List<IDataExecuting>();
// 获取自定义实体过滤器
var ides = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(IDataExecuting))));
foreach (var ide in ides)
{
var instance = Activator.CreateInstance(ide);
items.Add((IDataExecuting)instance);
}
_cache.Add(cacheKey, items);
}
foreach (var item in items.Where(u => u.EntityNames != null && u.EntityNames.Contains(model.EntityValue.GetType())))
{
item.Execute(oldData, model);
}
}
}
public interface IDataExecuting
{
List<Type> EntityNames { get; }
void Execute(object oldData, DataFilterModel model);
}

View File

@ -170,7 +170,7 @@ public static class SqlSugarSetup
// 执行时间超过5秒时 // 执行时间超过5秒时
if (db.Ado.SqlExecutionTime.TotalSeconds <= 5) return; if (db.Ado.SqlExecutionTime.TotalSeconds <= 5) return;
var fileName = db.Ado.SqlStackTrace.FirstFileName; // 文件名 var fileName = db.Ado.SqlStackTrace.FirstFileName; // 文件名
var fileLine = db.Ado.SqlStackTrace.FirstLine; // 行号 var fileLine = db.Ado.SqlStackTrace.FirstLine; // 行号
var firstMethodName = db.Ado.SqlStackTrace.FirstMethodName; // 方法名 var firstMethodName = db.Ado.SqlStackTrace.FirstMethodName; // 方法名
@ -180,7 +180,7 @@ public static class SqlSugarSetup
}; };
} }
// 数据审计 // 数据审计
db.Aop.DataExecuting = (_, entityInfo) => db.Aop.DataExecuting = (oldValue, entityInfo) =>
{ {
// 若正在处理种子数据则直接返回 // 若正在处理种子数据则直接返回
if (_isHandlingSeedData) return; if (_isHandlingSeedData) return;
@ -204,7 +204,7 @@ public static class SqlSugarSetup
} }
// 若当前用户非空web线程时 // 若当前用户非空web线程时
if (App.User == null) return; if (App.User == null) return;
dynamic entityValue = entityInfo.EntityValue; dynamic entityValue = entityInfo.EntityValue;
if (entityInfo.PropertyName == nameof(EntityTenantId.TenantId)) if (entityInfo.PropertyName == nameof(EntityTenantId.TenantId))
{ {
@ -242,6 +242,9 @@ public static class SqlSugarSetup
else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserName)) else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserName))
entityInfo.SetValue(App.User?.FindFirst(ClaimConst.RealName)?.Value); entityInfo.SetValue(App.User?.FindFirst(ClaimConst.RealName)?.Value);
} }
//// 自定义数据审计
//SqlSugarDataExecuting.Execute(oldValue, entityInfo);
}; };
// 超管排除其他过滤器 // 超管排除其他过滤器
@ -282,7 +285,7 @@ public static class SqlSugarSetup
for (int j = 0; j < afterColumns.Count; j++) for (int j = 0; j < afterColumns.Count; j++)
{ {
if (!afterColumns[j].Value.Equals(beforeColumns[j].Value)) continue; if (!afterColumns[j].Value.Equals(beforeColumns[j].Value)) continue;
beforeColumns.Remove(beforeColumns[j]); beforeColumns.Remove(beforeColumns[j]);
afterColumns.Remove(afterColumns[j]); afterColumns.Remove(afterColumns[j]);
j--; j--;
@ -445,7 +448,7 @@ public static class SqlSugarSetup
} }
} }
_isHandlingSeedData = false; _isHandlingSeedData = false;
} }
/// <summary> /// <summary>

View File

@ -74,7 +74,7 @@
"vue-router": "^4.4.5", "vue-router": "^4.4.5",
"vue-signature-pad": "^3.0.2", "vue-signature-pad": "^3.0.2",
"vue3-tree-org": "^4.2.2", "vue3-tree-org": "^4.2.2",
"vxe-pc-ui": "^4.3.5", "vxe-pc-ui": "^4.3.6",
"vxe-table": "^4.8.10", "vxe-table": "^4.8.10",
"vxe-table-plugin-element": "^4.0.4", "vxe-table-plugin-element": "^4.0.4",
"vxe-table-plugin-export-xlsx": "^4.0.7", "vxe-table-plugin-export-xlsx": "^4.0.7",