首页 新闻 搜索 专区 学院

EfCode 根据登陆时输入的租户信息实现动态过滤

0
悬赏园豆:10 [待解决问题]

/// <summary>
/// 实体
/// </summary>
public partial class ReadContext : BaseContext
{
private int _tenantId;
private int _accountId;

    public ReadContext (DbContextOptions options, ITenantProvider tenantProvider, IAccountProvider accountProvider)
        : base(options, tenantProvider)
    {
        _tenantId = tenantProvider.GetTenantId();
        _accountId = accountProvider.GetAccountId();
    }

 
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            // 添加软删除过滤
            if (typeof(ISoftDelete).IsAssignableFrom(entityType.ClrType))
                entityType.AddSoftDeleteQueryFilter();

            // 添加租户过滤
            if (typeof(IHasTenantId).IsAssignableFrom(entityType.ClrType))
                entityType.AddTenantQueryFilter(_tenantId);
        }
        base.OnModelCreating(modelBuilder);
    }

}

租户获取,登陆后是能获取得到的
/// <summary>
/// 租户提供者
/// </summary>
public class TenantProvider : ITenantProvider
{
private int _tenantId;
IHttpContextAccessor _accessor;

    public TenantProvider(IHttpContextAccessor accessor)
    {
        _accessor = accessor;
    }

    public int GetTenantId()
    {
        if (_accessor.HttpContext != null)
        {
            int.TryParse(_accessor.HttpContext.Request.Headers["TenantId"], out _tenantId);
            if (_tenantId == 0)
                int.TryParse(_accessor.HttpContext.User.Claims.FirstOrDefault(m => m.Type == ClaimsName.TenantId)?.Value, out _tenantId);
        }
        //if (_tenantId == 0) return 1;
        return _tenantId;
    }
}

过滤相关代码

    public static void AddTenantQueryFilter(this IMutableEntityType entityData, int tenantId)
    {
        var methodToCall = typeof(QueryFilterExtensions).GetMethod(nameof(GetTenantFilter), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(entityData.ClrType);
        var filter = methodToCall.Invoke(nameof(GetTenantFilter), new object[] { tenantId });
        entityData.SetQueryFilter((LambdaExpression)filter);
    }


    private static LambdaExpression GetTenantFilter<TEntity>(int tenantId) where TEntity : class, IHasTenantId
    {
        Expression<Func<TEntity, bool>> filter = x => x.TenantId == tenantId;
        return filter;
    }

现在的方式是在执行OnModelCreating的时候配置,但是貌似不能根据登陆时候租户进行过滤,我看了下abp的,也是类似于这样,但是不懂他那个具体做了什么处理。

noert的主页 noert | 初学一级 | 园豆:30
提问于:2020-11-19 15:20
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册