forked from pig-mesh/pig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pig-mesh#13 from Hccake/dev
移植 ballcat 的数据权限到 pig
- Loading branch information
Showing
49 changed files
with
2,080 additions
and
27 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
37 changes: 37 additions & 0 deletions
37
pig-auth/src/main/java/com/pig4cloud/pig/auth/converter/CustomAccessTokenConverter.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,37 @@ | ||
package com.pig4cloud.pig.auth.converter; | ||
|
||
import com.pig4cloud.pig.common.core.constant.SecurityConstants; | ||
import com.pig4cloud.pig.common.security.service.PigClientDetailsService; | ||
import com.pig4cloud.pig.common.security.service.PigUser; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.oauth2.common.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.provider.ClientDetails; | ||
import org.springframework.security.oauth2.provider.OAuth2Authentication; | ||
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author hccake | ||
*/ | ||
@RequiredArgsConstructor | ||
public class CustomAccessTokenConverter extends DefaultAccessTokenConverter { | ||
|
||
final PigClientDetailsService pigClientDetailsService; | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) { | ||
Map<String, Object> response = (Map<String, Object>) super.convertAccessToken(token, authentication); | ||
|
||
ClientDetails clientDetails = pigClientDetailsService | ||
.loadClientByClientId(authentication.getOAuth2Request().getClientId()); | ||
if (clientDetails != null && clientDetails.getScope().contains("read_data_scope")) { | ||
PigUser principal = (PigUser) authentication.getPrincipal(); | ||
response.put(SecurityConstants.DETAILS_USER_DATA_SCOPE, principal.getUserDataScope()); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
} |
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
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
49 changes: 49 additions & 0 deletions
49
...on-core/src/main/java/com/pig4cloud/pig/common/core/constant/enums/DataScopeTypeEnum.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,49 @@ | ||
package com.pig4cloud.pig.common.core.constant.enums; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
/** | ||
* 数据权限范围类型 | ||
* @author hccake | ||
*/ | ||
@Getter | ||
@AllArgsConstructor | ||
public enum DataScopeTypeEnum { | ||
|
||
/** | ||
* 查询全部数据 | ||
*/ | ||
ALL(0), | ||
|
||
/** | ||
* 本人 | ||
*/ | ||
SELF(1), | ||
|
||
/** | ||
* 本人及子级 | ||
*/ | ||
SELF_CHILD_LEVEL(2), | ||
|
||
/** | ||
* 本级 | ||
*/ | ||
LEVEL(3), | ||
|
||
/** | ||
* 本级及子级 | ||
*/ | ||
LEVEL_CHILD_LEVEL(4), | ||
|
||
/** | ||
* 自定义 | ||
*/ | ||
CUSTOM(5); | ||
|
||
/** | ||
* 类型 | ||
*/ | ||
private final Integer type; | ||
|
||
} |
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,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>pig-common</artifactId> | ||
<groupId>com.pig4cloud</groupId> | ||
<version>3.3.4</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>pig-common-datascope</artifactId> | ||
|
||
<dependencies> | ||
<!-- slf4j日志 --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.jsqlparser</groupId> | ||
<artifactId>jsqlparser</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
36 changes: 36 additions & 0 deletions
36
...ommon/pig-common-datascope/src/main/java/com/pigcloud/pig/common/datascope/DataScope.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,36 @@ | ||
package com.pigcloud.pig.common.datascope; | ||
|
||
import net.sf.jsqlparser.expression.Alias; | ||
import net.sf.jsqlparser.expression.Expression; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* @author Hccake 2020/9/28 | ||
* @version 1.0 | ||
*/ | ||
public interface DataScope { | ||
|
||
/** | ||
* 数据所对应的资源 | ||
* @return 资源标识 | ||
*/ | ||
String getResource(); | ||
|
||
/** | ||
* 该资源相关的所有表,推荐使用 Set 类型。 <br/> | ||
* 如需忽略表名大小写判断,则可以使用 TreeSet,并设置忽略大小写的自定义Comparator。 <br/> | ||
* eg. new TreeSet<>(String.CASE_INSENSITIVE_ORDER); | ||
* @return tableNames | ||
*/ | ||
Collection<String> getTableNames(); | ||
|
||
/** | ||
* 根据表名和表别名,动态生成的 where/or 筛选条件 | ||
* @param tableName 表名 | ||
* @param tableAlias 表别名,可能为空 | ||
* @return 数据规则表达式 | ||
*/ | ||
Expression getExpression(String tableName, Alias tableAlias); | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...datascope/src/main/java/com/pigcloud/pig/common/datascope/DataScopeAutoConfiguration.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,54 @@ | ||
package com.pigcloud.pig.common.datascope; | ||
|
||
import com.pigcloud.pig.common.datascope.handler.DataPermissionHandler; | ||
import com.pigcloud.pig.common.datascope.handler.DefaultDataPermissionHandler; | ||
import com.pigcloud.pig.common.datascope.interceptor.DataPermissionAnnotationAdvisor; | ||
import com.pigcloud.pig.common.datascope.interceptor.DataPermissionInterceptor; | ||
import com.pigcloud.pig.common.datascope.processor.DataScopeSqlProcessor; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author hccake | ||
*/ | ||
@RequiredArgsConstructor | ||
@ConditionalOnBean(DataScope.class) | ||
public class DataScopeAutoConfiguration { | ||
|
||
/** | ||
* 数据权限处理器 | ||
* @param dataScopeList 需要控制的数据范围集合 | ||
* @return DataPermissionHandler | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean | ||
public DataPermissionHandler dataPermissionHandler(List<DataScope> dataScopeList) { | ||
return new DefaultDataPermissionHandler(dataScopeList); | ||
} | ||
|
||
/** | ||
* 数据权限注解 Advisor,用于处理数据权限的链式调用关系 | ||
* @return DataPermissionAnnotationAdvisor | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean(DataPermissionAnnotationAdvisor.class) | ||
public DataPermissionAnnotationAdvisor dataPermissionAnnotationAdvisor() { | ||
return new DataPermissionAnnotationAdvisor(); | ||
} | ||
|
||
/** | ||
* mybatis 拦截器,用于拦截处理 sql | ||
* @param dataPermissionHandler 数据权限处理器 | ||
* @return DataPermissionInterceptor | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean | ||
public DataPermissionInterceptor dataPermissionInterceptor(DataPermissionHandler dataPermissionHandler) { | ||
return new DataPermissionInterceptor(new DataScopeSqlProcessor(), dataPermissionHandler); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...-datascope/src/main/java/com/pigcloud/pig/common/datascope/annotation/DataPermission.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,35 @@ | ||
package com.pigcloud.pig.common.datascope.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* 数据权限注解,注解在 Mapper类 或者 对应方法上 用于提供该 mapper 对应表,所需控制的实体信息 | ||
* @author Hccake 2020/9/27 | ||
* @version 1.0 | ||
*/ | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface DataPermission { | ||
|
||
/** | ||
* 当前类或方法是否忽略数据权限 | ||
* @return boolean 默认返回 false | ||
*/ | ||
boolean ignore() default false; | ||
|
||
/** | ||
* 仅对指定资源类型进行数据权限控制,只在开启情况下有效,当该数组有值时,exclude不生效 | ||
* @see DataPermission#excludeResources | ||
* @return 资源类型数组 | ||
*/ | ||
String[] includeResources() default {}; | ||
|
||
/** | ||
* 对指定资源类型跳过数据权限控制,只在开启情况下有效,当该includeResources有值时,exclude不生效 | ||
* @see DataPermission#includeResources | ||
* @return 资源类型数组 | ||
*/ | ||
String[] excludeResources() default {}; | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...ascope/src/main/java/com/pigcloud/pig/common/datascope/handler/DataPermissionHandler.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,35 @@ | ||
package com.pigcloud.pig.common.datascope.handler; | ||
|
||
import com.pigcloud.pig.common.datascope.DataScope; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 数据权限处理器 | ||
* | ||
* @author Hccake 2020/9/28 | ||
* @version 1.0 | ||
*/ | ||
public interface DataPermissionHandler { | ||
|
||
/** | ||
* 系统配置的所有的数据范围 | ||
* @return 数据范围集合 | ||
*/ | ||
List<DataScope> dataScopes(); | ||
|
||
/** | ||
* 根据权限注解过滤后的数据范围集合 | ||
* @param mappedStatementId Mapper方法ID | ||
* @return 数据范围集合 | ||
*/ | ||
List<DataScope> filterDataScopes(String mappedStatementId); | ||
|
||
/** | ||
* 是否忽略权限控制,用于及早的忽略控制,例如管理员直接放行,而不必等到DataScope中再进行过滤处理,提升效率 | ||
* @return boolean true: 忽略,false: 进行权限控制 | ||
* @param mappedStatementId Mapper方法ID | ||
*/ | ||
boolean ignorePermissionControl(String mappedStatementId); | ||
|
||
} |
Oops, something went wrong.