疯狂的狮子li
2022-01-13 6ca853516671882e8e54d9630a4299f12fbcf9aa
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
package com.ruoyi.common.helper;
 
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.ServletUtils;
 
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 数据权限助手
 *
 * @author Lion Li
 * @version 3.5.0
 */
@SuppressWarnings("unchecked cast")
public class DataPermissionHelper {
 
    private static final String DATA_PERMISSION_KEY = "data:permission";
 
    public static <T> T getVariable(String key) {
        Map<String, Object> context = getContext();
        return (T) context.get(key);
    }
 
 
 
    public static void setVariable(String key, Object value) {
        Map<String, Object> context = getContext();
        context.put(key, value);
    }
 
    public static Map<String, Object> getContext() {
        HttpServletRequest request = ServletUtils.getRequest();
        Object attribute = request.getAttribute(DATA_PERMISSION_KEY);
        if (ObjectUtil.isNull(attribute)) {
            request.setAttribute(DATA_PERMISSION_KEY, new HashMap<>());
            attribute = request.getAttribute(DATA_PERMISSION_KEY);
        }
        if (attribute instanceof Map) {
            return (Map<String, Object>) attribute;
        }
        throw new NullPointerException("data permission context type exception");
    }
}