puzhibing
2026-03-05 de5f07fe1acd8d462f9c54d7a04c5c47a87264d1
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.xiaopeng.account.service.impl;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaopeng.account.mapper.XpOperationsRoleMapper;
import com.xiaopeng.account.service.ISysGroupMenuService;
import com.xiaopeng.account.service.ISysPermissionService;
import com.xiaopeng.account.service.ISysRoleService;
import com.xiaopeng.account.service.ISysUserRoleService;
import com.xiaopeng.api.feignClient.operation.SysMenuClient;
import com.xiaopeng.api.model.account.*;
import com.xiaopeng.api.model.operation.SysMenu;
import com.xiaopeng.common.core.domain.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
/**
 * 用户权限处理
 *
 * @author ruoyi
 */
@Service
public class SysPermissionServiceImpl implements ISysPermissionService
{
    private static final Logger log = LoggerFactory.getLogger(SysPermissionServiceImpl.class);
 
    @Autowired
    private ISysRoleService roleService;
 
    @Autowired
    private ISysUserRoleService userRoleService;
 
    @Autowired
    private ISysGroupMenuService groupMenuService;
 
    @Autowired
    private SysMenuClient sysMenuClient;
    @Autowired
    private XpOperationsRoleMapper xpOperationsRoleMapper;
    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    @Override
    public Set<String> getRolePermission(SysUser user)
    {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin())
        {
            roles.add("admin");
        }
        else
        {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getId()));
        }
        return roles;
    }
 
    @Override
    public Set<String> getRolePermission(XpOperationsUser user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        roles.addAll(roleService.selectRolePermissionByOperationUserId(user.getId()));
        return roles;
    }
 
    /**
     * 获取菜单数据权限
     *
     * 权限链路: userId -> sys_user_role -> roleId -> sys_role -> groupId
     *          -> sys_group_menu -> menuIds -> (Feign) sys_menu -> perms
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    @Override
    public Set<String> getMenuPermission(SysUser user)
    {
        Set<String> perms = new HashSet<>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            perms.add("*:*:*");
        } else {
            try {
                // 1. 根据 userId 查询 sys_user_role 获取 roleId
                SysUserRole userRole = userRoleService.selectSysUserRoleByUserId(user.getId());
                if (userRole == null || userRole.getRoleId() == null) {
                    log.warn("用户[{}]未分配角色", user.getId());
                    return perms;
                }
                Long roleId = userRole.getRoleId();
 
                // 2. 根据 roleId 查询 sys_role 获取 groupId
                Long groupId = roleService.selectGroupIdByRoleId(roleId);
                if (groupId == null) {
                    log.warn("角色[{}]未关联权限组", roleId);
                    return perms;
                }
 
                // 3. 根据 groupId 查询 sys_group_menu 获取 menuId 列表
                List<Long> menuIds = groupMenuService.selectMenuIdsByGroupId(groupId);
                if (menuIds == null || menuIds.isEmpty()) {
                    log.warn("权限组[{}]未配置菜单权限", groupId);
                    return perms;
                }
 
                // 4. 通过 Feign 调用 operation 服务,根据 menuId 列表获取菜单列表
                R<List<SysMenu>> result = sysMenuClient.getMenusByMenuIds(menuIds);
                if (R.isSuccess(result) && result.getData() != null) {
                    perms = result.getData().stream().map(item -> item.getId().toString()).collect(Collectors.toSet());
                } else {
                    log.error("调用菜单服务获取菜单列表失败: {}", result != null ? result.getMsg() : "返回结果为空");
                }
 
            } catch (Exception e) {
                log.error("获取用户[{}]菜单权限异常", user.getId(), e);
            }
        }
 
 
 
        return perms;
    }
 
    @Override
    public Set<String> getMenuPermission(XpOperationsUser user) {
        Set<String> perms = new HashSet<>();
        try {
            // 1. 根据 userId 查询 sys_user_role 获取 roleId
            List<XpOperationsRole> xpOperationsRoles = xpOperationsRoleMapper.selectSysUserRoleByOperationUserId(user.getId());
            if (CollectionUtils.isEmpty(xpOperationsRoles)) {
                log.warn("用户[{}]未分配角色", user.getId());
                return perms;
            }
            List<Long> roleIds = xpOperationsRoles.stream().map(XpOperationsRole::getRoleId).collect(Collectors.toList());
 
            // 2. 根据 roleId 查询 sys_role 获取 groupId
            List<SysRole> sysRoles = roleService.list(Wrappers.lambdaQuery(SysRole.class)
                    .in(SysRole::getId, roleIds));
            List<Long> groupIds = sysRoles.stream().map(SysRole::getGroupId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(groupIds)) {
                log.warn("角色[{}]未关联权限组", groupIds);
                return perms;
            }
 
            // 3. 根据 groupId 查询 sys_group_menu 获取 menuId 列表
            List<SysGroupMenu> sysGroupMenus = groupMenuService.list(Wrappers.lambdaQuery(SysGroupMenu.class)
                    .in(SysGroupMenu::getGroupId, groupIds));
            List<Long> menuIds = sysGroupMenus.stream().map(SysGroupMenu::getMenuId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(menuIds)) {
                log.warn("权限组[{}]未配置菜单权限", menuIds);
                return perms;
            }
 
            // 4. 通过 Feign 调用 operation 服务,根据 menuId 列表获取菜单列表
            R<List<SysMenu>> result = sysMenuClient.getMenusByMenuIds(menuIds);
            if (R.isSuccess(result) && result.getData() != null) {
                perms = result.getData().stream().map(item -> item.getId().toString()).collect(Collectors.toSet());
            } else {
                log.error("调用菜单服务获取菜单列表失败: {}", result != null ? result.getMsg() : "返回结果为空");
            }
 
        } catch (Exception e) {
            log.error("获取用户[{}]菜单权限异常", user.getId(), e);
        }
        return perms;
    }
}