【同步】Boot 和 Cloud 的功能同步
This commit is contained in:
@@ -22,7 +22,7 @@ public enum OAuth2GrantTypeEnum {
|
||||
|
||||
private final String grantType;
|
||||
|
||||
public static OAuth2GrantTypeEnum getByGranType(String grantType) {
|
||||
public static OAuth2GrantTypeEnum getByGrantType(String grantType) {
|
||||
return ArrayUtil.firstMatch(o -> o.getGrantType().equals(grantType), values());
|
||||
}
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ public class AuthController {
|
||||
// 1.3 获得菜单列表
|
||||
Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId));
|
||||
List<MenuDO> menuList = menuService.getMenuList(menuIds);
|
||||
menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单
|
||||
menuList = menuService.filterDisableMenus(menuList);
|
||||
|
||||
// 2. 拼接结果返回
|
||||
return success(AuthConvert.INSTANCE.convert(user, roles, menuList));
|
||||
|
||||
@@ -21,10 +21,10 @@ import cn.iocoder.yudao.module.system.service.oauth2.OAuth2ClientService;
|
||||
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2GrantService;
|
||||
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService;
|
||||
import cn.iocoder.yudao.module.system.util.oauth2.OAuth2Utils;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -105,7 +105,7 @@ public class OAuth2OpenController {
|
||||
@RequestParam(value = "refresh_token", required = false) String refreshToken) { // 刷新模式
|
||||
List<String> scopes = OAuth2Utils.buildScopes(scope);
|
||||
// 1.1 校验授权类型
|
||||
OAuth2GrantTypeEnum grantTypeEnum = OAuth2GrantTypeEnum.getByGranType(grantType);
|
||||
OAuth2GrantTypeEnum grantTypeEnum = OAuth2GrantTypeEnum.getByGrantType(grantType);
|
||||
if (grantTypeEnum == null) {
|
||||
throw exception0(BAD_REQUEST.getCode(), StrUtil.format("未知授权类型({})", grantType));
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ public class MenuController {
|
||||
public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() {
|
||||
List<MenuDO> list = menuService.getMenuListByTenant(
|
||||
new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||
list = menuService.filterDisableMenus(list);
|
||||
list.sort(Comparator.comparing(MenuDO::getSort));
|
||||
return success(BeanUtils.toBean(list, MenuSimpleRespVO.class));
|
||||
}
|
||||
|
||||
@@ -52,6 +52,14 @@ public interface MenuService {
|
||||
*/
|
||||
List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 过滤掉关闭的菜单及其子菜单
|
||||
*
|
||||
* @param list 菜单列表
|
||||
* @return 过滤后的菜单列表
|
||||
*/
|
||||
List<MenuDO> filterDisableMenus(List<MenuDO> list);
|
||||
|
||||
/**
|
||||
* 筛选菜单列表
|
||||
*
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package cn.iocoder.yudao.module.system.service.permission;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuSaveVO;
|
||||
@@ -19,11 +21,11 @@ import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
||||
import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
|
||||
@@ -106,12 +108,57 @@ public class MenuServiceImpl implements MenuService {
|
||||
|
||||
@Override
|
||||
public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) {
|
||||
// 查询所有菜单,并过滤掉关闭的节点
|
||||
List<MenuDO> menus = getMenuList(reqVO);
|
||||
// 开启多租户的情况下,需要过滤掉未开通的菜单
|
||||
tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId())));
|
||||
return menus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuDO> filterDisableMenus(List<MenuDO> menuList) {
|
||||
if (CollUtil.isEmpty(menuList)){
|
||||
return Collections.emptyList();
|
||||
}
|
||||
Map<Long, MenuDO> menuMap = convertMap(menuList, MenuDO::getId);
|
||||
|
||||
// 遍历 menu 菜单,查找不是禁用的菜单,添加到 enabledMenus 结果
|
||||
List<MenuDO> enabledMenus = new ArrayList<>();
|
||||
Set<Long> disabledMenuCache = new HashSet<>(); // 存下递归搜索过被禁用的菜单,防止重复的搜索
|
||||
for (MenuDO menu : menuList) {
|
||||
if (isMenuDisabled(menu, menuMap, disabledMenuCache)) {
|
||||
continue;
|
||||
}
|
||||
enabledMenus.add(menu);
|
||||
}
|
||||
return enabledMenus;
|
||||
}
|
||||
|
||||
private boolean isMenuDisabled(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> disabledMenuCache) {
|
||||
// 如果已经判定是禁用的节点,直接结束
|
||||
if (disabledMenuCache.contains(node.getId())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 1. 遍历到 parentId 为根节点,则无需判断
|
||||
Long parentId = node.getParentId();
|
||||
if (ObjUtil.equal(parentId, ID_ROOT)) {
|
||||
if (CommonStatusEnum.isDisable(node.getStatus())) {
|
||||
disabledMenuCache.add(node.getId());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 继续遍历 parent 节点
|
||||
MenuDO parent = menuMap.get(parentId);
|
||||
if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuCache)) {
|
||||
disabledMenuCache.add(node.getId());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MenuDO> getMenuList(MenuListReqVO reqVO) {
|
||||
return menuMapper.selectList(reqVO);
|
||||
|
||||
Reference in New Issue
Block a user