feat(GlobalFilter): 添加过滤器类型支持并优化各操作过滤逻辑

为全局过滤器添加类型支持(查询、更新、删除),并修改相关操作(Select/Update/Delete)的过滤逻辑,确保只应用对应类型的过滤器。同时修复了RepositoryDbSet中过滤器的禁用逻辑,使其与操作类型匹配。
This commit is contained in:
廖宇杰
2025-07-07 02:39:54 +08:00
parent 1509714b74
commit a023662311
6 changed files with 38 additions and 26 deletions

View File

@@ -1,4 +1,5 @@
using FreeSql.Extensions.EntityUtil; using FreeSql.Extensions.EntityUtil;
using FreeSql.Internal;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -32,7 +33,9 @@ namespace FreeSql
{ {
var select = base.OrmSelect(dywhere); var select = base.OrmSelect(dywhere);
if (_repo._asTablePriv != null) select.AsTable(_repo._asTablePriv); if (_repo._asTablePriv != null) select.AsTable(_repo._asTablePriv);
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray(); var disableFilter = _repo.DataFilter._filtersByOrm
.Where(a => a.Value.IsEnabled == false || (a.Value.Filter.FilterType & GlobalFilter.FilterType.Query) != GlobalFilter.FilterType.Query)
.Select(a => a.Key).ToArray();
if (disableFilter.Any()) select.DisableGlobalFilter(disableFilter); if (disableFilter.Any()) select.DisableGlobalFilter(disableFilter);
return select; return select;
} }
@@ -41,7 +44,7 @@ namespace FreeSql
{ {
var update = base.OrmUpdate(entitys); var update = base.OrmUpdate(entitys);
if (_repo._asTablePriv != null) update.AsTable(old => _repo._asTablePriv(_entityType, old)); if (_repo._asTablePriv != null) update.AsTable(old => _repo._asTablePriv(_entityType, old));
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray(); var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false || (a.Value.Filter.FilterType & GlobalFilter.FilterType.Update) != GlobalFilter.FilterType.Update).Select(a => a.Key).ToArray();
if (disableFilter.Any()) update.DisableGlobalFilter(disableFilter); if (disableFilter.Any()) update.DisableGlobalFilter(disableFilter);
return update; return update;
} }
@@ -50,7 +53,7 @@ namespace FreeSql
{ {
var delete = base.OrmDelete(dywhere); var delete = base.OrmDelete(dywhere);
if (_repo._asTablePriv != null) delete.AsTable(old => _repo._asTablePriv(_entityType, old)); if (_repo._asTablePriv != null) delete.AsTable(old => _repo._asTablePriv(_entityType, old));
var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false).Select(a => a.Key).ToArray(); var disableFilter = _repo.DataFilter._filtersByOrm.Where(a => a.Value.IsEnabled == false || (a.Value.Filter.FilterType & GlobalFilter.FilterType.Delete) != GlobalFilter.FilterType.Delete).Select(a => a.Key).ToArray();
if (disableFilter.Any()) delete.DisableGlobalFilter(disableFilter); if (disableFilter.Any()) delete.DisableGlobalFilter(disableFilter);
return delete; return delete;
} }

View File

@@ -80,7 +80,6 @@ namespace FreeSql
get => _unitOfWork; get => _unitOfWork;
} }
public IUpdate<TEntity> UpdateDiy => _dbset.OrmUpdateInternal(null); public IUpdate<TEntity> UpdateDiy => _dbset.OrmUpdateInternal(null);
public virtual ISelect<TEntity> Select => _dbset.OrmSelectInternal(null); public virtual ISelect<TEntity> Select => _dbset.OrmSelectInternal(null);
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp); public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp); public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp);

View File

@@ -39,7 +39,7 @@ namespace FreeSql.Internal.CommonProvider
_isAutoSyncStructure = _orm.CodeFirst.IsAutoSyncStructure; _isAutoSyncStructure = _orm.CodeFirst.IsAutoSyncStructure;
this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params)); this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params));
if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>(); if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
_whereGlobalFilter = _orm.GlobalFilter.GetFilters(); _whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Delete) == GlobalFilter.FilterType.Delete).ToList();
} }
protected void ClearData() protected void ClearData()
@@ -47,7 +47,7 @@ namespace FreeSql.Internal.CommonProvider
_where.Clear(); _where.Clear();
_whereTimes = 0; _whereTimes = 0;
_params.Clear(); _params.Clear();
_whereGlobalFilter = _orm.GlobalFilter.GetFilters(); _whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Delete) == GlobalFilter.FilterType.Delete).ToList();
} }
public IDelete<T1> WithTransaction(DbTransaction transaction) public IDelete<T1> WithTransaction(DbTransaction transaction)

