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(SysMenu::getRouterName).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;
|
}
|
}
|