未启用多租户时,不要操作租户相关表

This commit is contained in:
大石头 2024-04-03 22:47:15 +08:00
parent 9df6dfaa35
commit 231a8b413f
5 changed files with 51 additions and 28 deletions

View File

@ -218,18 +218,32 @@ public class ReadOnlyEntityController<TEntity> : ControllerBaseX where TEntity :
}
// 多租户
var ctxTenant = TenantContext.Current;
if (ctxTenant != null && IsTenantSource)
var set = CubeSetting.Current;
if (set.EnableTenant)
{
var tenant = Tenant.FindById(ctxTenant.TenantId);
if (tenant != null)
var ctxTenant = TenantContext.Current;
if (ctxTenant != null && IsTenantSource)
{
HttpContext.Items["TenantId"] = tenant.Id;
var tenant = Tenant.FindById(ctxTenant.TenantId);
if (tenant != null)
{
HttpContext.Items["TenantId"] = tenant.Id;
if (!exp.IsNullOrEmpty())
exp = "TenantId={#TenantId} and " + exp;
else
exp = "TenantId={#TenantId}";
if (typeof(TEntity) == typeof(Tenant))
{
if (!exp.IsNullOrEmpty())
exp = "Id={#TenantId} and " + exp;
else
exp = "Id={#TenantId}";
}
else
{
if (!exp.IsNullOrEmpty())
exp = "TenantId={#TenantId} and " + exp;
else
exp = "TenantId={#TenantId}";
}
}
}
}

View File

@ -47,8 +47,9 @@ public class IndexController : ControllerBaseX
ViewBag.Config = SysConfig.Current;
//!!! 租户切换
var set = CubeSetting.Current;
var tenantId = Request.Query["TenantId"].ToInt(-1);
if (tenantId >= 0)
if (tenantId >= 0 && set.EnableTenant)
{
// 判断租户关系
var list = TenantUser.FindAllByUserId(user.ID);

View File

@ -232,27 +232,31 @@ public class ReadOnlyEntityController<TEntity> : ControllerBaseX where TEntity :
}
// 多租户
var ctxTenant = TenantContext.Current;
if (ctxTenant != null && IsTenantSource)
var set = CubeSetting.Current;
if (set.EnableTenant)
{
var tenant = Tenant.FindById(ctxTenant.TenantId);
if (tenant != null)
var ctxTenant = TenantContext.Current;
if (ctxTenant != null && IsTenantSource)
{
HttpContext.Items["TenantId"] = tenant.Id;
var tenant = Tenant.FindById(ctxTenant.TenantId);
if (tenant != null)
{
HttpContext.Items["TenantId"] = tenant.Id;
if (typeof(TEntity) == typeof(Tenant))
{
if (!exp.IsNullOrEmpty())
exp = "Id={#TenantId} and " + exp;
if (typeof(TEntity) == typeof(Tenant))
{
if (!exp.IsNullOrEmpty())
exp = "Id={#TenantId} and " + exp;
else
exp = "Id={#TenantId}";
}
else
exp = "Id={#TenantId}";
}
else
{
if (!exp.IsNullOrEmpty())
exp = "TenantId={#TenantId} and " + exp;
else
exp = "TenantId={#TenantId}";
{
if (!exp.IsNullOrEmpty())
exp = "TenantId={#TenantId} and " + exp;
else
exp = "TenantId={#TenantId}";
}
}
}
}

View File

@ -73,6 +73,9 @@ public static class ManagerProviderHelper
/// <param name="userId"></param>
public static void ChooseTenant(this HttpContext context, Int32 userId)
{
var set = CubeSetting.Current;
if (!set.EnableTenant) return;
/*
*
*

View File

@ -21,7 +21,8 @@ public class TenantMiddleware
var changed = false;
try
{
if (TenantContext.Current == null)
var set = CubeSetting.Current;
if (set.EnableTenant && TenantContext.Current == null)
{
var tenantId = ctx.GetTenantId();
if (tenantId > 0)