xyc
2025-02-21 664db98c9e8595ce4dd636a27f480e3a08b81ff5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有,侵权必究!
 */
 
package com.zt.core.aspect;
 
import com.zt.common.annotation.DataFilter;
import com.zt.common.constant.Constant;
import com.zt.common.db.query.DataScope;
import com.zt.common.exception.ErrorCode;
import com.zt.common.exception.RenException;
import com.zt.core.context.User;
import com.zt.core.context.UserContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
import java.util.Map;
 
/**
 * 数据过滤,切面处理类
 *
 * @author Mark sunlightcs@gmail.com
 */
@Aspect
@Component("dataFilter")
public class DataFilterAspect {
 
    @Pointcut("@annotation(com.zt.common.annotation.DataFilter)")
    public void dataFilterCut() {
 
    }
 
    @Before("dataFilterCut()")
    public void dataFilter(JoinPoint point) {
        Object params = point.getArgs()[0];
        if (params != null && params instanceof Map) {
            User user = UserContext.getUser();
 
            // 如果是超级管理员,则不进行数据过滤
            if (user.isSuperAdmin()) {
                return;
            }
 
            try {
                // 否则进行数据过滤
                Map map = (Map) params;
                String sqlFilter = getSqlFilter(user, point);
                map.put(Constant.Q.SQL_FILTER, new DataScope(sqlFilter));
            } catch (Exception e) {
 
            }
 
            return;
        }
 
        throw new RenException(ErrorCode.DATA_SCOPE_PARAMS_ERROR);
    }
 
    /**
     * 获取数据过滤的SQL
     */
    private String getSqlFilter(User user, JoinPoint point) throws Exception {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = point.getTarget().getClass()
                .getDeclaredMethod(signature.getName(), signature.getParameterTypes());
        DataFilter dataFilter = method.getAnnotation(DataFilter.class);
 
        // 获取表的别名
        // String tableAlias = dataFilter.tableAlias();
        // if(StringUtils.isNotBlank(tableAlias)){
        // tableAlias += ".";
        // }
 
        StringBuilder sqlFilter = new StringBuilder();
        sqlFilter.append(" (");
        //
        // //部门ID列表
        // List<Long> deptIds = user.getDeptIdList();
        // if(CollUtil.isNotEmpty(deptIds)){
        // sqlFilter.append(tableAlias).append(dataFilter.deptId());
        //
        // sqlFilter.append(" in(").append(StringUtils.join(deptIds,
        // ",")).append(")");
        // }
        //
        // //查询本人数据
        // if(CollUtil.isNotEmpty(deptIds)){
        // sqlFilter.append(" or ");
        // }
        // sqlFilter.append(tableAlias).append(dataFilter.userId()).append("=").append(user.getId());
        //
        // sqlFilter.append(")");
 
        return sqlFilter.toString();
    }
}