Skip to content

租户拦截过滤器缺少查询相关 #94

Description

@nygula

TenantInterceptor 只有 OnCreate/OnValid(,漏了
OnQuery——这正是查询不自动过滤的原因。补上后,任何注册了该拦截器的实体,查询都自动追加 TenantId 条件。
建议在其中增加如下代码
`

///

查询时自动附加租户过滤条件,无需在业务代码中手动调用 ApplyTenant
/// 实体工厂
/// 查询条件表达式
/// 查询操作来源
/// 修改后的查询条件,已合并租户过滤
protected override XCode.Expression? OnQuery(IEntityFactory factory, XCode.Expression? where, QueryAction action)
{
// 已由 OnInit 检查实体是否实现 ITenantScope;此处通过工厂扩展方法统一追加租户过滤
// where 为 null 时同样需要合并租户条件,因此构造一个空 WhereExpression 后调用 ApplyTenant
// 注意:ApplyTenant 仅接受 WhereExpression,故这里用 & 运算符把任意 Expression 归一到 WhereExpression 并保留原条件
return (where & XCode.Expression.Empty).ApplyTenant(factory);
}

`

然后用户还需要再Biz.cs代码中,1.继承ITenantScope 2.增加 Meta.Modules.Add();

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions