+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springblade.core.boot.tenant;
+
+import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
+import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.ToString;
+
+/**
+ * 租户拦截器
+ *
+ * @author Chill
+ */
+@Data
+@ToString(callSuper = true)
+@EqualsAndHashCode(callSuper = true)
+public class BladeTenantInterceptor extends TenantLineInnerInterceptor {
+
+ /**
+ * 租户处理器
+ */
+ private TenantLineHandler tenantLineHandler;
+
+ @Override
+ public void setTenantLineHandler(TenantLineHandler tenantLineHandler) {
+ super.setTenantLineHandler(tenantLineHandler);
+ this.tenantLineHandler = tenantLineHandler;
+ }
+
+}
diff --git a/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/TenantConfiguration.java b/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/TenantConfiguration.java
index 7693888..32831fe 100644
--- a/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/TenantConfiguration.java
+++ b/blade-core-boot/src/main/java/org/springblade/core/boot/tenant/TenantConfiguration.java
@@ -16,13 +16,15 @@
package org.springblade.core.boot.tenant;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
+import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import lombok.AllArgsConstructor;
-import org.springblade.core.boot.config.MybatisPlusConfiguration;
+import org.springblade.core.mp.config.MybatisPlusConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
/**
* 多租户配置类
@@ -36,19 +38,29 @@ import org.springframework.context.annotation.Configuration;
public class TenantConfiguration {
/**
- * 多租户配置类
- */
- private final BladeTenantProperties properties;
-
- /**
- * 自定义租户处理器
+ * 自定义多租户处理器
*
+ * @param tenantProperties 多租户配置类
* @return TenantHandler
*/
@Bean
- @ConditionalOnMissingBean(TenantLineHandler.class)
- public TenantLineHandler bladeTenantHandler() {
- return new BladeTenantHandler(properties);
+ @Primary
+ public TenantLineHandler bladeTenantHandler(BladeTenantProperties tenantProperties) {
+ return new BladeTenantHandler(tenantProperties);
+ }
+
+ /**
+ * 自定义租户拦截器
+ *
+ * @param tenantHandler 多租户处理器
+ * @return BladeTenantInterceptor
+ */
+ @Bean
+ @Primary
+ public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantLineHandler tenantHandler) {
+ BladeTenantInterceptor tenantInterceptor = new BladeTenantInterceptor();
+ tenantInterceptor.setTenantLineHandler(tenantHandler);
+ return tenantInterceptor;
}
/**
diff --git a/blade-core-boot/src/main/resources/bootstrap.yml b/blade-core-boot/src/main/resources/bootstrap.yml
index 86a5f48..1fbd7e4 100644
--- a/blade-core-boot/src/main/resources/bootstrap.yml
+++ b/blade-core-boot/src/main/resources/bootstrap.yml
@@ -101,7 +101,7 @@ mybatis-plus:
swagger:
title: SpringBlade 接口文档系统
description: SpringBlade 接口文档系统
- version: 3.1.0
+ version: 3.2.0
license: Powered By SpringBlade
licenseUrl: https://bladex.vip
terms-of-service-url: https://bladex.vip
diff --git a/blade-core-cloud/pom.xml b/blade-core-cloud/pom.xml
index 04851dd..c22e16f 100644
--- a/blade-core-cloud/pom.xml
+++ b/blade-core-cloud/pom.xml
@@ -5,7 +5,7 @@
blade-toolorg.springblade
- 3.1.0
+ 3.2.04.0.0
diff --git a/blade-core-datascope/pom.xml b/blade-core-datascope/pom.xml
new file mode 100644
index 0000000..79524fc
--- /dev/null
+++ b/blade-core-datascope/pom.xml
@@ -0,0 +1,37 @@
+
+
+
+ blade-tool
+ org.springblade
+ 3.2.0
+
+ 4.0.0
+
+ blade-core-datascope
+ ${project.artifactId}
+ ${blade.tool.version}
+ jar
+
+
+
+
+ org.springblade
+ blade-core-mybatis
+ ${blade.tool.version}
+
+
+
+ org.springframework.boot
+ spring-boot-starter-jdbc
+
+
+ tomcat-jdbc
+ org.apache.tomcat
+
+
+
+
+
+
diff --git a/blade-core-datascope/src/main/java/org/springblade/core/datascope/annotation/DataAuth.java b/blade-core-datascope/src/main/java/org/springblade/core/datascope/annotation/DataAuth.java
new file mode 100644
index 0000000..d0da7c5
--- /dev/null
+++ b/blade-core-datascope/src/main/java/org/springblade/core/datascope/annotation/DataAuth.java
@@ -0,0 +1,60 @@
+/**
+ * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springblade.core.datascope.annotation;
+
+import org.springblade.core.datascope.constant.DataScopeConstant;
+import org.springblade.core.datascope.enums.DataScopeEnum;
+
+import java.lang.annotation.*;
+
+/**
+ * 数据权限定义
+ *
+ * @author Chill
+ */
+@Target({ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+@Documented
+public @interface DataAuth {
+
+ /**
+ * 资源编号
+ */
+ String code() default "";
+
+ /**
+ * 数据权限对应字段
+ */
+ String column() default DataScopeConstant.DEFAULT_COLUMN;
+
+ /**
+ * 数据权限规则
+ */
+ DataScopeEnum type() default DataScopeEnum.ALL;
+
+ /**
+ * 可见字段
+ */
+ String field() default "*";
+
+ /**
+ * 数据权限规则值域
+ */
+ String value() default "";
+
+}
+
diff --git a/blade-core-datascope/src/main/java/org/springblade/core/datascope/config/DataScopeConfiguration.java b/blade-core-datascope/src/main/java/org/springblade/core/datascope/config/DataScopeConfiguration.java
new file mode 100644
index 0000000..f6a73ab
--- /dev/null
+++ b/blade-core-datascope/src/main/java/org/springblade/core/datascope/config/DataScopeConfiguration.java
@@ -0,0 +1,64 @@
+/**
+ * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springblade.core.datascope.config;
+
+import lombok.AllArgsConstructor;
+import org.springblade.core.datascope.handler.BladeDataScopeHandler;
+import org.springblade.core.datascope.handler.BladeScopeModelHandler;
+import org.springblade.core.datascope.handler.DataScopeHandler;
+import org.springblade.core.datascope.handler.ScopeModelHandler;
+import org.springblade.core.datascope.interceptor.DataScopeInterceptor;
+import org.springblade.core.datascope.props.DataScopeProperties;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+/**
+ * 数据权限配置类
+ *
+ * @author Chill
+ */
+@Configuration(proxyBeanMethods = false)
+@AllArgsConstructor
+@EnableConfigurationProperties(DataScopeProperties.class)
+public class DataScopeConfiguration {
+
+ private final JdbcTemplate jdbcTemplate;
+
+ @Bean
+ @ConditionalOnMissingBean(ScopeModelHandler.class)
+ public ScopeModelHandler scopeModelHandler() {
+ return new BladeScopeModelHandler(jdbcTemplate);
+ }
+
+ @Bean
+ @ConditionalOnBean(ScopeModelHandler.class)
+ @ConditionalOnMissingBean(DataScopeHandler.class)
+ public DataScopeHandler dataScopeHandler(ScopeModelHandler scopeModelHandler) {
+ return new BladeDataScopeHandler(scopeModelHandler);
+ }
+
+ @Bean
+ @ConditionalOnBean(DataScopeHandler.class)
+ @ConditionalOnMissingBean(DataScopeInterceptor.class)
+ public DataScopeInterceptor interceptor(DataScopeHandler dataScopeHandler, DataScopeProperties dataScopeProperties) {
+ return new DataScopeInterceptor(dataScopeHandler, dataScopeProperties);
+ }
+
+}
diff --git a/blade-core-datascope/src/main/java/org/springblade/core/datascope/constant/DataScopeConstant.java b/blade-core-datascope/src/main/java/org/springblade/core/datascope/constant/DataScopeConstant.java
new file mode 100644
index 0000000..5631dd5
--- /dev/null
+++ b/blade-core-datascope/src/main/java/org/springblade/core/datascope/constant/DataScopeConstant.java
@@ -0,0 +1,64 @@
+/**
+ * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springblade.core.datascope.constant;
+
+import org.springblade.core.tool.utils.StringUtil;
+
+/**
+ * 数据权限常量
+ *
+ * @author Chill
+ */
+public interface DataScopeConstant {
+
+ String DEFAULT_COLUMN = "create_dept";
+
+ /**
+ * 获取部门数据
+ */
+ String DATA_BY_DEPT = "select id from blade_dept where ancestors like concat(concat('%', ?),'%') and is_deleted = 0";
+
+ /**
+ * 根据resourceCode获取数据权限配置
+ */
+ String DATA_BY_CODE = "select resource_code, scope_column, scope_field, scope_type, scope_value from blade_scope_data where resource_code = ?";
+
+ /**
+ * 根据mapperId获取数据权限配置
+ *
+ * @param size 数量
+ * @return String
+ */
+ static String dataByMapper(int size) {
+ return "select resource_code, scope_column, scope_field, scope_type, scope_value from blade_scope_data where scope_class = ? and id in (select scope_id from blade_role_scope where role_id in (" + buildHolder(size) + "))";
+ }
+
+ /**
+ * 获取Sql占位符
+ *
+ * @param size 数量
+ * @return String
+ */
+ static String buildHolder(int size) {
+ StringBuilder builder = StringUtil.builder();
+ for (int i = 0; i < size; i++) {
+ builder.append("?,");
+ }
+ return StringUtil.removeSuffix(builder.toString(), ",");
+ }
+
+
+}
diff --git a/blade-core-datascope/src/main/java/org/springblade/core/datascope/enums/DataScopeEnum.java b/blade-core-datascope/src/main/java/org/springblade/core/datascope/enums/DataScopeEnum.java
new file mode 100644
index 0000000..4931ec0
--- /dev/null
+++ b/blade-core-datascope/src/main/java/org/springblade/core/datascope/enums/DataScopeEnum.java
@@ -0,0 +1,75 @@
+/**
+ * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springblade.core.datascope.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 数据权限类型
+ *
+ * @author lengleng, Chill
+ */
+@Getter
+@AllArgsConstructor
+public enum DataScopeEnum {
+ /**
+ * 全部数据
+ */
+ ALL(1, "全部"),
+
+ /**
+ * 本人可见
+ */
+ OWN(2, "本人可见"),
+
+ /**
+ * 所在机构可见
+ */
+ OWN_DEPT(3, "所在机构可见"),
+
+ /**
+ * 所在机构及子级可见
+ */
+ OWN_DEPT_CHILD(4, "所在机构及子级可见"),
+
+ /**
+ * 自定义
+ */
+ CUSTOM(5, "自定义");
+
+ /**
+ * 类型
+ */
+ private final int type;
+ /**
+ * 描述
+ */
+ private final String description;
+
+ public static DataScopeEnum of(Integer dataScopeType) {
+ if (dataScopeType == null) {
+ return null;
+ }
+ DataScopeEnum[] values = DataScopeEnum.values();
+ for (DataScopeEnum scopeTypeEnum : values) {
+ if (scopeTypeEnum.type == dataScopeType) {
+ return scopeTypeEnum;
+ }
+ }
+ return null;
+ }
+}
diff --git a/blade-core-datascope/src/main/java/org/springblade/core/datascope/exception/DataScopeException.java b/blade-core-datascope/src/main/java/org/springblade/core/datascope/exception/DataScopeException.java
new file mode 100644
index 0000000..bf95845
--- /dev/null
+++ b/blade-core-datascope/src/main/java/org/springblade/core/datascope/exception/DataScopeException.java
@@ -0,0 +1,35 @@
+/**
+ * Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springblade.core.datascope.exception;
+
+/**
+ * 数据权限异常
+ *
+ * @author L.cm
+ */
+public class DataScopeException extends RuntimeException {
+
+ public DataScopeException() {
+ }
+
+ public DataScopeException(String message) {
+ super(message);
+ }
+
+ public DataScopeException(Throwable cause) {
+ super(cause);
+ }
+}
diff --git a/blade-core-datascope/src/main/java/org/springblade/core/datascope/handler/BladeDataScopeHandler.java b/blade-core-datascope/src/main/java/org/springblade/core/datascope/handler/BladeDataScopeHandler.java
new file mode 100644
index 0000000..9ae41c2
--- /dev/null
+++ b/blade-core-datascope/src/main/java/org/springblade/core/datascope/handler/BladeDataScopeHandler.java
@@ -0,0 +1,83 @@
+/**
+ * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springblade.core.datascope.handler;
+
+import lombok.RequiredArgsConstructor;
+import org.springblade.core.datascope.enums.DataScopeEnum;
+import org.springblade.core.datascope.model.DataScopeModel;
+import org.springblade.core.secure.BladeUser;
+import org.springblade.core.tool.constant.RoleConstant;
+import org.springblade.core.tool.utils.BeanUtil;
+import org.springblade.core.tool.utils.Func;
+import org.springblade.core.tool.utils.PlaceholderUtil;
+import org.springblade.core.tool.utils.StringUtil;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * 默认数据权限规则
+ *
+ * @author Chill
+ */
+@RequiredArgsConstructor
+public class BladeDataScopeHandler implements DataScopeHandler {
+
+ private final ScopeModelHandler scopeModelHandler;
+
+ @Override
+ public String sqlCondition(String mapperId, DataScopeModel dataScope, BladeUser bladeUser, String originalSql) {
+
+ //数据权限资源编号
+ String code = dataScope.getResourceCode();
+
+ //根据mapperId从数据库中获取对应模型
+ DataScopeModel dataScopeDb = scopeModelHandler.getDataScopeByMapper(mapperId, bladeUser.getRoleId());
+
+ //mapperId配置未取到则从数据库中根据资源编号获取
+ if (dataScopeDb == null && StringUtil.isNotBlank(code)) {
+ dataScopeDb = scopeModelHandler.getDataScopeByCode(code);
+ }
+
+ //未从数据库找到对应配置则采用默认
+ dataScope = (dataScopeDb != null) ? dataScopeDb : dataScope;
+
+ //判断数据权限类型并组装对应Sql
+ Integer scopeRule = Objects.requireNonNull(dataScope).getScopeType();
+ DataScopeEnum scopeTypeEnum = DataScopeEnum.of(scopeRule);
+ List ids = new ArrayList<>();
+ String whereSql = "where scope.{} in ({})";
+ if (DataScopeEnum.ALL == scopeTypeEnum || StringUtil.containsAny(bladeUser.getRoleName(), RoleConstant.ADMIN)) {
+ return null;
+ } else if (DataScopeEnum.CUSTOM == scopeTypeEnum) {
+ whereSql = PlaceholderUtil.getDefaultResolver().resolveByMap(dataScope.getScopeValue(), BeanUtil.toMap(bladeUser));
+ } else if (DataScopeEnum.OWN == scopeTypeEnum) {
+ ids.add(bladeUser.getUserId());
+ } else if (DataScopeEnum.OWN_DEPT == scopeTypeEnum) {
+ ids.addAll(Func.toLongList(bladeUser.getDeptId()));
+ } else if (DataScopeEnum.OWN_DEPT_CHILD == scopeTypeEnum) {
+ List deptIds = Func.toLongList(bladeUser.getDeptId());
+ ids.addAll(deptIds);
+ deptIds.forEach(deptId -> {
+ List deptIdList = scopeModelHandler.getDeptAncestors(deptId);
+ ids.addAll(deptIdList);
+ });
+ }
+ return StringUtil.format("select {} from ({}) scope " + whereSql, Func.toStr(dataScope.getScopeField(), "*"), originalSql, dataScope.getScopeColumn(), StringUtil.join(ids));
+ }
+
+}
diff --git a/blade-core-datascope/src/main/java/org/springblade/core/datascope/handler/BladeScopeModelHandler.java b/blade-core-datascope/src/main/java/org/springblade/core/datascope/handler/BladeScopeModelHandler.java
new file mode 100644
index 0000000..8666432
--- /dev/null
+++ b/blade-core-datascope/src/main/java/org/springblade/core/datascope/handler/BladeScopeModelHandler.java
@@ -0,0 +1,114 @@
+/**
+ * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springblade.core.datascope.handler;
+
+import lombok.RequiredArgsConstructor;
+import org.springblade.core.datascope.constant.DataScopeConstant;
+import org.springblade.core.datascope.model.DataScopeModel;
+import org.springblade.core.tool.utils.*;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.springblade.core.tool.utils.CacheUtil.SYS_CACHE;
+
+
+/**
+ * BladeScopeModelHandler
+ *
+ * @author Chill
+ */
+@RequiredArgsConstructor
+public class BladeScopeModelHandler implements ScopeModelHandler {
+
+ private static final String SCOPE_CACHE_CODE = "dataScope:code:";
+ private static final String SCOPE_CACHE_CLASS = "dataScope:class:";
+ private static final String DEPT_CACHE_ANCESTORS = "dept:ancestors:";
+ private static final DataScopeModel SEARCHED_DATA_SCOPE_MODEL = new DataScopeModel(Boolean.TRUE);
+
+ private final JdbcTemplate jdbcTemplate;
+
+ /**
+ * 获取数据权限
+ *
+ * @param mapperId 数据权限mapperId
+ * @param roleId 用户角色集合
+ * @return DataScopeModel
+ */
+ @Override
+ public DataScopeModel getDataScopeByMapper(String mapperId, String roleId) {
+ List