feat:(REQ-2524) 修改鉴权菜单的返回,角色没有权限,抛NPE的问题

This commit is contained in:
lilong 2024-06-04 14:00:01 +08:00
parent d61723aadd
commit f0ea5340e4
5 changed files with 114 additions and 9 deletions

View File

@ -1,10 +1,9 @@
package cn.axzo.tyr.server.controller;
import cn.axzo.basics.common.util.TreeUtil;
import cn.axzo.tyr.client.common.enums.FeatureResourceType;
import cn.axzo.tyr.client.model.req.CommonDictQueryReq;
import cn.axzo.tyr.client.model.req.FeatureResourceTreeSaveReq;
import cn.axzo.tyr.client.model.req.GetFeatureResourceTreeReq;
import cn.axzo.tyr.client.model.req.PermissionCheckReq;
import cn.axzo.tyr.client.model.req.QuerySaasRoleGroupReq;
import cn.axzo.tyr.client.model.res.CommonDictResp;
import cn.axzo.tyr.client.model.res.FeatureResourceDTO;
@ -23,6 +22,7 @@ import cn.axzo.tyr.server.service.RoleService;
import cn.axzo.tyr.server.service.SaasCommonDictService;
import cn.axzo.tyr.server.service.SaasFeatureResourceService;
import cn.axzo.tyr.server.service.SaasRoleGroupService;
import cn.axzo.tyr.server.service.TyrSaasAuthService;
import cn.axzo.tyr.server.service.impl.SaasFeatureResourceCacheService;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
@ -73,6 +73,8 @@ public class PrivateController {
private RolePermissionCacheService rolePermissionCacheService;
@Autowired
private ProductPermissionCacheService productPermissionCacheService;
@Autowired
private TyrSaasAuthService tyrSaasAuthService;
@ -274,6 +276,11 @@ public class PrivateController {
return productPermissionCacheService.hasProductIds(request);
}
@PostMapping("/api/private/permission/auth")
public Object authPermission(@Validated @RequestBody PermissionCheckReq request) {
return tyrSaasAuthService.authPermission(request);
}
@Data
@Builder
@NoArgsConstructor

View File

@ -75,6 +75,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@ -447,10 +448,12 @@ public class TyrSaasAuthServiceImpl implements TyrSaasAuthService {
}
log.info("build permission for role:{}", role.getId());
Set<Long> rolePermissionIds = role.getSaasPermissions().stream()
.filter(Objects::nonNull)
.map(SaasPermissionRes::getId)
.collect(Collectors.toSet());
Set<Long> rolePermissionIds = Optional.ofNullable(role.getSaasPermissions())
.map(e -> e.stream()
.filter(Objects::nonNull)
.map(SaasPermissionRes::getId)
.collect(Collectors.toSet()))
.orElseGet(Sets::newHashSet);
//角色标签类型匹配产品标签类型
Set<Long> productPermissionIds = productFeatures.stream()
.filter(productFeatureRelationVO -> Objects.equals(productFeatureRelationVO.getDictCode(), String.valueOf(role.getProductUnitType())))
@ -1053,6 +1056,7 @@ public class TyrSaasAuthServiceImpl implements TyrSaasAuthService {
List<SaasFeature> saasFeatures = saasFeatureDao.lambdaQuery()
.in(SaasFeature::getFeatureCode, req.getFeatureCodes())
.eq(SaasFeature::getIsDelete, TableIsDeleteEnum.NORMAL.value)
.eq(StringUtils.isNotBlank(req.getTerminal()), SaasFeature::getTerminal, req.getTerminal())
.list();
if (CollectionUtils.isEmpty(saasFeatures)) {
log.info("featureCode not found:{}", req.getFeatureCodes());

View File

@ -0,0 +1,62 @@
package cn.axzo.tyr.server.service.impl;
import cn.axzo.tyr.base.BaseTest;
import cn.axzo.tyr.base.MysqlDataLoader;
import cn.axzo.tyr.client.model.req.PermissionCheckReq;
import cn.axzo.tyr.server.repository.dao.SaasFeatureDao;
import cn.axzo.tyr.server.repository.dao.SaasRoleDao;
import cn.axzo.tyr.server.repository.entity.SaasFeature;
import cn.axzo.tyr.server.service.TyrSaasAuthService;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.jupiter.api.Assertions.*;
class TyrSaasAuthServiceImplTest extends BaseTest {
@Autowired
private MysqlDataLoader mysqlDataLoader;
@Autowired
private TyrSaasAuthService tyrSaasAuthService;
@Autowired
private SaasFeatureDao saasFeatureDao;
@BeforeEach
@Override
public void setup() {
super.setup();
mysqlDataLoader.loadFromClassName(getClass().getSimpleName());
}
@Test
void authPermission() {
saasFeatureDao.save(SaasFeature.builder()
.featureCode("CMS_001")
.terminal("CMS")
.build());
PermissionCheckReq permissionCheckReq = PermissionCheckReq.builder()
.ouId(5708L)
.workspaceId(300L)
.personId(42642L)
.featureCodes(Lists.newArrayList("dfff"))
.build();
boolean result = tyrSaasAuthService.authPermission(permissionCheckReq);
Assertions.assertFalse(result);
result = tyrSaasAuthService.authPermission(PermissionCheckReq.builder()
.ouId(5708L)
.workspaceId(300L)
.personId(42642L)
.featureCodes(Lists.newArrayList("CMS_001"))
.terminal("sdf")
.build());
Assertions.assertFalse(result);
}
}

View File

@ -0,0 +1,2 @@
### DEFAULT

View File

@ -107,7 +107,37 @@ CREATE TABLE `saas_role` (
KEY `ou_workspace_idx` (`owner_ou_id`,`workspace_id`,`is_delete`)
) ENGINE=InnoDB AUTO_INCREMENT=102623 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='saas-角色';
CREATE TABLE `saas_feature_resource` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '自增主键ID',
`feature_code` varchar(64) NOT NULL DEFAULT '' COMMENT '资源编码-权限码',
`feature_name` varchar(64) NOT NULL DEFAULT '' COMMENT '资源名称',
`feature_type` tinyint NOT NULL DEFAULT '0' COMMENT '资源类型1-菜单 2-页面 3-应用入口 4-组件',
`terminal` varchar(32) NOT NULL DEFAULT '' COMMENT '资源所属端',
`component_type` tinyint NOT NULL DEFAULT '0' COMMENT '组件细分类型 1-跳转子页面 2-跳转公共组件 3-弹出窗口 4-下拉项 5-操作按钮 6-数据卡片 7-站外跳转',
`parent_id` bigint NOT NULL DEFAULT '0' COMMENT '上级资源ID',
`path` varchar(255) DEFAULT '0' COMMENT '资源ID层级路径 逗号分隔',
`display_order` int NOT NULL DEFAULT '0' COMMENT '展示顺序',
`status` tinyint NOT NULL DEFAULT '1' COMMENT '资源状态 0-隐藏 1-展示',
`icon` varchar(255) NOT NULL DEFAULT '' COMMENT '资源图标',
`redirect_type` tinyint NOT NULL DEFAULT '0' COMMENT '跳转类型 1-站内跳转 2-站外跳转',
`link_url` varchar(255) NOT NULL DEFAULT '' COMMENT '资源跳转URI',
`link_type` tinyint NOT NULL DEFAULT '0' COMMENT '路由类型1-PC 2-小程序 3-原生',
`link_ext` varchar(255) NOT NULL DEFAULT '' COMMENT 'APP适配参数',
`app_item_id` int NOT NULL DEFAULT '0' COMMENT '小程序id',
`sync_version` int NOT NULL DEFAULT '0' COMMENT '资源同步版本',
-- `extra` json DEFAULT NULL COMMENT '扩展字段',
`extra` varchar(255) DEFAULT NULL COMMENT '扩展字段',
`auth_type` tinyint NOT NULL DEFAULT '1' COMMENT '授权类型0-全部角色 1-指定角色',
`sub_auth_type` tinyint NOT NULL DEFAULT '0' COMMENT '子级鉴权类型 0-不鉴权1-鉴权',
`create_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`create_by` bigint NOT NULL DEFAULT '0' COMMENT '创建人',
`update_by` bigint NOT NULL DEFAULT '0' COMMENT '更新人',
`is_delete` bigint NOT NULL DEFAULT '0' COMMENT '删除标识',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=459 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='功能资源表';
alter table saas_feature_resource
add column `workspace_type` tinyint DEFAULT '0' COMMENT '1:企业工作台 2;项目工作台' after `path`,
add column `version` int DEFAULT '0' COMMENT '最低版本序列,主要支持版本灰度策略' after `workspace_type`
add column `uni_code` varchar(64) not null default '' comment '唯一编码用于pre环境菜单同步' after `version`;
add column `workspace_type` tinyint DEFAULT '0' COMMENT '1:企业工作台 2;项目工作台' after `path`;
alter table saas_feature_resource add column `version` int DEFAULT '0' COMMENT '最低版本序列,主要支持版本灰度策略' after `workspace_type`;
alter table saas_feature_resource add column `uni_code` varchar(64) not null default '' comment '唯一编码用于pre环境菜单同步' after `version`;