Commit e9ba4ac7053cd5d59651407887be51ac7c33a319
1 parent
c99115ab
初始化 DataPermission 的模型
Showing
4 changed files
with
83 additions
and
22 deletions
yudao-framework/yudao-spring-boot-starter-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/config/DataPermission.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.datapermission.config; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.datapermission.core.rule.DataPermissionRule; | |
| 4 | + | |
| 5 | +import java.lang.annotation.*; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * 数据权限注解 | |
| 9 | + * 可声明在类或者方法上,标识使用的数据权限规则 | |
| 10 | + * | |
| 11 | + * @author 芋道源码 | |
| 12 | + */ | |
| 13 | +@Target({ElementType.TYPE, ElementType.METHOD}) | |
| 14 | +@Retention(RetentionPolicy.RUNTIME) | |
| 15 | +@Documented | |
| 16 | +public @interface DataPermission { | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * 当前类或方法是否开启数据权限 | |
| 20 | + * 即使不添加 @DataPermission 注解,默认是开启状态 | |
| 21 | + * 可通过设置 enable 为 false 禁用 | |
| 22 | + */ | |
| 23 | + boolean enable() default true; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * 生效的数据权限规则数组,优先级高于 {@link #excludeRules()} | |
| 27 | + */ | |
| 28 | + Class<DataPermissionRule>[] includeRules() default {}; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * 排除的数据权限规则数组,优先级最低 | |
| 32 | + */ | |
| 33 | + Class<DataPermissionRule>[] excludeRules() default {}; | |
| 34 | + | |
| 35 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/interceptor/DataPermissionInterceptor.java
| ... | ... | @@ -24,7 +24,6 @@ import org.apache.ibatis.session.ResultHandler; |
| 24 | 24 | import org.apache.ibatis.session.RowBounds; |
| 25 | 25 | |
| 26 | 26 | import java.sql.Connection; |
| 27 | -import java.sql.SQLException; | |
| 28 | 27 | import java.util.Collection; |
| 29 | 28 | import java.util.Deque; |
| 30 | 29 | import java.util.LinkedList; |
| ... | ... | @@ -35,9 +34,11 @@ public class DataPermissionInterceptor extends JsqlParserSupport implements Inne |
| 35 | 34 | // private TenantLineHandler tenantLineHandler; |
| 36 | 35 | |
| 37 | 36 | @Override |
| 38 | - public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { | |
| 37 | + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { | |
| 38 | + // TODO 芋艿:这个判断,后续读懂下 | |
| 39 | 39 | if (InterceptorIgnoreHelper.willIgnoreTenantLine(ms.getId())) return; |
| 40 | 40 | PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); |
| 41 | + // TODO 芋艿:null=》DataScope | |
| 41 | 42 | mpBs.sql(parserSingle(mpBs.sql(), null)); |
| 42 | 43 | } |
| 43 | 44 | |
| ... | ... | @@ -124,20 +125,6 @@ public class DataPermissionInterceptor extends JsqlParserSupport implements Inne |
| 124 | 125 | } |
| 125 | 126 | |
| 126 | 127 | /** |
| 127 | - * 追加 SelectItem | |
| 128 | - * | |
| 129 | - * @param selectItems SelectItem | |
| 130 | - */ | |
| 131 | - protected void appendSelectItem(List<SelectItem> selectItems) { | |
| 132 | - if (CollectionUtils.isEmpty(selectItems)) return; | |
| 133 | - if (selectItems.size() == 1) { | |
| 134 | - SelectItem item = selectItems.get(0); | |
| 135 | - if (item instanceof AllColumns || item instanceof AllTableColumns) return; | |
| 136 | - } | |
| 137 | - selectItems.add(new SelectExpressionItem(new Column(getTenantIdColumn()))); | |
| 138 | - } | |
| 139 | - | |
| 140 | - /** | |
| 141 | 128 | * 处理 PlainSelect |
| 142 | 129 | */ |
| 143 | 130 | protected void processPlainSelect(PlainSelect plainSelect) { |
| ... | ... | @@ -377,12 +364,6 @@ public class DataPermissionInterceptor extends JsqlParserSupport implements Inne |
| 377 | 364 | return new Column(column.toString()); |
| 378 | 365 | } |
| 379 | 366 | |
| 380 | -// @Override | |
| 381 | -// public void setProperties(Properties properties) { | |
| 382 | -// PropertyMapper.newInstance(properties).whenNotBlank("tenantLineHandler", | |
| 383 | -// ClassUtils::newInstance, this::setTenantLineHandler); | |
| 384 | -// } | |
| 385 | - | |
| 386 | 367 | // TODO 芋艿:未实现 |
| 387 | 368 | |
| 388 | 369 | private boolean ignoreTable(String tableName) { | ... | ... |
yudao-framework/yudao-spring-boot-starter-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/DataPermissionRule.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.datapermission.core.rule; | |
| 2 | + | |
| 3 | +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | |
| 4 | +import net.sf.jsqlparser.expression.Alias; | |
| 5 | +import net.sf.jsqlparser.expression.Expression; | |
| 6 | + | |
| 7 | +import java.util.Set; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 数据权限规则接口 | |
| 11 | + * 通过实现接口,自定义数据规则。例如说, | |
| 12 | + * | |
| 13 | + * @author 芋道源码 | |
| 14 | + */ | |
| 15 | +public interface DataPermissionRule { | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * 返回需要生效的表名数组 | |
| 19 | + * 为什么需要该方法?Data Permission 数组基于 SQL 重写,通过 Where 返回只有权限的数据 | |
| 20 | + * | |
| 21 | + * 如果需要基于实体名获得表名,可调用 {@link TableInfoHelper#getTableInfo(Class)} 获得 | |
| 22 | + * | |
| 23 | + * @return 表名数组 | |
| 24 | + */ | |
| 25 | + Set<String> getTableNames(); | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * 根据表名和别名,生成对应的 WHERE / OR 过滤条件 | |
| 29 | + * | |
| 30 | + * @param tableName 表名 | |
| 31 | + * @param tableAlias 别名,可能为空 | |
| 32 | + * @return 过滤条件 Expression 表达式 | |
| 33 | + */ | |
| 34 | + Expression getExpression(String tableName, Alias tableAlias); | |
| 35 | + | |
| 36 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-data-permission/src/main/java/cn/iocoder/yudao/framework/datapermission/core/rule/DataPermissionRuleFactory.java
0 → 100644