Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
T
travel-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
cloud
travel-cloud
Commits
807839f8
提交
807839f8
authored
11月 29, 2024
作者:
hzh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
租户权限助手功能实现
上级
97eb9226
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
102 行增加
和
0 行删除
+102
-0
TenantPermissionHelper.java
.../dromara/common/tenant/helper/TenantPermissionHelper.java
+102
-0
没有找到文件。
ruoyi-common/ruoyi-common-tenant/src/main/java/org/dromara/common/tenant/helper/TenantPermissionHelper.java
0 → 100644
浏览文件 @
807839f8
package
org
.
dromara
.
common
.
tenant
.
helper
;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
com.baomidou.mybatisplus.core.plugins.IgnoreStrategy
;
import
com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper
;
import
lombok.AccessLevel
;
import
lombok.NoArgsConstructor
;
import
org.dromara.common.core.utils.reflect.ReflectUtils
;
import
java.util.Stack
;
import
java.util.function.Supplier
;
/**
* 租户权限助手
*
* @author Lion Li
* @version 3.5.0
*/
@NoArgsConstructor
(
access
=
AccessLevel
.
PRIVATE
)
@SuppressWarnings
(
"unchecked cast"
)
public
class
TenantPermissionHelper
{
private
static
final
ThreadLocal
<
Stack
<
Integer
>>
REENTRANT_IGNORE
=
ThreadLocal
.
withInitial
(
Stack:
:
new
);
private
static
IgnoreStrategy
getIgnoreStrategy
()
{
Object
ignoreStrategyLocal
=
ReflectUtils
.
getStaticFieldValue
(
ReflectUtils
.
getField
(
InterceptorIgnoreHelper
.
class
,
"IGNORE_STRATEGY_LOCAL"
));
if
(
ignoreStrategyLocal
instanceof
ThreadLocal
<?>
IGNORE_STRATEGY_LOCAL
)
{
if
(
IGNORE_STRATEGY_LOCAL
.
get
()
instanceof
IgnoreStrategy
ignoreStrategy
)
{
return
ignoreStrategy
;
}
}
return
null
;
}
/**
* 开启忽略数据权限(开启后需手动调用 {@link #disableIgnore()} 关闭)
*/
public
static
void
enableIgnore
()
{
IgnoreStrategy
ignoreStrategy
=
getIgnoreStrategy
();
if
(
ObjectUtil
.
isNull
(
ignoreStrategy
))
{
InterceptorIgnoreHelper
.
handle
(
IgnoreStrategy
.
builder
().
tenantLine
(
true
).
build
());
}
else
{
ignoreStrategy
.
setTenantLine
(
true
);
}
Stack
<
Integer
>
reentrantStack
=
REENTRANT_IGNORE
.
get
();
reentrantStack
.
push
(
reentrantStack
.
size
()
+
1
);
}
/**
* 关闭忽略数据权限
*/
public
static
void
disableIgnore
()
{
IgnoreStrategy
ignoreStrategy
=
getIgnoreStrategy
();
if
(
ObjectUtil
.
isNotNull
(
ignoreStrategy
))
{
boolean
noOtherIgnoreStrategy
=
!
Boolean
.
TRUE
.
equals
(
ignoreStrategy
.
getDynamicTableName
())
&&
!
Boolean
.
TRUE
.
equals
(
ignoreStrategy
.
getBlockAttack
())
&&
!
Boolean
.
TRUE
.
equals
(
ignoreStrategy
.
getIllegalSql
())
&&
!
Boolean
.
TRUE
.
equals
(
ignoreStrategy
.
getTenantLine
())
&&
CollectionUtil
.
isEmpty
(
ignoreStrategy
.
getOthers
());
Stack
<
Integer
>
reentrantStack
=
REENTRANT_IGNORE
.
get
();
boolean
empty
=
reentrantStack
.
isEmpty
()
||
reentrantStack
.
pop
()
==
1
;
if
(
noOtherIgnoreStrategy
&&
empty
)
{
InterceptorIgnoreHelper
.
clearIgnoreStrategy
();
}
else
if
(
empty
)
{
ignoreStrategy
.
setTenantLine
(
false
);
}
}
}
/**
* 在忽略数据权限中执行
*
* @param handle 处理执行方法
*/
public
static
void
ignore
(
Runnable
handle
)
{
enableIgnore
();
try
{
handle
.
run
();
}
finally
{
disableIgnore
();
}
}
/**
* 在忽略数据权限中执行
*
* @param handle 处理执行方法
*/
public
static
<
T
>
T
ignore
(
Supplier
<
T
>
handle
)
{
enableIgnore
();
try
{
return
handle
.
get
();
}
finally
{
disableIgnore
();
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论