-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
【代码优化】全局:复用 MyBatis Plus 数据权限插件,简化项目的数据权限实现
- Loading branch information
Showing
5 changed files
with
77 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
297 changes: 0 additions & 297 deletions
297
.../cn/iocoder/yudao/framework/datapermission/core/db/DataPermissionDatabaseInterceptor.java
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
...ain/java/cn/iocoder/yudao/framework/datapermission/core/db/DataPermissionRuleHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cn.iocoder.yudao.framework.datapermission.core.db; | ||
|
||
import cn.hutool.core.collection.CollUtil; | ||
import cn.iocoder.yudao.framework.datapermission.core.rule.DataPermissionRule; | ||
import cn.iocoder.yudao.framework.datapermission.core.rule.DataPermissionRuleFactory; | ||
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; | ||
import com.baomidou.mybatisplus.extension.plugins.handler.MultiDataPermissionHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import net.sf.jsqlparser.expression.Expression; | ||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression; | ||
import net.sf.jsqlparser.schema.Table; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 基于 {@link DataPermissionRule} 的数据权限处理器 | ||
* | ||
* 它的底层,是基于 MyBatis Plus 的 <a href="https://baomidou.com/plugins/data-permission/">数据权限插件</a> | ||
* 核心原理:它会在 SQL 执行前拦截 SQL 语句,并根据用户权限动态添加权限相关的 SQL 片段。这样,只有用户有权限访问的数据才会被查询出来 | ||
* | ||
* @author 芋道源码 | ||
*/ | ||
@RequiredArgsConstructor | ||
public class DataPermissionRuleHandler implements MultiDataPermissionHandler { | ||
|
||
private final DataPermissionRuleFactory ruleFactory; | ||
|
||
@Override | ||
public Expression getSqlSegment(Table table, Expression where, String mappedStatementId) { | ||
// 获得 Mapper 对应的数据权限的规则 | ||
List<DataPermissionRule> rules = ruleFactory.getDataPermissionRule(mappedStatementId); | ||
if (CollUtil.isEmpty(rules)) { | ||
return null; | ||
} | ||
|
||
// 生成条件 | ||
Expression allExpression = null; | ||
for (DataPermissionRule rule : rules) { | ||
// 判断表名是否匹配 | ||
String tableName = MyBatisUtils.getTableName(table); | ||
if (!rule.getTableNames().contains(tableName)) { | ||
continue; | ||
} | ||
|
||
// 单条规则的条件 | ||
Expression oneExpress = rule.getExpression(tableName, table.getAlias()); | ||
if (oneExpress == null) { | ||
continue; | ||
} | ||
// 拼接到 allExpression 中 | ||
allExpression = allExpression == null ? oneExpress | ||
: new AndExpression(allExpression, oneExpress); | ||
} | ||
return allExpression; | ||
} | ||
|
||
} |
Oops, something went wrong.