| | |
| | | package org.dromara.common.mybatis.handler; |
| | | |
| | | import cn.hutool.core.annotation.AnnotationUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
| | | import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList; |
| | | import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| | | import org.apache.ibatis.io.Resources; |
| | | import org.dromara.common.core.domain.dto.RoleDTO; |
| | | import org.dromara.common.core.domain.model.LoginUser; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | |
| | | import org.dromara.common.mybatis.enums.DataScopeType; |
| | | import org.dromara.common.mybatis.helper.DataPermissionHelper; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.springframework.context.ConfigurableApplicationContext; |
| | | import org.springframework.context.expression.BeanFactoryResolver; |
| | | import org.springframework.core.io.Resource; |
| | | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| | | import org.springframework.core.io.support.ResourcePatternResolver; |
| | | import org.springframework.core.type.ClassMetadata; |
| | | import org.springframework.core.type.classreading.CachingMetadataReaderFactory; |
| | | import org.springframework.expression.BeanResolver; |
| | | import org.springframework.expression.ExpressionParser; |
| | | import org.springframework.expression.ParserContext; |
| | | import org.springframework.expression.common.TemplateParserContext; |
| | | import org.springframework.expression.spel.standard.SpelExpressionParser; |
| | | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| | | import org.springframework.util.ClassUtils; |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.util.Arrays; |
| | | import java.util.HashSet; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | |
| | | public class PlusDataPermissionHandler { |
| | | |
| | | /** |
| | | * æ¹æ³æç±»(åç§°) ä¸ æ³¨è§£çæ å°å
³ç³»ç¼å |
| | | */ |
| | | private final Map<String, DataPermission> dataPermissionCacheMap = new ConcurrentHashMap<>(); |
| | | |
| | | /** |
| | | * spel è§£æå¨ |
| | | */ |
| | | private final ExpressionParser parser = new SpelExpressionParser(); |
| | |
| | | private final BeanResolver beanResolver = new BeanFactoryResolver(SpringUtils.getBeanFactory()); |
| | | |
| | | /** |
| | | * æé æ¹æ³ï¼æ«ææå®å
ä¸ç Mapper 类并åå§åç¼å |
| | | * |
| | | * @param mapperPackage Mapper ç±»æå¨çå
è·¯å¾ |
| | | */ |
| | | public PlusDataPermissionHandler(String mapperPackage) { |
| | | scanMapperClasses(mapperPackage); |
| | | } |
| | | |
| | | /** |
| | | * è·åæ°æ®è¿æ»¤æ¡ä»¶ç SQL çæ®µ |
| | | * |
| | | * @param where åå§çæ¥è¯¢æ¡ä»¶è¡¨è¾¾å¼ |
| | |
| | | * @return æ°æ®è¿æ»¤æ¡ä»¶ç SQL çæ®µ |
| | | */ |
| | | public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) { |
| | | // è·åæ°æ®æéé
ç½® |
| | | DataPermission dataPermission = getDataPermission(mappedStatementId); |
| | | // è·åå½åç»å½ç¨æ·ä¿¡æ¯ |
| | | LoginUser currentUser = DataPermissionHelper.getVariable("user"); |
| | | if (ObjectUtil.isNull(currentUser)) { |
| | | currentUser = LoginHelper.getLoginUser(); |
| | | DataPermissionHelper.setVariable("user", currentUser); |
| | | } |
| | | // 妿æ¯è¶
级管çåæç§æ·ç®¡çåï¼åä¸è¿æ»¤æ°æ® |
| | | if (LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin()) { |
| | | return where; |
| | | } |
| | | // æé æ°æ®è¿æ»¤æ¡ä»¶ç SQL çæ®µ |
| | | String dataFilterSql = buildDataFilter(dataPermission, isSelect); |
| | | if (StringUtils.isBlank(dataFilterSql)) { |
| | | return where; |
| | | } |
| | | try { |
| | | // è·åæ°æ®æéé
ç½® |
| | | DataPermission dataPermission = DataPermissionHelper.getPermission(); |
| | | // è·åå½åç»å½ç¨æ·ä¿¡æ¯ |
| | | LoginUser currentUser = DataPermissionHelper.getVariable("user"); |
| | | if (ObjectUtil.isNull(currentUser)) { |
| | | currentUser = LoginHelper.getLoginUser(); |
| | | DataPermissionHelper.setVariable("user", currentUser); |
| | | } |
| | | // 妿æ¯è¶
级管çåæç§æ·ç®¡çåï¼åä¸è¿æ»¤æ°æ® |
| | | if (LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin()) { |
| | | return where; |
| | | } |
| | | // æé æ°æ®è¿æ»¤æ¡ä»¶ç SQL çæ®µ |
| | | String dataFilterSql = buildDataFilter(dataPermission, isSelect); |
| | | if (StringUtils.isBlank(dataFilterSql)) { |
| | | return where; |
| | | } |
| | | Expression expression = CCJSqlParserUtil.parseExpression(dataFilterSql); |
| | | // æ°æ®æé使ç¨åç¬çæ¬å· 鲿¢ä¸å
¶ä»æ¡ä»¶å²çª |
| | | ParenthesedExpressionList<Expression> parenthesis = new ParenthesedExpressionList<>(expression); |
| | |
| | | } |
| | | } catch (JSQLParserException e) { |
| | | throw new ServiceException("æ°æ®æéè§£æå¼å¸¸ => " + e.getMessage()); |
| | | } finally { |
| | | DataPermissionHelper.removePermission(); |
| | | } |
| | | } |
| | | |
| | |
| | | context.setVariable(dataColumn.key()[i], dataColumn.value()[i]); |
| | | } |
| | | |
| | | // å¿½ç¥æ°æ®æé 鲿¢spel表达å¼å
æå
¶ä»sqlæ¥è¯¢å¯¼è´æ»å¾ªç¯è°ç¨ |
| | | String sql = DataPermissionHelper.ignore(() -> |
| | | parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class) |
| | | ); |
| | | // è§£æsql模æ¿å¹¶å¡«å
|
| | | String sql = parser.parseExpression(type.getSqlTemplate(), parserContext).getValue(context, String.class); |
| | | conditions.add(joinStr + sql); |
| | | isSuccess = true; |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * æ«ææå®å
ä¸ç Mapper ç±»ï¼å¹¶æ¥æ¾å
¶ä¸å¸¦æç¹å®æ³¨è§£çæ¹æ³æç±» |
| | | * |
| | | * @param mapperPackage Mapper ç±»æå¨çå
è·¯å¾ |
| | | */ |
| | | private void scanMapperClasses(String mapperPackage) { |
| | | // åå»ºèµæºè§£æå¨åå
æ°æ®è¯»åå·¥å |
| | | PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); |
| | | CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory(); |
| | | // å° Mapper å
è·¯å¾æåé符æå为æ°ç» |
| | | String[] packagePatternArray = StringUtils.splitPreserveAllTokens(mapperPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); |
| | | String classpath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX; |
| | | try { |
| | | for (String packagePattern : packagePatternArray) { |
| | | // å°å
è·¯å¾è½¬æ¢ä¸ºèµæºè·¯å¾ |
| | | String path = ClassUtils.convertClassNameToResourcePath(packagePattern); |
| | | // è·åæå®è·¯å¾ä¸çææ .class æä»¶èµæº |
| | | Resource[] resources = resolver.getResources(classpath + path + "/*.class"); |
| | | for (Resource resource : resources) { |
| | | // è·åèµæºçç±»å
æ°æ® |
| | | ClassMetadata classMetadata = factory.getMetadataReader(resource).getClassMetadata(); |
| | | // è·åèµæºå¯¹åºç类对象 |
| | | Class<?> clazz = Resources.classForName(classMetadata.getClassName()); |
| | | // æ¥æ¾ç±»ä¸çç¹å®æ³¨è§£ |
| | | findAnnotation(clazz); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("åå§åæ°æ®å®å
¨ç¼åæ¶åºé:{}", e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 卿å®çç±»ä¸æ¥æ¾ç¹å®ç注解 DataPermissionï¼å¹¶å°å¸¦æè¿ä¸ªæ³¨è§£çæ¹æ³æç±»åå¨å° dataPermissionCacheMap ä¸ |
| | | * |
| | | * @param clazz è¦æ¥æ¾çç±» |
| | | */ |
| | | private void findAnnotation(Class<?> clazz) { |
| | | DataPermission dataPermission; |
| | | for (Method method : clazz.getMethods()) { |
| | | if (method.isDefault() || method.isVarArgs()) { |
| | | continue; |
| | | } |
| | | String mappedStatementId = clazz.getName() + "." + method.getName(); |
| | | if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) { |
| | | dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class); |
| | | dataPermissionCacheMap.put(mappedStatementId, dataPermission); |
| | | } |
| | | } |
| | | if (AnnotationUtil.hasAnnotation(clazz, DataPermission.class)) { |
| | | dataPermission = AnnotationUtil.getAnnotation(clazz, DataPermission.class); |
| | | dataPermissionCacheMap.put(clazz.getName(), dataPermission); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®æ å°è¯å¥ ID æç±»åè·å对åºç DataPermission 注解对象 |
| | | * |
| | | * @param mapperId æ å°è¯å¥ ID |
| | | * @return DataPermission 注解对象ï¼å¦æä¸åå¨åè¿å null |
| | | */ |
| | | public DataPermission getDataPermission(String mapperId) { |
| | | // æ£æ¥ç¼å䏿¯å¦å
嫿 å°è¯å¥ ID 对åºç DataPermission 注解对象 |
| | | if (dataPermissionCacheMap.containsKey(mapperId)) { |
| | | return dataPermissionCacheMap.get(mapperId); |
| | | } |
| | | // 妿ç¼åä¸ä¸å
嫿 å°è¯å¥ ID 对åºç DataPermission 注解对象ï¼åå°è¯ä½¿ç¨ç±»åä½ä¸ºé®æ¥æ¾ |
| | | String clazzName = mapperId.substring(0, mapperId.lastIndexOf(".")); |
| | | if (dataPermissionCacheMap.containsKey(clazzName)) { |
| | | return dataPermissionCacheMap.get(clazzName); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * æ£æ¥ç»å®çæ å°è¯å¥ ID æ¯å¦ææï¼å³æ¯å¦è½å¤æ¾å°å¯¹åºç DataPermission 注解对象 |
| | | * |
| | | * @param mapperId æ å°è¯å¥ ID |
| | | * @return 妿æ¾å°å¯¹åºç DataPermission 注解对象ï¼åè¿å falseï¼å¦åè¿å true |
| | | */ |
| | | public boolean invalid(String mapperId) { |
| | | return getDataPermission(mapperId) == null; |
| | | public boolean invalid() { |
| | | return DataPermissionHelper.getPermission() == null; |
| | | } |
| | | } |