feat:(REQ-2750-1) 修改roleService.query接口,支持超管角色合并

This commit is contained in:
lilong 2024-10-08 15:35:12 +08:00
parent 5210de5c3a
commit 53aa5c0ebc
5 changed files with 159 additions and 9 deletions

View File

@ -125,9 +125,9 @@ public class PrivateRoleController {
superAdmin.setDescription(RoleTypeEnum.SUPER_ADMIN.getDesc());
superAdmin.setName(RoleTypeEnum.SUPER_ADMIN.getDesc());
superAdmin.setRoleCode(e.getRoleCode());
superAdmin.setWorkspaceId(-1L);
superAdmin.setWorkspaceId(0L);
superAdmin.setWorkspaceType(e.getWorkspaceType());
superAdmin.setOwnerOuId(-1L);
superAdmin.setOwnerOuId(0L);
superAdmin.setRoleType(RoleTypeEnum.SUPER_ADMIN.getValue());
superAdmin.setIsDelete(0L);
superAdmin.setCreateAt(now);

View File

@ -1,5 +1,8 @@
package cn.axzo.tyr.server.service.impl;
import cn.axzo.apollo.workspace.api.workspace.WorkspaceApi;
import cn.axzo.apollo.workspace.api.workspace.req.GetSimpleWorkspaceReqV2;
import cn.axzo.apollo.workspace.api.workspace.res.SimpleWorkspaceRes;
import cn.axzo.basics.common.constant.enums.TableIsDeleteEnum;
import cn.axzo.basics.common.exception.ServiceException;
import cn.axzo.basics.profiles.api.UserProfileServiceApi;
@ -87,6 +90,7 @@ import cn.axzo.tyr.server.service.SaasRoleGroupRelationService;
import cn.axzo.tyr.server.service.SaasRoleGroupService;
import cn.axzo.tyr.server.service.SaasRoleUserRelationService;
import cn.axzo.tyr.server.util.RpcInternalUtil;
import cn.axzo.tyr.server.utils.RpcExternalUtil;
import cn.azxo.framework.common.constatns.Constants;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
@ -205,6 +209,8 @@ public class RoleServiceImpl extends ServiceImpl<SaasRoleMapper, SaasRole>
@Autowired
private MqProducer mqProducer;
@Autowired
private WorkspaceApi workspaceApi;
private static final String TARGET_TYPE = "saasFeatureResourceId";
@ -344,9 +350,39 @@ public class RoleServiceImpl extends ServiceImpl<SaasRoleMapper, SaasRole>
.eq(Objects.nonNull(req.getProductUnitType()), SaasRole::getProductUnitType, req.getProductUnitType())
.orderByDesc(BaseEntity::getId)
.list();
list.addAll(listSuperAdmin(req));
return getByIds(list.stream().map(BaseEntity::getId).collect(Collectors.toList()), req.getIsCommon(), req.getWorkspaceId(), req.getOuId(), req.getIncludePermissionGroup(), req.getIncludeSpecialRole());
}
/**
* 历史每个workspaceId都有一个超管角色现在每个业务类一个超管角色
* 新的超管角色workspaceId = 0(不是-1是因为很多接口默认就会查询-1的数据要过滤掉超管角色比较复杂)
* 历史传入workspaceId时会查询出项目的超管角色所以有传workspaceId时需要兼容历史接口
* @param req
* @return
*/
private List<SaasRole> listSuperAdmin(QuerySaasRoleReq req) {
if (CollectionUtils.isEmpty(req.getWorkspaceId())) {
return Collections.emptyList();
}
GetSimpleWorkspaceReqV2 build = GetSimpleWorkspaceReqV2.builder()
.ids(req.getWorkspaceId())
.build();
List<SimpleWorkspaceRes> workspaces = RpcExternalUtil.rpcApolloProcessor(() -> workspaceApi.getListV2(build),
"查询项目信息", build);
if (CollectionUtils.isEmpty(workspaces)) {
return Collections.emptyList();
}
return saasRoleDao.lambdaQuery()
.in(SaasRole::getWorkspaceType, Lists.transform(workspaces, SimpleWorkspaceRes::getType))
.eq(SaasRole::getRoleType, RoleTypeEnum.SUPER_ADMIN.getValue())
.list();
}
@Override
public List<QueryBatchByIdentityIdTypeRes> queryBatchByIdentityIdType(List<QueryByIdentityIdTypeReq> req) {
List<QueryBatchByIdentityIdTypeRes> result = new ArrayList<>();

View File

@ -1,5 +1,6 @@
package cn.axzo.tyr.base;
import cn.axzo.apollo.workspace.api.workspace.WorkspaceApi;
import cn.axzo.framework.rocketmq.Event;
import cn.axzo.framework.rocketmq.EventProducer;
import cn.axzo.thrones.client.saas.ServicePkgClient;
@ -43,6 +44,9 @@ public class TestConfig {
@MockBean
private ServicePkgClient servicePkgClient;
@MockBean
private WorkspaceApi workspaceApi;
@Bean
@Primary
public EventProducer<Object> dummyEventProducer() {

View File

@ -1,23 +1,26 @@
package cn.axzo.tyr.server.service;
import cn.axzo.apollo.core.web.Results;
import cn.axzo.apollo.workspace.api.workspace.WorkspaceApi;
import cn.axzo.apollo.workspace.api.workspace.req.GetSimpleWorkspaceReqV2;
import cn.axzo.apollo.workspace.api.workspace.res.SimpleWorkspaceRes;
import cn.axzo.tyr.base.BaseTest;
import cn.axzo.tyr.base.MysqlDataLoader;
import cn.axzo.tyr.client.common.enums.RoleTypeEnum;
import cn.axzo.tyr.client.model.req.ListRoleReq;
import cn.axzo.tyr.client.model.req.QuerySaasRoleReq;
import cn.axzo.tyr.client.model.res.SaasRoleRes;
import cn.axzo.tyr.client.model.vo.SaasRoleVO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
class RoleServiceTest extends BaseTest {
@ -25,12 +28,15 @@ class RoleServiceTest extends BaseTest {
private RoleService roleService;
@Autowired
private MysqlDataLoader mysqlDataLoader;
@Autowired
private WorkspaceApi workspaceApi;
@BeforeEach
@Override
public void setup() {
super.setup();
mysqlDataLoader.loadFromClassName(getClass().getSimpleName());
MockitoAnnotations.initMocks(this);
}
@Test
@ -73,4 +79,68 @@ class RoleServiceTest extends BaseTest {
.build());
Assertions.assertEquals(roles.size(), 2);
}
@Test
void query() {
Mockito.when(workspaceApi.getListV2(Mockito.eq(GetSimpleWorkspaceReqV2.builder()
.ids(Lists.newArrayList(3L, -1L))
.build())))
.thenReturn(Results.ok(Lists.newArrayList(SimpleWorkspaceRes.builder()
.type(2)
.build())));
Mockito.when(workspaceApi.getListV2(Mockito.eq(GetSimpleWorkspaceReqV2.builder()
.ids(Lists.newArrayList(-1L))
.build())))
.thenReturn(Results.ok(Lists.newArrayList()));
// 只查询预设角色,workspaceId = -1
List<SaasRoleVO> query = roleService.query(QuerySaasRoleReq.builder().build());
Assertions.assertEquals(query.size(), 4);
// 只查询预设角色,workspaceId = -1
// 只查询预设角色和指定workspaceId的角色
query = roleService.query(QuerySaasRoleReq.builder()
.workspaceId(Lists.newArrayList(3L))
.ouId(Lists.newArrayList(4L))
.build());
Assertions.assertEquals(query.size(), 5);
// 只查询预设角色和指定workspaceId的角色
// 只查询指定workspaceId的超管角色
query = roleService.query(QuerySaasRoleReq.builder()
.roleType(Lists.newArrayList(RoleTypeEnum.SUPER_ADMIN.getValue()))
.workspaceId(Lists.newArrayList(3L))
.ouId(Lists.newArrayList(4L))
.build());
Assertions.assertEquals(query.size(), 1);
// 只查询指定workspaceId的超管角色
// 只查询指定workspaceId的超管角色预设角色
query = roleService.query(QuerySaasRoleReq.builder()
.roleType(Lists.newArrayList(RoleTypeEnum.SUPER_ADMIN.getValue(),
RoleTypeEnum.INIT.getValue()))
.workspaceId(Lists.newArrayList(3L))
.ouId(Lists.newArrayList(4L))
.build());
Assertions.assertEquals(query.size(), 5);
// 只查询指定workspaceId的超管角色预设角色
// 根据workspaceTypeCode查询
query = roleService.query(QuerySaasRoleReq.builder()
.workspaceTypeCode(Lists.newArrayList("2"))
.build());
Assertions.assertEquals(query.size(), 3);
// 根据workspaceTypeCode查询
// 根据workspaceIdworkspaceTypeCode查询
query = roleService.query(QuerySaasRoleReq.builder()
.workspaceTypeCode(Lists.newArrayList("2"))
.workspaceId(Lists.newArrayList(3L))
.ouId(Lists.newArrayList(4L))
.build());
Assertions.assertEquals(query.size(), 3);
// 根据workspaceIdworkspaceTypeCode查询
}
}

View File

@ -10,7 +10,47 @@ INSERT INTO saas_role_group_relation (id, role_id, saas_role_group_id, is_delete
INSERT INTO saas_role_group_relation (id, role_id, saas_role_group_id, is_delete, create_at, update_at) VALUES (25, 25326, 13, 0, '2023-10-23 17:50:00', '2023-10-23 17:49:59');
INSERT INTO saas_role_group_relation (id, role_id, saas_role_group_id, is_delete, create_at, update_at) VALUES (26, 25327, 13, 0, '2023-10-23 17:50:00', '2023-10-23 17:49:59');
INSERT INTO saas_role_group (id, workspace_type_code, ou_type_code, name, workspace_id, ou_id, parent_id, sort, code, category_code, is_delete, create_at, update_at) VALUES (6, '6', '6', '研发中心', -1, -1, 0, 3, 'A1', '', 0, '2023-09-19 14:31:19', '2024-05-29 09:33:24');
INSERT INTO saas_role_group (id, workspace_type_code, ou_type_code, name, workspace_id, ou_id, parent_id, sort, code, category_code, is_delete, create_at, update_at) VALUES (13, '2', '5', '专业分包', -1, -1, 0, 1, 'A2', '', 0, '2023-10-23 17:49:59', '2024-08-15 18:39:13');
INSERT INTO saas_role_group (id, workspace_type_code, ou_type_code, name, workspace_id, ou_id, parent_id, sort, code, category_code, is_delete, create_at, update_at)
VALUES (6, '6', '6', '研发中心', -1, -1, 0, 3, 'A1', '', 0, '2023-09-19 14:31:19', '2024-05-29 09:33:24');
INSERT INTO saas_role_group (id, workspace_type_code, ou_type_code, name, workspace_id, ou_id, parent_id, sort, code, category_code, is_delete, create_at, update_at)
VALUES (13, '2', '5', '专业分包', -1, -1, 0, 1, 'A2', '', 0, '2023-10-23 17:49:59', '2024-08-15 18:39:13');
-- INSERT INTO saas_role (id, NAME, description, role_type, role_code, workspace_id, owner_ou_id, product_unit_type, workspace_type, is_delete, create_at, update_at, create_by, update_by, fit_ou_type_bit, fit_ou_node_type_bit, position_template_id, project_team_manage_role_resource_id, from_pre_role_id, job_code, is_display, sort, enabled)
-- VALUES (3414, '超级管理员', '', 'super_admin', '', 3, 4, 1, 2, 0, '2022-10-19 15:52:33', '2024-09-29 17:16:27', 0, 0, 65535, 65535, 0, 0, 0, '', 1, 0, 1);
-- INSERT INTO saas_role (id, NAME, description, role_type, role_code, workspace_id, owner_ou_id, product_unit_type, workspace_type, is_delete, create_at, update_at, create_by, update_by, fit_ou_type_bit, fit_ou_node_type_bit, position_template_id, project_team_manage_role_resource_id, from_pre_role_id, job_code, is_display, sort, enabled)
-- VALUES (3415, '超级管理员', '', 'super_admin', '', 8, 1, 1, 2, 0, '2022-10-19 15:52:33', '2024-09-29 17:16:27', 0, 0, 65535, 65535, 0, 0, 0, '', 1, 0, 1);
INSERT INTO saas_role (id, NAME, description, role_type, role_code, workspace_id, owner_ou_id, product_unit_type, workspace_type, is_delete, create_at, update_at, create_by, update_by, fit_ou_type_bit, fit_ou_node_type_bit, position_template_id, project_team_manage_role_resource_id, from_pre_role_id, job_code, is_display, sort, enabled)
VALUES (101100, '超级管理员', '超级管理员', 'super_admin', 'ou_superadmin', 0, 0, 7, 1, 0, '2024-09-25 21:47:42', '2024-09-29 17:17:27', 2051297, 2051297, 1, 65535, 0, null, 0, '', 1, 0, 1);
INSERT INTO saas_role (id, NAME, description, role_type, role_code, workspace_id, owner_ou_id, product_unit_type, workspace_type, is_delete, create_at, update_at, create_by, update_by, fit_ou_type_bit, fit_ou_node_type_bit, position_template_id, project_team_manage_role_resource_id, from_pre_role_id, job_code, is_display, sort, enabled)
VALUES (101101, '超级管理员', '超级管理员', 'super_admin', 'pro_superadmin', 0, 0, 1, 2, 0, '2024-09-25 21:47:42', '2024-09-29 17:17:27', 2051297, 2051297, 1, 65535, 0, null, 0, '', 1, 0, 1);
INSERT INTO saas_role (id, NAME, description, role_type, role_code, workspace_id, owner_ou_id, product_unit_type, workspace_type, is_delete, create_at, update_at, create_by, update_by, fit_ou_type_bit, fit_ou_node_type_bit, position_template_id, project_team_manage_role_resource_id, from_pre_role_id, job_code, is_display, sort, enabled)
VALUES (101102, '超级管理员', '超级管理员', 'super_admin', 'oms_superadmin', 0, 0, 6, 6, 0, '2024-09-25 21:47:42', '2024-09-29 17:17:27', 2051297, 2051297, 1, 65535, 0, null, 0, '', 1, 0, 1);
INSERT INTO saas_role (id, NAME, description, role_type, role_code, workspace_id, owner_ou_id, product_unit_type, workspace_type, is_delete, create_at, update_at, create_by, update_by, fit_ou_type_bit, fit_ou_node_type_bit, position_template_id, project_team_manage_role_resource_id, from_pre_role_id, job_code, is_display, sort, enabled)
VALUES (101103, '超级管理员', '超级管理员', 'super_admin', 'zw_superadmin', 0, 0, 3, 3, 0, '2024-09-25 21:47:42', '2024-09-29 17:17:27', 2051297, 2051297, 1, 65535, 0, null, 0, '', 1, 0, 1);
INSERT INTO saas_role_group_relation (id, role_id, saas_role_group_id, is_delete, create_at, update_at)
VALUES (549, 101100, 18, 0, '2024-09-25 21:47:42', '2024-09-25 21:47:42');
INSERT INTO saas_role_group_relation (id, role_id, saas_role_group_id, is_delete, create_at, update_at)
VALUES (550, 101101, 14, 0, '2024-09-25 21:47:42', '2024-09-25 21:47:42');
INSERT INTO saas_role_group_relation (id, role_id, saas_role_group_id, is_delete, create_at, update_at)
VALUES (551, 101102, 57, 0, '2024-09-25 21:47:42', '2024-09-25 21:47:42');
INSERT INTO saas_role_group (id, workspace_type_code, ou_type_code, name, workspace_id, ou_id, parent_id, sort, code, category_code, is_delete, create_at, update_at, path)
VALUES (14, '2', '1', '总包单位', -1, -1, 0, 1, '', '', 0, '2023-10-23 17:50:04', '2024-09-10 10:18:44', '14,');
INSERT INTO saas_role_group (id, workspace_type_code, ou_type_code, name, workspace_id, ou_id, parent_id, sort, code, category_code, is_delete, create_at, update_at, path)
VALUES (18, '1', '7', '企业通用', -1, -1, 0, 4, '', '', 0, '2023-10-23 17:50:15', '2024-09-10 10:18:44', '18,');
INSERT INTO saas_role_group (id, workspace_type_code, ou_type_code, name, workspace_id, ou_id, parent_id, sort, code, category_code, is_delete, create_at, update_at, path)
VALUES (57, '6', '6', 'OMS超管组', -1, -1, 0, 9, 'omsSuperAdminGroup', '', 0, '2024-04-17 11:38:28', '2024-09-10 10:18:45', '57,');
INSERT INTO saas_role_user_relation (id, identity_id, role_id, identity_type, natural_person_id, workspace_id, ou_id, resource_type, resource_id, is_delete, create_at, update_at, create_by, update_by, job_type)
VALUES (197519, 28801, 3414, 3, 24510, 3, 4, 0, 0, 0, '2024-01-18 16:36:16', '2024-09-29 17:16:39', 0, 0, 2);
INSERT INTO saas_role_user_relation (id, identity_id, role_id, identity_type, natural_person_id, workspace_id, ou_id, resource_type, resource_id, is_delete, create_at, update_at, create_by, update_by, job_type)
VALUES (183677, 40, 3415, 3, 2232, 8, 1, 0, 0, 0, '2023-10-06 15:13:35', '2024-09-29 17:16:39', 0, 0, 2);
INSERT INTO saas_role_user_relation (id, identity_id, role_id, identity_type, natural_person_id, workspace_id, ou_id, resource_type, resource_id, is_delete, create_at, update_at, create_by, update_by, job_type)
VALUES (1158315, 30903, 101100, 3, 25998, 3, 4, 0, 0, 0, '2024-09-29 16:33:31', '2024-09-29 16:33:31', 0, 0, 2);
INSERT INTO saas_role_user_relation (id, identity_id, role_id, identity_type, natural_person_id, workspace_id, ou_id, resource_type, resource_id, is_delete, create_at, update_at, create_by, update_by, job_type)
VALUES (1157571, 2007583, 101101, 3, 60411, 8, 1, 0, 0, 0, '2024-09-27 09:39:49', '2024-09-27 09:39:48', 0, 0, 2);
#-->SaasRoleUserRelationServiceImplTest.sql