View File

@@ -22,7 +22,7 @@ namespace FreeSql.Internal.CommonProvider
{ {
public Select1Provider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere) public Select1Provider(IFreeSql orm, CommonUtils commonUtils, CommonExpression commonExpression, object dywhere) : base(orm, commonUtils, commonExpression, dywhere)
{ {
_whereGlobalFilter = _orm.GlobalFilter.GetFilters(); _whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Query) == GlobalFilter.FilterType.Query).ToList();
} }
protected ISelect<T1> InternalFrom(LambdaExpression lambdaExp) protected ISelect<T1> InternalFrom(LambdaExpression lambdaExp)

View File

@@ -184,7 +184,7 @@ namespace FreeSql.Internal.CommonProvider
this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params)); this.Where(_commonUtils.WhereObject(_table, "", dywhere, _params));
if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>(); if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
IgnoreCanUpdate(); IgnoreCanUpdate();
_whereGlobalFilter = _orm.GlobalFilter.GetFilters(); _whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Update) == GlobalFilter.FilterType.Update).ToList();
_sourceOld = _source; _sourceOld = _source;
} }
@@ -212,7 +212,7 @@ namespace FreeSql.Internal.CommonProvider
_params.Clear(); _params.Clear();
_paramsSource.Clear(); _paramsSource.Clear();
IgnoreCanUpdate(); IgnoreCanUpdate();
_whereGlobalFilter = _orm.GlobalFilter.GetFilters(); _whereGlobalFilter = _orm.GlobalFilter.GetFilters().Where(l => (l.FilterType & GlobalFilter.FilterType.Update) == GlobalFilter.FilterType.Update).ToList();
_batchProgress = null; _batchProgress = null;
_interceptSql = null; _interceptSql = null;
_tableAlias = null; _tableAlias = null;

View File

@@ -12,8 +12,17 @@ namespace FreeSql.Internal
ConcurrentDictionary<string, Item> _filters = new ConcurrentDictionary<string, Item>(StringComparer.CurrentCultureIgnoreCase); ConcurrentDictionary<string, Item> _filters = new ConcurrentDictionary<string, Item>(StringComparer.CurrentCultureIgnoreCase);
int _id = 0; int _id = 0;
[Flags]
public enum FilterType
{
Query = 1, // 2^0
Update = 2, // 2^1
Delete = 4 // 2^2
}
public class Item public class Item
{ {
public FilterType FilterType { get; set; }
public int Id { get; internal set; } public int Id { get; internal set; }
public string Name { get; internal set; } public string Name { get; internal set; }
internal Func<bool> Condition { get; set; } internal Func<bool> Condition { get; set; }
@@ -67,7 +76,7 @@ namespace FreeSql.Internal
/// <returns></returns> /// <returns></returns>
public GlobalFilter ApplyOnlyIf<TEntity>(string name, Func<bool> condition, Expression<Func<TEntity, bool>> where, bool before = false) => Apply(true, name, condition, where, before); public GlobalFilter ApplyOnlyIf<TEntity>(string name, Func<bool> condition, Expression<Func<TEntity, bool>> where, bool before = false) => Apply(true, name, condition, where, before);
GlobalFilter Apply<TEntity>(bool only, string name, Func<bool> condition, Expression<Func<TEntity, bool>> where, bool before) GlobalFilter Apply<TEntity>(bool only, string name, Func<bool> condition, Expression<Func<TEntity, bool>> where, bool before, FilterType filterType = FilterType.Query | FilterType.Update | FilterType.Delete)
{ {
if (name == null) throw new ArgumentNullException(nameof(name)); if (name == null) throw new ArgumentNullException(nameof(name));
if (where == null) return this; if (where == null) return this;
@@ -84,6 +93,7 @@ namespace FreeSql.Internal
item.Condition = condition; item.Condition = condition;
item.Only = only; item.Only = only;
item.Before = before; item.Before = before;
item.FilterType = filterType;
_filters.AddOrUpdate(name, item, (_, __) => item); _filters.AddOrUpdate(name, item, (_, __) => item);
return this; return this;
} }