zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
package security.internal;
 
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
import kernel.web.ApplicationUtil;
import security.Resource;
import security.Role;
 
public class SecurityAuthoritiesHolderImpl implements SecurityAuthoritiesHolder {
 
    private Map<String, Map<String, String>> cache = new HashMap<String, Map<String, String>>();
 
    private List<Resource> getResourcesByType(String resType) {
        List<Resource> resources=ApplicationUtil.executeSelect(Resource.class,"WHERE RES_TYPE=?",new Object[] {resType});
        String roleSql="SELECT * FROM SCT_ROLE R WHERE EXISTS (SELECT ROLE_UUID FROM SCT_ROLE_RESOURCE WHERE RESOURCE_UUID=? AND ROLE_UUID=R.UUID)";
        
        for(Resource resource:resources) {
            List<Role> roleList=ApplicationUtil.executeDQL(roleSql,new Object[] {resource.getId()}, Role.class);
            if(null==roleList || roleList.isEmpty()) continue;
            resource.setRoles(roleList.stream().filter(role->null!=role).collect(Collectors.toSet()));
        }
        
        return resources;
    }
 
    public Map<String, String> loadAuthorities(String resType) {
        Map<String, String> authorities = cache.get(resType);
        if (authorities != null) return authorities;
        
        cache.put(resType, authorities=new LinkedHashMap<String, String>());
        List<Resource> urlResources = getResourcesByType(resType);
        
        Collections.sort(urlResources,new Comparator<Resource>() {
            public int compare(Resource o1, Resource o2) {
                if (o1.getResString().length()<o2.getResString().length()) {
                    return 1;
                } else if (o1.getResString().length()==o2.getResString().length()){
                    return 0;
                }else {
                    return -1;
                }
            }
          });
        
        for (Resource resource : urlResources) {
            authorities.put(resource.getResString(),resource.getRoleAuthorities());
        }
        
        return authorities;
    }
 
    @Override
    public void clean() {
        cache = new HashMap<String, Map<String, String>>();
    }
}