@@ -5,13 +5,14 @@ import com.yunhai.userhome.entity.UserCurrentRole;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 用户当前激活角色(user_current_role 表读写)。
|
||||
* 用户当前激活角色访问(UserBase 单一权威源,经 SDK 走 oapi,本地无存储)。<br>
|
||||
* 保留接口以稳定既有 81 处消费点(仅读 roleId/schId);实现由本地表改为 UserBase 开放接口。
|
||||
*/
|
||||
public interface IUserCurrentRoleService {
|
||||
|
||||
/** 取用户当前角色(可能不存在)。 */
|
||||
/** 取用户当前激活角色(未切换时兜底主身份;无任何身份返回 Optional.empty)。 */
|
||||
Optional<UserCurrentRole> findById(Integer userId);
|
||||
|
||||
/** 保存/覆盖用户当前角色。 */
|
||||
/** 切换用户当前激活角色(目标身份须属该用户,由 UserBase 校验)。 */
|
||||
void save(UserCurrentRole role);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.yunhai.userhome.service.IMenuBarService;
|
||||
import com.yunhai.userhome.service.IUserCurrentRoleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -55,6 +56,7 @@ public class MenuBarServiceImpl implements IMenuBarService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveMenuBar(MenuBar menuBar) {
|
||||
if (menuBar.getId() != null) {
|
||||
Optional<MenuBar> optional = menuBarRepository.findById(menuBar.getId());
|
||||
@@ -76,6 +78,7 @@ public class MenuBarServiceImpl implements IMenuBarService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void toggleMenuBarShow(Integer id, Integer schId) {
|
||||
Optional<MenuBar> optionalMenuBar = menuBarRepository.findById(id);
|
||||
if (!optionalMenuBar.isPresent())
|
||||
|
||||
@@ -1,26 +1,68 @@
|
||||
package com.yunhai.userhome.service.impl;
|
||||
|
||||
import com.yunhai.platform.openapi.client.OpenApiUserClient;
|
||||
import com.yunhai.platform.utils.model.BoaRoleInfo;
|
||||
import com.yunhai.userhome.entity.UserCurrentRole;
|
||||
import com.yunhai.userhome.repository.UserCurrentRoleRepository;
|
||||
import com.yunhai.userhome.service.IUserCurrentRoleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 用户当前激活角色访问实现:数据源由本地 {@code user_current_role} 表迁移至 UserBase 单一权威源
|
||||
* (多角色迁移方案)。<br>
|
||||
* {@link #findById} → UserBase {@code /oapi/identity/current}(未切换兜底 is_main 主身份);
|
||||
* {@link #save} → UserBase {@code /oapi/identity/switch}(目标身份属主校验 + 白名单,均在 UserBase 侧)。
|
||||
*/
|
||||
@Service
|
||||
public class UserCurrentRoleServiceImpl implements IUserCurrentRoleService {
|
||||
|
||||
@Autowired
|
||||
private UserCurrentRoleRepository repository;
|
||||
private OpenApiUserClient openApiUserClient;
|
||||
|
||||
@Override
|
||||
public Optional<UserCurrentRole> findById(Integer userId) {
|
||||
return repository.findByUserId(userId);
|
||||
if (userId == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
BoaRoleInfo info = openApiUserClient.getCurrentIdentity(userId);
|
||||
if (info == null) {
|
||||
// 无当前身份(超管等平台账号):兜底平台级默认身份(schId=0),避免下游 orElseThrow 抛错 / 拿空菜单
|
||||
return Optional.of(platformDefaultRole(userId));
|
||||
}
|
||||
return Optional.of(toRole(info));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(UserCurrentRole role) {
|
||||
repository.save(role);
|
||||
if (role == null || role.getUserId() == null) {
|
||||
throw new IllegalArgumentException("用户ID不能为空");
|
||||
}
|
||||
openApiUserClient.switchIdentity(role.getUserId(), role.getRoleId(), role.getSchId());
|
||||
}
|
||||
|
||||
/** BoaRoleInfo → UserCurrentRole(字段同名,纯 DTO 映射) */
|
||||
private UserCurrentRole toRole(BoaRoleInfo info) {
|
||||
if (info == null) {
|
||||
return null;
|
||||
}
|
||||
UserCurrentRole role = new UserCurrentRole();
|
||||
role.setRoleId(info.getRoleId());
|
||||
role.setRoleName(info.getRoleName());
|
||||
role.setSchId(info.getSchId());
|
||||
role.setSchName(info.getSchName());
|
||||
role.setChildId(info.getChildId());
|
||||
role.setChildName(info.getChildName());
|
||||
return role;
|
||||
}
|
||||
|
||||
/** 平台级默认身份:超管 roleId=0、schId=0(对齐各控制器 type==1 → schId=0 的既有约定) */
|
||||
private UserCurrentRole platformDefaultRole(Integer userId) {
|
||||
UserCurrentRole role = new UserCurrentRole();
|
||||
role.setUserId(userId);
|
||||
role.setRoleId(0);
|
||||
role.setSchId(0);
|
||||
return role;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user