@ -8,8 +8,10 @@
package com.epmet.commons.mybatis.aspect ;
package com.epmet.commons.mybatis.aspect ;
import com.epmet.commons.mybatis.annotation.DataFilter ;
import com.epmet.commons.mybatis.constant.OpeScopeConstant ;
import com.epmet.commons.mybatis.constant.OpeScopeConstant ;
import com.epmet.commons.mybatis.dto.form.OperationScopeDTO ;
import com.epmet.commons.mybatis.dto.form.OperationScopeDTO ;
import com.epmet.commons.mybatis.dto.form.OperationScopeFormDTO ;
import com.epmet.commons.mybatis.dto.form.StaffPermCacheResultDTO ;
import com.epmet.commons.mybatis.dto.form.StaffPermCacheResultDTO ;
import com.epmet.commons.mybatis.dto.form.StaffPermissionFormDTO ;
import com.epmet.commons.mybatis.dto.form.StaffPermissionFormDTO ;
import com.epmet.commons.mybatis.entity.DataScope ;
import com.epmet.commons.mybatis.entity.DataScope ;
@ -23,6 +25,7 @@ import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint ;
import org.aspectj.lang.JoinPoint ;
import org.aspectj.lang.annotation.Aspect ;
import org.aspectj.lang.annotation.Aspect ;
import org.aspectj.lang.annotation.Before ;
import org.aspectj.lang.annotation.Before ;
import org.aspectj.lang.reflect.MethodSignature ;
import org.slf4j.Logger ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
@ -53,15 +56,11 @@ public class DataFilterAspect {
@Before ( "@annotation(com.epmet.commons.mybatis.annotation.DataFilter)" )
@Before ( "@annotation(com.epmet.commons.mybatis.annotation.DataFilter)" )
public void dataFilter ( JoinPoint point ) {
public void dataFilter ( JoinPoint point ) {
// 反射的方式
// 通过反射,取到注解属性
//MethodSignature signature = (MethodSignature) point.getSignature();
DataFilter dataFilterAnno = ( ( MethodSignature ) point . getSignature ( ) ) . getMethod ( ) . getAnnotation ( DataFilter . class ) ;
//Class[] parameterTypes = signature.getParameterTypes();
String tableAlias = dataFilterAnno . tableAlias ( ) ;
//for (Class parameterType : parameterTypes) {
// if (parameterType == DataScope.class) {
//
// }
//}
// 从ThreadLocal中取所需权限
String requirePermission = AccessOpeAspect . requirePermissionTl . get ( ) ;
String requirePermission = AccessOpeAspect . requirePermissionTl . get ( ) ;
// 没有配置所需权限,不做操作,打印提示日志
// 没有配置所需权限,不做操作,打印提示日志
if ( StringUtils . isBlank ( requirePermission ) ) {
if ( StringUtils . isBlank ( requirePermission ) ) {
@ -69,10 +68,14 @@ public class DataFilterAspect {
return ;
return ;
}
}
String app = loginUserUtil . getLoginUserApp ( ) ;
String client = loginUserUtil . getLoginUserClient ( ) ;
String userId = loginUserUtil . getLoginUserId ( ) ;
StaffPermissionFormDTO staffPermissionFormDTO = new StaffPermissionFormDTO ( ) ;
StaffPermissionFormDTO staffPermissionFormDTO = new StaffPermissionFormDTO ( ) ;
staffPermissionFormDTO . setApp ( loginUserUtil . getLoginUserApp ( ) ) ;
staffPermissionFormDTO . setApp ( app ) ;
staffPermissionFormDTO . setClient ( loginUserUtil . getLoginUserClient ( ) ) ;
staffPermissionFormDTO . setClient ( cl ient) ;
staffPermissionFormDTO . setStaffId ( loginUserUtil . getLoginUserId ( ) ) ;
staffPermissionFormDTO . setStaffId ( userId ) ;
Result < StaffPermCacheResultDTO > result = govAccessFeignClient . getStaffCurrPermissions ( staffPermissionFormDTO ) ;
Result < StaffPermCacheResultDTO > result = govAccessFeignClient . getStaffCurrPermissions ( staffPermissionFormDTO ) ;
if ( result . getCode ( ) ! = 0 ) {
if ( result . getCode ( ) ! = 0 ) {
@ -91,10 +94,17 @@ public class DataFilterAspect {
// 校验操作权限
// 校验操作权限
validateOpePermission ( permCacheResultDTO . getPermissions ( ) , requirePermission ) ;
validateOpePermission ( permCacheResultDTO . getPermissions ( ) , requirePermission ) ;
// 生成过滤sql
Object [ ] methodArgs = point . getArgs ( ) ;
Object [ ] methodArgs = point . getArgs ( ) ;
for ( Object methodArg : methodArgs ) {
for ( Object methodArg : methodArgs ) {
if ( methodArg instanceof DataScope ) {
if ( methodArg instanceof DataScope ) {
( ( DataScope ) methodArg ) . setSqlFilter ( getSqlFilterSegment ( permCacheResultDTO . getRoleIdList ( ) , requirePermission , permCacheResultDTO . getOrgIdPath ( ) ) ) ;
( ( DataScope ) methodArg ) . setSqlFilter ( getSqlFilterSegment (
userId ,
permCacheResultDTO . getRoleIdList ( ) ,
requirePermission ,
permCacheResultDTO . getOrgIdPath ( ) ,
permCacheResultDTO . getGridId ( ) ,
tableAlias ) ) ;
return ;
return ;
}
}
}
}
@ -118,56 +128,111 @@ public class DataFilterAspect {
*
*
* @return
* @return
* /
* /
private String getSqlFilterSegment ( Set < String > roleIds , String reqiurePermission , String orgIdPath ) {
private String getSqlFilterSegment ( String userId , S et < String > roleIds , String reqiurePermission , String orgIdPath , String gridId , String tableAlias ) {
// 根据角色列表查询操作范围列表
// 根据角色列表查询操作范围列表
// todo 暂停,先模拟数据
Set < OperationScopeDTO > scopeDTOS = new HashSet < > ( ) ;
//roleIds.forEach(roleId -> {
roleIds . forEach ( roleId - > {
// OperationScopeFormDTO osformDto = new OperationScopeFormDTO();
OperationScopeFormDTO osformDto = new OperationScopeFormDTO ( ) ;
// osformDto.setRoleId(roleId);
osformDto . setRoleId ( roleId ) ;
// osformDto.setOperationKey(reqiurePermission);
osformDto . setOperationKey ( reqiurePermission ) ;
// Result<List<OperationScopeDTO>> result = govAccessFeignClient.getOperationScopesByRoleId(osformDto);
Result < Set < OperationScopeDTO > > result = govAccessFeignClient . getOperationScopesByRoleId ( osformDto ) ;
// List<OperationScopeDTO> scopeDTOS = result.getData();
if ( result . success ( ) ) {
//});
scopeDTOS . addAll ( result . getData ( ) ) ;
Set < OperationScopeDTO > scopeDTOS = genScopeDtos ( ) ;
}
} ) ;
// 过滤有效范围
// 过滤范围
HashSet < String > scopes = filteScopes ( scopeDTOS ) ;
HashSet < String > scopes = filteScopes ( scopeDTOS ) ;
if ( CollectionUtils . isEmpty ( scopes ) ) {
// 没有范围限制
return "" ;
}
StringBuilder sb = new StringBuilder ( ) ;
// 1.生成sql:组织范围过滤
genOrgScopeSql ( sb , orgIdPath , scopes , tableAlias ) ;
// 2.生成sql:我发起的
genIStartedSql ( sb , userId , tableAlias ) ;
// 3.生成sql:本网格的
genInGrid ( sb , gridId , tableAlias ) ;
//sb.append(")");
System . out . println ( "生成的过滤sql:" + sb . toString ( ) ) ;
return sb . toString ( ) ;
}
/ * *
* 网格sql
* @param sb
* @param gridId
* @param tableAlias
* /
private void genInGrid ( StringBuilder sb , String gridId , String tableAlias ) {
if ( StringUtils . isBlank ( tableAlias ) ) {
sb . append ( " OR GRID_ID ='" ) . append ( gridId ) . append ( "'" ) ;
} else {
sb . append ( " OR " ) . append ( tableAlias ) . append ( ".GRID_ID ='" ) . append ( gridId ) . append ( "'" ) ;
}
}
/ * *
* sql : 我发起的
* @param userId
* /
private void genIStartedSql ( StringBuilder sb , String userId , String tableAlias ) {
if ( StringUtils . isBlank ( tableAlias ) ) {
sb . append ( " OR CREATED_BY ='" ) . append ( userId ) . append ( "'" ) ;
} else {
sb . append ( " OR " ) . append ( tableAlias ) . append ( ".CREATED_BY ='" ) . append ( userId ) . append ( "'" ) ;
}
}
/ * *
* 计算组织范围过滤sql , 整体入口
* @param sb
* @param orgIdPath
* @param scopes
* /
public void genOrgScopeSql ( StringBuilder sb , String orgIdPath , HashSet < String > scopes , String tableAlias ) {
// 取出父组织ID path 和当前组织ID
// 取出父组织ID path 和当前组织ID
String pOrgPath = orgIdPath . substring ( 0 , orgIdPath . lastIndexOf ( orgIdPathSpliter ) ) ;
String pOrgPath = orgIdPath . substring ( 0 , orgIdPath . lastIndexOf ( orgIdPathSpliter ) ) ;
String currOrgPath = orgIdPath . substring ( orgIdPath . lastIndexOf ( orgIdPathSpliter ) + 1 ) ;
String currOrgPath = orgIdPath . substring ( orgIdPath . lastIndexOf ( orgIdPathSpliter ) + 1 ) ;
genOrgScopeSql ( sb , scopes , currOrgPath , pOrgPath , tableAlias ) ;
StringBuilder sb = new StringBuilder ( " AND (" ) ;
getOrgScopeSql ( sb , scopes , currOrgPath , pOrgPath ) ;
sb . replace ( sb . lastIndexOf ( "OR" ) , sb . lastIndexOf ( "OR" ) + 3 , "" ) ;
sb . replace ( sb . lastIndexOf ( "OR" ) , sb . lastIndexOf ( "OR" ) + 3 , "" ) ;
sb . append ( ") " ) ;
// 拼接sql语句
sb . replace ( 141 , 142 , "" ) ;
// TODO
return "" ;
}
}
/ * *
/ * *
* 计算范围过滤sql
* 计算组织范围过滤sql
* @param scopes
* @param scopes
* @param currOrg
* @param currOrg
* @param pOrgPath
* @param pOrgPath
* @return
* @return
* /
* /
private void getOrgScopeSql ( StringBuilder sb , HashSet < String > scopes , String currOrg , String pOrgPath ) {
private void gen OrgScopeSql ( StringBuilder sb , HashSet < String > scopes , String currOrg , String pOrgPath , String tableAlias ) {
for ( String scope : scopes ) {
for ( String scope : scopes ) {
switch ( scope ) {
switch ( scope ) {
case OpeScopeConstant . ORG_CURR :
case OpeScopeConstant . ORG_CURR :
getAgencyCurrScopedSql ( sb , currOrg ) ;
if ( StringUtils . isBlank ( tableAlias ) ) {
sb . append ( " OR " ) ;
sb . append ( " ORG_ID = '" ) . append ( currOrg ) . append ( "' OR " ) ;
} else {
sb . append ( " " ) . append ( tableAlias ) . append ( ".ORG_ID = '" ) . append ( currOrg ) . append ( "' OR " ) ;
}
break ;
break ;
case OpeScopeConstant . ORG_CURR_AND_SUB :
case OpeScopeConstant . ORG_CURR_AND_SUB :
getAgencyCurrAndSubScopedSql ( sb , pOrgPath ) ;
if ( StringUtils . isBlank ( tableAlias ) ) {
sb . append ( " OR " ) ;
sb . append ( " ORG_ID_PATH like '" ) . append ( pOrgPath ) . append ( "%' " ) . append ( " OR " ) ;
} else {
sb . append ( " " ) . append ( tableAlias ) . append ( ".ORG_ID_PATH like '" ) . append ( pOrgPath ) . append ( "%' " ) . append ( " OR " ) ;
}
break ;
break ;
case OpeScopeConstant . ORG_CURR_SUB :
case OpeScopeConstant . ORG_CURR_SUB :
getAgencyCurrSubScopedSql ( sb , pOrgPath , currOrg ) ;
if ( StringUtils . isBlank ( tableAlias ) ) {
sb . append ( " OR " ) ;
sb . append ( " ORG_ID_PATH like '" ) . append ( pOrgPath ) . append ( orgIdPathSpliter ) . append ( currOrg ) . append ( "%' " ) . append ( " OR " ) ;
} else {
sb . append ( " " ) . append ( tableAlias ) . append ( ".ORG_ID_PATH like '" ) . append ( pOrgPath ) . append ( orgIdPathSpliter ) . append ( currOrg ) . append ( "%' " ) . append ( " OR " ) ;
}
break ;
break ;
case OpeScopeConstant . ORG_EQUAL :
case OpeScopeConstant . ORG_EQUAL :
// todo 同级
// todo 同级
@ -185,32 +250,6 @@ public class DataFilterAspect {
}
}
}
}
/ * *
* 本身
* @param sb
* @param orgId
* /
public void getAgencyCurrScopedSql ( StringBuilder sb , String orgId ) {
sb . append ( " ORG_ID = " ) . append ( orgId ) ;
}
/ * *
* 本身及子级
* @param sb
* @param pOrgIdPath
* /
public void getAgencyCurrAndSubScopedSql ( StringBuilder sb , String pOrgIdPath ) {
sb . append ( " ORG_ID_PATH like '" ) . append ( pOrgIdPath ) . append ( "%'" ) ;
}
/ * *
* 子级组织 ( 不含本身 )
* @param sb
* /
public void getAgencyCurrSubScopedSql ( StringBuilder sb , String pOrgIdPath , String currOrgIdPath ) {
sb . append ( "ORG_ID_PATH like '" ) . append ( pOrgIdPath ) . append ( orgIdPathSpliter ) . append ( currOrgIdPath ) . append ( "%'" ) ;
}
/ * *
/ * *
* 过滤有效范围
* 过滤有效范围
*
*