feat:(feature/REQ-2750-1) 改造list接口,支持超管角色合并

This commit is contained in:
lilong 2024-10-09 14:13:02 +08:00
parent e34157104b
commit 5390a9b9f1
6 changed files with 263 additions and 88 deletions

View File

@ -1365,7 +1365,7 @@ public class RoleServiceImpl extends ServiceImpl<SaasRoleMapper, SaasRole>
// 因为历史是每一个workspaceId一个超管角色现在是一个workspaceType一个超管角色
// 为了兼容历史情况入参有workspaceId且roleType为空或者roleType包含superAdmin的需要组装superAdmin的查询条件
assembleSuperAdminWrapper(param, wrapper);
IPage<SaasRole> page = this.page(PageConverter.toMybatis(param, SaasRole.class), wrapper);
@ -1386,6 +1386,55 @@ public class RoleServiceImpl extends ServiceImpl<SaasRoleMapper, SaasRole>
permissionRelations));
}
/**
* 因为历史是每一个workspaceId一个超管角色现在是一个workspaceType一个超管角色
* 为了兼容历史情况入参有workspaceId且roleType为空或者roleType包含superAdmin的需要组装superAdmin的查询条件
* @param param
* @param wrapper
*/
private void assembleSuperAdminWrapper(PageRoleReq param, QueryWrapper<SaasRole> wrapper) {
if (Objects.isNull(param.getWorkspaceId())
&& CollectionUtils.isEmpty(param.getWorkspaceIds())
&& CollectionUtils.isEmpty(param.getWorkspaceOuPairs())) {
return;
}
if (CollectionUtils.isNotEmpty(param.getRoleTypes())
&& !param.getRoleTypes().contains(RoleTypeEnum.SUPER_ADMIN.getValue())) {
return;
}
List<Long> workspaceIds = Lists.newArrayList();
if (Objects.nonNull(param.getWorkspaceId())) {
workspaceIds.add(param.getWorkspaceId());
}
if (CollectionUtils.isNotEmpty(param.getWorkspaceIds())) {
workspaceIds.addAll(param.getWorkspaceIds());
}
if (CollectionUtils.isNotEmpty(param.getWorkspaceOuPairs())) {
workspaceIds.addAll(param.getWorkspaceOuPairs().stream()
.map(ListRoleUserRelationParam.WorkspaceOuPair::getWorkspaceId)
.collect(Collectors.toList()));
}
GetSimpleWorkspaceReqV2 build = GetSimpleWorkspaceReqV2.builder()
.ids(workspaceIds)
.build();
List<SimpleWorkspaceRes> workspaces = RpcExternalUtil.rpcApolloProcessor(() -> workspaceApi.getListV2(build),
"查询项目信息", build);
if (CollectionUtils.isEmpty(workspaces)) {
return;
}
wrapper.or(j -> {
j.in("workspace_type", Lists.transform(workspaces, SimpleWorkspaceRes::getType));
j.eq("owner_ou_id", 0L);
j.eq("workspace_id", 0L);
j.eq("role_type", RoleTypeEnum.SUPER_ADMIN.getValue());
});
}
private Set<Long> resolveRoleGroupCodes(PageRoleReq param) {
if (CollectionUtils.isEmpty(param.getRoleGroupCodes())) {
return Collections.emptySet();
@ -1703,6 +1752,11 @@ public class RoleServiceImpl extends ServiceImpl<SaasRoleMapper, SaasRole>
ListRoleUserRelationParam listRoleUserRelationParam = ListRoleUserRelationParam.builder()
.roleIds(roleIds)
.needUsers(param.getNeedRoleUser())
.workspaceId(param.getWorkspaceId())
.workspaceIds(Optional.ofNullable(param.getWorkspaceIds())
.map(Sets::newHashSet)
.orElse(null))
.workspaceOuPairs(param.getWorkspaceOuPairs())
.build();
List<SaasRoleUserV2DTO> saasRoleUserV2DTOS = saasRoleUserRelationService.listV2(listRoleUserRelationParam);

View File

@ -286,12 +286,11 @@ public class RoleUserService implements SaasRoleUserService {
@Transactional(rollbackFor = Exception.class)
public void createSuperAdminRole(CreateSuperAdminRoleParam param) {
//获取超管角色
// 因为以前一个项目一个超管但是上线为了保证兼容所以这里要根据workspaceId = -1,ouId = -1
// 因为以前一个项目一个超管
// 不根据code查询是因为增加一个业务的超管就需要维护
ListRoleReq listRoleReq = ListRoleReq.builder()
.roleTypes(Lists.newArrayList(RoleTypeEnum.SUPER_ADMIN.getValue()))
.workspaceType(param.getWorkspaceType())
.needPresetRole(true)
.build();
SaasRoleRes superAdmin = roleService.list(listRoleReq).stream()
.findFirst()
@ -354,7 +353,6 @@ public class RoleUserService implements SaasRoleUserService {
.ouId(e.getOuId())
.build())
.collect(Collectors.toList()))
.needPresetRole(true)
.build();
List<SaasRoleRes> roleList = roleService.list(listRoleReq);
if (CollectionUtils.isEmpty(roleList)) {
@ -397,7 +395,6 @@ public class RoleUserService implements SaasRoleUserService {
.workspaceId(param.getWorkspaceId())
.ouId(param.getOuId())
.build()))
.needPresetRole(true)
.build();
List<SaasRoleRes> saasRoles = roleService.list(listRoleReq);
if (CollectionUtils.isEmpty(saasRoles)) {

View File

@ -88,12 +88,121 @@ class RoleServiceTest extends BaseTest {
.roleIds(Lists.newArrayList(24425L, 25324L))
.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(), 6);
// 只查询预设角色和指定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(), 4);
// 根据workspaceIdworkspaceTypeCode查询
}
@Test
void isSuperAdmin() {
List<IsSuperAdminRes> superAdmin = roleService.isSuperAdmin(Lists.newArrayList(QueryByIdentityIdTypeReq.builder()
.identityId(28801L)
.identityType(3)
.workspaceId(3L)
.ouId(4L)
.personId(24510L)
.build()));
Assertions.assertEquals(superAdmin.size(), 1);
Assertions.assertTrue(superAdmin.get(0).getIsSuperAdmin());
superAdmin = roleService.isSuperAdmin(Lists.newArrayList(QueryByIdentityIdTypeReq.builder()
.identityId(28802L)
.identityType(3)
.workspaceId(3L)
.ouId(4L)
.personId(24511L)
.build()));
Assertions.assertEquals(superAdmin.size(), 1);
Assertions.assertFalse(superAdmin.get(0).getIsSuperAdmin());
}
@Test
void listSuperAdmin() {
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(3L))
.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()));
Mockito.when(userProfileServiceApi.postPersonProfiles(Mockito.any()))
.thenReturn(CommonResponse.success(Lists.newArrayList()));
// 根据workspaceId查询所有角色包括超管
roles = roleService.list(ListRoleReq.builder()
List<SaasRoleRes> roles = roleService.list(ListRoleReq.builder()
.workspaceOuPairs(Lists.newArrayList(ListRoleUserRelationParam.WorkspaceOuPair.builder()
.workspaceId(3L)
.ouId(4L)
@ -181,92 +290,26 @@ class RoleServiceTest extends BaseTest {
.collect(Collectors.toList())
.size(), 1);
// 根据workspaceId查询自定义角色预设角色
}
@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))
// 查询所有角色
roles = roleService.list(ListRoleReq.builder()
.build());
Assertions.assertEquals(query.size(), 5);
// 只查询预设角色和指定workspaceId的角色
Assertions.assertEquals(roles.size(), 9);
// 查询所有角色
// 只查询指定workspaceId的超管角色
query = roleService.query(QuerySaasRoleReq.builder()
.roleType(Lists.newArrayList(RoleTypeEnum.SUPER_ADMIN.getValue()))
.workspaceId(Lists.newArrayList(3L))
.ouId(Lists.newArrayList(4L))
// 查询预设角色
roles = roleService.list(ListRoleReq.builder()
.needPresetRole(true)
.build());
Assertions.assertEquals(query.size(), 1);
// 只查询指定workspaceId的超管角色
Assertions.assertEquals(roles.size(), 4);
// 查询预设角色
// 只查询指定workspaceId的超管角色预设角色
query = roleService.query(QuerySaasRoleReq.builder()
.roleType(Lists.newArrayList(RoleTypeEnum.SUPER_ADMIN.getValue(),
RoleTypeEnum.INIT.getValue()))
.workspaceId(Lists.newArrayList(3L))
.ouId(Lists.newArrayList(4L))
// 直接查询超管角色
roles = roleService.list(ListRoleReq.builder()
.roleTypes(Lists.newArrayList(RoleTypeEnum.SUPER_ADMIN.getValue()))
.workspaceType(2)
.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(), 4);
// 根据workspaceIdworkspaceTypeCode查询
}
@Test
void isSuperAdmin() {
List<IsSuperAdminRes> superAdmin = roleService.isSuperAdmin(Lists.newArrayList(QueryByIdentityIdTypeReq.builder()
.identityId(28801L)
.identityType(3)
.workspaceId(3L)
.ouId(4L)
.personId(24510L)
.build()));
Assertions.assertEquals(superAdmin.size(), 1);
Assertions.assertTrue(superAdmin.get(0).getIsSuperAdmin());
superAdmin = roleService.isSuperAdmin(Lists.newArrayList(QueryByIdentityIdTypeReq.builder()
.identityId(28802L)
.identityType(3)
.workspaceId(3L)
.ouId(4L)
.personId(24511L)
.build()));
Assertions.assertEquals(superAdmin.size(), 1);
Assertions.assertFalse(superAdmin.get(0).getIsSuperAdmin());
Assertions.assertEquals(roles.size(), 1);
// 直接查询超管角色
}
}

View File

@ -0,0 +1,55 @@
package cn.axzo.tyr.server.service.impl;
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.model.roleuser.dto.SuperAminInfoResp;
import cn.axzo.tyr.client.model.roleuser.req.SuperAdminParam;
import com.google.common.collect.Lists;
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.List;
class RoleUserServiceTest extends BaseTest {
@Autowired
private RoleUserService roleUserService;
@Autowired
private MysqlDataLoader mysqlDataLoader;
@Autowired
private WorkspaceApi workspaceApi;
@BeforeEach
@Override
public void setup() {
super.setup();
mysqlDataLoader.loadFromClassName(getClass().getSimpleName());
MockitoAnnotations.initMocks(this);
}
@Test
void superAdminList() {
Mockito.when(workspaceApi.getListV2(Mockito.eq(GetSimpleWorkspaceReqV2.builder()
.ids(Lists.newArrayList(3L))
.build())))
.thenReturn(Results.ok(Lists.newArrayList(SimpleWorkspaceRes.builder()
.type(2)
.build())));
List<SuperAminInfoResp> superAminInfoResps = roleUserService.superAdminList(SuperAdminParam.builder()
.workspaceId(3L)
.ouId(4L)
.build());
Assertions.assertEquals(superAminInfoResps.size(), 1);
}
}

View File

@ -55,6 +55,7 @@ INSERT INTO saas_role_user_relation (id, identity_id, role_id, identity_type, na
VALUES (1157571, 40, 101101, 3, 2232, 8, 1, 0, 0, 0, '2024-09-27 09:39:49', '2024-09-27 09:39:48', 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 (197520, 28802, 24425, 3, 24511, 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 (1157572, 40, 101101, 3, 2232, 3, 4, 0, 0, 0, '2024-09-27 09:39:49', '2024-09-27 09:39:48', 0, 0, 2);
#-->SaasRoleUserRelationServiceImplTest.sql

View File

@ -0,0 +1,25 @@
#-->DEFAULT
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_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, 28801, 101100, 3, 24510, 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, 40, 101101, 3, 2232, 8, 1, 0, 0, 0, '2024-09-27 09:39:49', '2024-09-27 09:39:48', 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 (197520, 28802, 24425, 3, 24511, 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 (1157572, 40, 101101, 3, 2232, 3, 4, 0, 0, 0, '2024-09-27 09:39:49', '2024-09-27 09:39:48', 0, 0, 2);
#-->SaasRoleUserRelationServiceImplTest.sql