|
| 1 | +package com.github.cadecode.uniboot.common.plugin.mybatis.aspect; |
| 2 | + |
| 3 | +import cn.hutool.core.util.ObjUtil; |
| 4 | +import com.github.cadecode.uniboot.common.core.util.SpringUtil; |
| 5 | +import com.github.cadecode.uniboot.common.plugin.mybatis.annotation.UseDataScope; |
| 6 | +import com.github.cadecode.uniboot.common.plugin.mybatis.handler.DataScopeResolver; |
| 7 | +import lombok.*; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.aspectj.lang.JoinPoint; |
| 10 | +import org.aspectj.lang.annotation.After; |
| 11 | +import org.aspectj.lang.annotation.Aspect; |
| 12 | +import org.aspectj.lang.annotation.Before; |
| 13 | +import org.aspectj.lang.annotation.Pointcut; |
| 14 | +import org.aspectj.lang.reflect.MethodSignature; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | + |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +/** |
| 20 | + * 数据权限注解切面 |
| 21 | + * |
| 22 | + * @author Cade Li |
| 23 | + * @since 2024/5/23 |
| 24 | + */ |
| 25 | +@Slf4j |
| 26 | +@RequiredArgsConstructor |
| 27 | +@Aspect |
| 28 | +@Component |
| 29 | +public class DataScopeAspect { |
| 30 | + |
| 31 | + private static final ThreadLocal<DataScopeParam> DATA_SCOPE_LOCAL = new ThreadLocal<>(); |
| 32 | + |
| 33 | + public static DataScopeParam currDataScope() { |
| 34 | + return DATA_SCOPE_LOCAL.get(); |
| 35 | + } |
| 36 | + |
| 37 | + @Pointcut("@within(com.github.cadecode.uniboot.common.plugin.mybatis.annotation.UseDataScope) " + |
| 38 | + "|| @annotation(com.github.cadecode.uniboot.common.plugin.mybatis.annotation.UseDataScope)") |
| 39 | + public void pointCut() { |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + @Before("pointCut()") |
| 44 | + public void before(JoinPoint point) { |
| 45 | + DataScopeParam dataScopeParam = getDataScopeParam(point); |
| 46 | + DATA_SCOPE_LOCAL.set(dataScopeParam); |
| 47 | + } |
| 48 | + |
| 49 | + @After("pointCut()") |
| 50 | + public void after(JoinPoint point) { |
| 51 | + DATA_SCOPE_LOCAL.remove(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 获取数据权限注解内容 |
| 56 | + */ |
| 57 | + private DataScopeParam getDataScopeParam(JoinPoint point) { |
| 58 | + MethodSignature methodSignature = (MethodSignature) point.getSignature(); |
| 59 | + // 获取方法上的注解内容 |
| 60 | + UseDataScope useDataScopeM = methodSignature.getMethod().getAnnotation(UseDataScope.class); |
| 61 | + // 获取类上的注解内容 |
| 62 | + UseDataScope useDataScopeC = methodSignature.getMethod().getDeclaringClass().getAnnotation(UseDataScope.class); |
| 63 | + DataScopeParam dataScopeParam = new DataScopeParam(); |
| 64 | + if (Objects.isNull(useDataScopeM) && Objects.isNull(useDataScopeC)) { |
| 65 | + return dataScopeParam; |
| 66 | + } |
| 67 | + // 方法上有注解 |
| 68 | + if (Objects.nonNull(useDataScopeM)) { |
| 69 | + dataScopeParam.setEnableFilter(useDataScopeM.enableFilter()); |
| 70 | + // 方法上有注解,且类上也有 |
| 71 | + if (Objects.nonNull(useDataScopeC)) { |
| 72 | + dataScopeParam.setField(ObjUtil.defaultIfEmpty(useDataScopeM.field(), |
| 73 | + ObjUtil.defaultIfEmpty(useDataScopeC.field(), UseDataScope.DEFAULT_FIELD))); |
| 74 | + dataScopeParam.setResolverName(ObjUtil.defaultIfEmpty(useDataScopeM.resolverName(), |
| 75 | + ObjUtil.defaultIfEmpty(useDataScopeC.field(), UseDataScope.DEFAULT_RESOLVER_NAME))); |
| 76 | + } else { |
| 77 | + // 方法上有注解,类上无注解 |
| 78 | + dataScopeParam.setField(ObjUtil.defaultIfEmpty(useDataScopeM.field(), UseDataScope.DEFAULT_FIELD)); |
| 79 | + dataScopeParam.setResolverName(ObjUtil.defaultIfEmpty(useDataScopeM.resolverName(), UseDataScope.DEFAULT_RESOLVER_NAME)); |
| 80 | + } |
| 81 | + } else { |
| 82 | + // 方法上无注解,类上有注解 |
| 83 | + dataScopeParam.setEnableFilter(useDataScopeC.enableFilter()); |
| 84 | + dataScopeParam.setField(ObjUtil.defaultIfEmpty(useDataScopeC.field(), UseDataScope.DEFAULT_FIELD)); |
| 85 | + dataScopeParam.setResolverName(ObjUtil.defaultIfEmpty(useDataScopeC.resolverName(), UseDataScope.DEFAULT_RESOLVER_NAME)); |
| 86 | + } |
| 87 | + // 从容器取出数据范围解析器 |
| 88 | + DataScopeResolver scopeResolver = SpringUtil.getBean(dataScopeParam.getResolverName(), DataScopeResolver.class); |
| 89 | + dataScopeParam.setDataScopeResolver(scopeResolver); |
| 90 | + return dataScopeParam; |
| 91 | + } |
| 92 | + |
| 93 | + @Data |
| 94 | + @AllArgsConstructor |
| 95 | + @NoArgsConstructor |
| 96 | + @Builder |
| 97 | + public static class DataScopeParam { |
| 98 | + |
| 99 | + private boolean enableFilter; |
| 100 | + private String field; |
| 101 | + private String resolverName; |
| 102 | + private DataScopeResolver dataScopeResolver; |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments