jinlin
2024-09-06 3ecb68c427a627ad8e90d8c555655e7724be2d96
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
/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有,侵权必究!
 */
 
package com.zt.security.service;
 
import java.util.*;
import java.util.stream.Collectors;
 
import javax.annotation.Resource;
 
import com.zt.core.sys.model.SysDept;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.zt.common.constant.CacheKey;
import com.zt.common.constant.Constant;
import com.zt.common.utils.CacheUtils;
import com.zt.core.context.User;
import com.zt.core.context.UserContext;
import com.zt.core.sys.model.SysUser;
import com.zt.modules.sys.dao.SysMenuDao;
import com.zt.modules.sys.dao.SysDeptDao;
import com.zt.modules.sys.mapstruct.SysUserMapper;
import com.zt.modules.sys.service.SysUserService;
import com.zt.modules.sys.service.SysDeptService;
import com.zt.security.model.SysUserToken;
 
/**
 * shiro相关接口
 *
 * @author hehz
 */
@Service
public class ShiroService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserTokenService sysUserTokenService;
 
    public SysUserToken getByToken(String token) {
        return sysUserTokenService.getByToken(token);
    }
    public void updateTokenById(SysUserToken tokenEntity) {
        sysUserTokenService.updateById(tokenEntity);
    }
 
    @Cacheable(value = Constant.Cache.TOKEN, key = "'user:' + #userId")
    public User getUser(Long userId) {
        SysUser entity = sysUserService.get(userId);
        User user = sysUserMapper.toUser(entity);
        user.setPlatform(Constant.Sys.PLATFORM_TENANT_ID == user.getTenantId());
        user.setPermissions(getUserPermissions(user));
        SysDept dept = sysDeptService.get(user.getCompanyId());
        if (dept!=null){
            user.setUnitType(dept.getNature());
            user.setUnitCode(dept.getCode());
        }
        return user;
    }
 
    /**
     * 获取用户权限列表
     */
    private Set<String> getUserPermissions(User user) {
        // 系统管理员,拥有最高权限
        List<String> permissionsList;
        if (user.isSuperAdmin()) {
            permissionsList = sysMenuDao.getPermissionsList();
            permissionsList.add(Constant.Permissions.SUPER_ADMIN);// 超级管理员权限
        } else {
            permissionsList = sysMenuDao.getUserMenuList(user.getTenantId(), user.getId()).stream()
                    .map(menu -> menu.getPermissions()).collect(Collectors.toList());
        }
 
        // 用户权限列表
        Set<String> permsSet = new HashSet<>();
        for (String permissions : permissionsList) {
            if (StringUtils.isBlank(permissions)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(permissions.trim().split(",")));
        }
 
        return permsSet;
    }
}