119 lines
4.0 KiB
C#
119 lines
4.0 KiB
C#
|
|
using Hncore.Infrastructure.Common;
|
|||
|
|
using Hncore.Infrastructure.DDD;
|
|||
|
|
using Hncore.Infrastructure.Extension;
|
|||
|
|
using Hncore.Infrastructure.Serializer;
|
|||
|
|
using Hncore.Infrastructure.WebApi;
|
|||
|
|
using Microsoft.AspNetCore.Http;
|
|||
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using System;
|
|||
|
|
using System.Linq;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace Hncore.Infrastructure.EF
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 上下文构造器的基类
|
|||
|
|
/// </summary>
|
|||
|
|
public class DbContextBase : DbContext
|
|||
|
|
{
|
|||
|
|
private IHttpContextAccessor _httpContextAccessor;
|
|||
|
|
|
|||
|
|
private bool _enabledLog = false;
|
|||
|
|
|
|||
|
|
private int _tenantid = 0;
|
|||
|
|
|
|||
|
|
private int _storeId = 0;
|
|||
|
|
|
|||
|
|
private bool _root = false;
|
|||
|
|
|
|||
|
|
private bool _allow = false;
|
|||
|
|
|
|||
|
|
public DbContextBase(DbContextOptions options, IHttpContextAccessor httpContextAccessor) : base(options)
|
|||
|
|
{
|
|||
|
|
_httpContextAccessor = httpContextAccessor;
|
|||
|
|
|
|||
|
|
if (UseTenantFilter())
|
|||
|
|
{
|
|||
|
|
ManageUserInfo manageUserInfo = _httpContextAccessor.HttpContext.Request.GetManageUserInfo();
|
|||
|
|
|
|||
|
|
if (manageUserInfo != null)
|
|||
|
|
{
|
|||
|
|
_tenantid = manageUserInfo.TenantId;
|
|||
|
|
_storeId = manageUserInfo.StoreId;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_allow = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private bool UseTenantFilter()
|
|||
|
|
{
|
|||
|
|
if (_httpContextAccessor == null || _httpContextAccessor.HttpContext == null)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return _httpContextAccessor.HttpContext.Items.ContainsKey("AuthPassedFilterName")
|
|||
|
|
&& _httpContextAccessor.HttpContext.Items["AuthPassedFilterName"].ToString() == "ManageAuth";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// model构造器 创建实体映射
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="modelBuilder"></param>
|
|||
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|||
|
|
{
|
|||
|
|
base.OnModelCreating(modelBuilder);
|
|||
|
|
|
|||
|
|
if (!EnvironmentVariableHelper.IsAspNetCoreProduction)
|
|||
|
|
{
|
|||
|
|
LogHelper.Debug("进入DbContextBase的OnModelCreating函数",
|
|||
|
|
$"UseGlobalManageAuthFilter:{GlobalData.UseGlobalManageAuthFilter}\ntoken:{_httpContextAccessor?.HttpContext?.Request?.GetManageUserInfo()?.ToJson(true)}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
foreach (var type in modelBuilder.Model.GetEntityTypes())
|
|||
|
|
{
|
|||
|
|
if (typeof(ISoftDelete).IsAssignableFrom(type.ClrType))
|
|||
|
|
{
|
|||
|
|
modelBuilder.Entity(type.ClrType).AddQueryFilter<ISoftDelete>(t => t.DeleteTag == 0);
|
|||
|
|
}
|
|||
|
|
//if (typeof(ITenant).IsAssignableFrom(type.ClrType))
|
|||
|
|
//{
|
|||
|
|
// modelBuilder.Entity(type.ClrType).AddQueryFilter<ITenant>(t => _allow || t.TenantId == _tenantid);
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
//if (typeof(ITenantStore).IsAssignableFrom(type.ClrType))
|
|||
|
|
//{
|
|||
|
|
// modelBuilder.Entity(type.ClrType)
|
|||
|
|
// .AddQueryFilter<ITenantStore>(t => _storeId == 0|| t.StoreId==_storeId);
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|||
|
|
{
|
|||
|
|
if (_httpContextAccessor?.HttpContext?.Request?.Headers != null)
|
|||
|
|
{
|
|||
|
|
if (_httpContextAccessor.HttpContext.Request.Headers.ContainsKey("enable-ef-log"))
|
|||
|
|
{
|
|||
|
|
if (_httpContextAccessor.HttpContext.Request.Headers.ContainsKey("enable-ef-log").ToBool())
|
|||
|
|
{
|
|||
|
|
if (_enabledLog == false)
|
|||
|
|
{
|
|||
|
|
optionsBuilder.EnableDebugTrace(_httpContextAccessor);
|
|||
|
|
_enabledLog = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#if DEBUG
|
|||
|
|
Console.WriteLine("当前为debug模式,开启EF DebugTrace");
|
|||
|
|
optionsBuilder.EnableDebugTrace(null);
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|