add
yj
2024-12-05 a160d838f9c0a79dfc2f18e7cd46bdd4faa59c6d
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
package com.dobbinsoft.fw.core.util;
 
 
 
import com.dobbinsoft.fw.core.entiy.inter.IdentityOwner;
import com.dobbinsoft.fw.core.entiy.inter.PermissionOwner;
import com.dobbinsoft.fw.core.exception.ServiceException;
 
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
 
/**
 * Created by rize on 2019/2/27.
 * Edit by rize on 2021/3/16.
 */
public class SessionUtil<U extends IdentityOwner, A extends PermissionOwner> implements ISessionUtil<U, A> {
 
    private ThreadLocal<U> userLocal = new ThreadLocal<U>();
 
    private ThreadLocal<A> adminLocal = new ThreadLocal<A>();
 
    private Class<U> userClass;
 
    private Class<A> adminClass;
 
    public SessionUtil(Class<U> userClass, Class<A> adminClass) {
        this.userClass = userClass;
        this.adminClass = adminClass;
    }
 
    public void setUser(U userDTO) {
        userLocal.set(userDTO);
    }
 
    public U getUser() {
        return userLocal.get();
    }
 
    public void setAdmin(A adminDTO) {
        adminLocal.set(adminDTO);
    }
 
    public A getAdmin() {
        return adminLocal.get();
    }
 
    public Class<U> getUserClass() {
        return this.userClass;
    }
 
    public Class<A> getAdminClass() {
        return this.adminClass;
    }
 
    public boolean hasPerm(String permission) throws ServiceException {
        //拥有的权限
        List<String> perms = getAdmin().getPerms();
        boolean hasPerm = false;
        //目标匹配权限
        String[] permissions = permission.split(":");
        outer : for(String item : perms) {
            //拥有的权限点
            String[] hasPer = item.split(":");
            inner : for (int i = 0; i < permissions.length; i++) {
                if ("*".equals(hasPer[i])) {
                    hasPerm = true;
                    break outer;
                } else if (hasPer[i].equals(permissions[i])){
                    //此层合格
                    if (i == permissions.length - 1) {
                        //若是目标层的最后一层。则表示所有层校验通过
                        hasPerm = true;
                    }
                } else {
                    break inner;
                }
            }
        }
        return hasPerm;
    }
 
}