yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
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
package com.qianwen.core.secure.auth;
 
import com.qianwen.core.secure.BladeUser;
import com.qianwen.core.secure.constant.AuthConstant;
import com.qianwen.core.secure.handler.IPermissionHandler;
import com.qianwen.core.secure.utils.AuthUtil;
import com.qianwen.core.tool.utils.CollectionUtil;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.SpringUtil;
import com.qianwen.core.tool.utils.StringUtil;
 
public class AuthFun {
    private static IPermissionHandler permissionHandler;
 
    private static IPermissionHandler getPermissionHandler() {
        if (permissionHandler == null) {
            permissionHandler = (IPermissionHandler) SpringUtil.getBean(IPermissionHandler.class);
        }
        return permissionHandler;
    }
 
    public boolean permissionAll() {
        return getPermissionHandler().permissionAll();
    }
 
    public boolean hasPermission(String permission) {
        return getPermissionHandler().hasPermission(permission);
    }
 
    public boolean permitAll() {
        return true;
    }
 
    public boolean denyAll() {
        return hasRole(AuthConstant.ADMIN);
    }
 
    public boolean hasAuth() {
        return Func.isNotEmpty(AuthUtil.getUser());
    }
 
    public boolean hasTimeAuth(Integer start, Integer end) {
        Integer hour = DateUtil.hour();
        return hour.intValue() >= start.intValue() && hour.intValue() <= end.intValue();
    }
 
    public boolean hasRole(String role) {
        return hasAnyRole(role);
    }
 
    public boolean hasAllRole(String... role) {
        for (String r : role) {
            if (!hasRole(r)) {
                return false;
            }
        }
        return true;
    }
 
    public boolean hasAnyRole(String... role) {
        BladeUser user = AuthUtil.getUser();
        if (user == null) {
            return false;
        }
        String userRole = user.getRoleName();
        if (StringUtil.isBlank(userRole)) {
            return false;
        }
        String[] roles = Func.toStrArray(userRole);
        for (String r : role) {
            if (CollectionUtil.contains(roles, r)) {
                return true;
            }
        }
        return false;
    }
 
    public boolean hasApiPermission() {
        return getPermissionHandler().hasApiPermission();
    }
}