Pre Merge pull request !687 from Binary/dev

This commit is contained in:
Binary 2025-05-26 06:45:24 +00:00 committed by Gitee
commit bd156ce586
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 45 additions and 1 deletions

View File

@ -1,10 +1,18 @@
package org.dromara.common.core.service; package org.dromara.common.core.service;
import java.util.Map;
/** /**
* 通用 角色服务 * 通用 角色服务
* *
* @author AprilWind * @author AprilWind
*/ */
public interface RoleService { public interface RoleService {
/**
* 根据用户ID查询角色权限
*
* @param userId 用户ID
* @return 角色权限 roleKey:roleName
*/
Map<String, String> selectRoleKeysByUserId(Long userId);
} }

View File

@ -3,10 +3,14 @@ package org.dromara.common.satoken.core.service;
import cn.dev33.satoken.stp.StpInterface; import cn.dev33.satoken.stp.StpInterface;
import org.dromara.common.core.domain.model.LoginUser; import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.enums.UserType; import org.dromara.common.core.enums.UserType;
import org.dromara.common.core.service.RoleService;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.satoken.utils.LoginHelper; import org.dromara.common.satoken.utils.LoginHelper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* sa-token 权限管理实现类 * sa-token 权限管理实现类
@ -36,6 +40,25 @@ public class SaPermissionImpl implements StpInterface {
@Override @Override
public List<String> getRoleList(Object loginId, String loginType) { public List<String> getRoleList(Object loginId, String loginType) {
LoginUser loginUser = LoginHelper.getLoginUser(); LoginUser loginUser = LoginHelper.getLoginUser();
if (loginId != null) {
// 查询指定用户权限
if (loginId instanceof Long) {
Long selectUserId = (Long) loginId;
Long userId = null;
if (loginUser != null) {
userId = loginUser.getUserId();
}
if (!selectUserId.equals(userId)) {
// 查询的不是当前登录用户 那么查询数据库指定用户的权限
RoleService roleService = SpringUtils.getBean("sysRoleServiceImpl");
Map<String, String> roleKeysByUserId = roleService.selectRoleKeysByUserId(selectUserId);
Set<String> roleKeySet = roleKeysByUserId.keySet();
return new ArrayList<>(roleKeySet);
} // else 查询的是当前登录用户 走原来的逻辑
}
}
UserType userType = UserType.getUserType(loginUser.getUserType()); UserType userType = UserType.getUserType(loginUser.getUserType());
if (userType == UserType.SYS_USER) { if (userType == UserType.SYS_USER) {
return new ArrayList<>(loginUser.getRolePermission()); return new ArrayList<>(loginUser.getRolePermission());

View File

@ -40,6 +40,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* 角色 业务层处理 * 角色 业务层处理
@ -97,6 +98,18 @@ public class SysRoleServiceImpl implements ISysRoleService, RoleService {
return baseMapper.selectRolesByUserId(userId); return baseMapper.selectRolesByUserId(userId);
} }
/**
* 根据用户ID查询角色权限
*
* @param userId 用户ID
* @return 角色权限 roleKey:roleName
*/
@Override
public Map<String, String> selectRoleKeysByUserId(Long userId) {
List<SysRoleVo> sysRoleVos = baseMapper.selectRolesByUserId(userId);
return sysRoleVos.stream().collect(Collectors.toMap(SysRoleVo::getRoleKey, SysRoleVo::getRoleName));
}
/** /**
* 根据用户ID查询角色列表(包含被授权状态) * 根据用户ID查询角色列表(包含被授权状态)
* *