Merge remote-tracking branch 'origin/feature/REQ-1502' into feature/REQ-1502

This commit is contained in:
张弘昊 2023-10-19 17:25:18 +08:00
commit 2defb39906
8 changed files with 133 additions and 49 deletions

View File

@ -3,6 +3,8 @@ package cn.axzo.tyr.client.model.req;
import cn.axzo.basics.common.page.PageRequest;
import lombok.Data;
import java.util.List;
/**
* 角色-含用户查询参数
*
@ -21,4 +23,7 @@ public class RoleWithUserQueryReq extends PageRequest {
/** 工作台ID **/
private Long workspaceId;
/** 企业工作台参建类型 **/
private Integer workspaceJoinType;
}

View File

@ -1,5 +1,6 @@
package cn.axzo.tyr.server;
import cn.axzo.tyr.server.job.CMSRoleJobHandler;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
@ -40,15 +41,15 @@ public class TyrApplication {
env.getProperty("spring.rabbitmq.username") +
"\n----------------------------------------------------------");
// try {
// test();
// } catch (Exception e) {
// e.printStackTrace();
// }
try {
test();
} catch (Exception e) {
e.printStackTrace();
}
}
// public static void test() throws Exception {
// OMSRoleJobHandler executor = SpringUtil.getBean(OMSRoleJobHandler.class);
// executor.execute(null);
// }
public static void test() throws Exception {
CMSRoleJobHandler executor = SpringUtil.getBean(CMSRoleJobHandler.class);
executor.execute(null);
}
}

View File

@ -72,6 +72,10 @@ public class CMSRoleJobHandler extends IJobHandler {
@Autowired
SaasPreGroupRoleRelationDao saasPreGroupRoleRelationDao;
private Set<Long> deleteSaasPermissionGroupId;
private Set<Long> deletePgroupPermissionRelationId;
private Set<Long> deleteRoleId;
/**
* 清洗CMS角色相关数据(先通过SQL检查和清除脏数据要不然无法保证各个实体的关联关系)
*
@ -83,9 +87,14 @@ public class CMSRoleJobHandler extends IJobHandler {
@Override
@XxlJob("CMSRoleJobHandler")
public ReturnT<String> execute(String s) throws Exception {
// 每次执行都初始化待删除列表
deleteSaasPermissionGroupId = new HashSet<>();
deletePgroupPermissionRelationId = new HashSet<>();
deleteRoleId = new HashSet<>();
log.info("CMSRoleJobHandler start");
buildProjectRole(saasPreTempalteIdOfProject, "2");
buildOuRole(saasPreTempalteIdOfOu, "1");
delete(deleteSaasPermissionGroupId,deletePgroupPermissionRelationId,deleteRoleId);
log.info("CMSRoleJobHandler end");
return ReturnT.SUCCESS;
}
@ -114,7 +123,7 @@ public class CMSRoleJobHandler extends IJobHandler {
// 根据模板id查询角色列表
List<SaasPreRole> role = saasPreRoleDao.lambdaQuery()
.in(SaasPreRole::getTemplateId, templateId)
.eq(SaasPreRole::getFitOuTypeBit, Arrays.asList(64))
.eq(SaasPreRole::getFitOuTypeBit, 64)
.eq(BaseEntity::getIsDelete,0)
.list();
saveRole(role,workspaceTypCode,"7","企业通用");
@ -141,23 +150,32 @@ public class CMSRoleJobHandler extends IJobHandler {
// 根据角色id查询角色权限集关联关系
List<SaasPreGroupRoleRelation> pgroupRoleRelation = saasPreGroupRoleRelationDao.lambdaQuery()
.eq(SaasPreGroupRoleRelation::getPreRoleId, preRole.getId())
.eq(BaseEntity::getIsDelete, 0)
.list();
if (CollectionUtils.isEmpty(pgroupRoleRelation)) {
log.info("未查询到preRole 对应的权限集关联关系 preRole:{}",JSONUtil.toJsonStr(preRole));
return;
}
List<SaasPermissionGroup> oldPermissionGroup = saasPermissionGroupDao.lambdaQuery()
.in(BaseEntity::getId, pgroupRoleRelation.stream().map(SaasPreGroupRoleRelation::getGroupId).collect(Collectors.toList()))
.eq(BaseEntity::getIsDelete, 0)
.list();
if (CollectionUtils.isEmpty(oldPermissionGroup)) {
log.info("未查询到preRole 对应的权限集 preRole:{}",JSONUtil.toJsonStr(preRole));
return;
}
List<SaasPgroupPermissionRelation> oldPgroupPermissionRelation = pgroupPermissionRelationDao.lambdaQuery()
.in(SaasPgroupPermissionRelation::getGroupId, oldPermissionGroup.stream().map(BaseEntity::getId).collect(Collectors.toList()))
.eq(BaseEntity::getIsDelete, 0)
.list();
if (CollectionUtils.isEmpty(oldPgroupPermissionRelation)) {
log.info("未查询到权限集对应的权限点 oldPermissionGroup:{}",JSONUtil.toJsonStr(oldPermissionGroup));
return;
}
List<SaasFeature> feature = featureDao.lambdaQuery()
.in(BaseEntity::getId, oldPgroupPermissionRelation.stream().map(SaasPgroupPermissionRelation::getFeatureId).collect(Collectors.toList()))
.eq(BaseEntity::getIsDelete, 0)
.list();
if (CollectionUtils.isEmpty(feature)) {
log.info("未查询到权限点 oldPgroupPermissionRelation:{}",JSONUtil.toJsonStr(oldPgroupPermissionRelation));
return;
}
// 创建新的权限集
SaasPermissionGroup saasPermissionGroup = new SaasPermissionGroup();
@ -169,14 +187,15 @@ public class CMSRoleJobHandler extends IJobHandler {
saasPermissionGroup.setIsCommon(1);
saasPermissionGroupDao.save(saasPermissionGroup);
// 创建新的权限集权限关联关系
feature.forEach(e -> {
List<SaasPgroupPermissionRelation> saasPgroupPermissionRelationList = feature.stream().map(e -> {
SaasPgroupPermissionRelation saasPgroupPermissionRelation = new SaasPgroupPermissionRelation();
saasPgroupPermissionRelation.setGroupId(saasPermissionGroup.getId());
saasPgroupPermissionRelation.setFeatureId(e.getId());
saasPgroupPermissionRelation.setCreateBy(-1L);
saasPgroupPermissionRelation.setUpdateBy(-1L);
pgroupPermissionRelationDao.save(saasPgroupPermissionRelation);
});
return saasPgroupPermissionRelation;
}).collect(Collectors.toList());
pgroupPermissionRelationDao.saveBatch(saasPgroupPermissionRelationList);
// 创建角色:根据预设角色创建新的角色
SaasRole newRole = new SaasRole();
@ -208,7 +227,7 @@ public class CMSRoleJobHandler extends IJobHandler {
// SELECT * FROM `saas_pgroup_role_relation` WHERE `role_id` in (
// SELECT id FROM saas_role WHERE is_delete=0 and workspace_id != -1 and from_pre_role_id = 0 and role_type!='super_admin' and `NAME` not like '%代班长%' and `NAME` not like '%带班长%')
List<SaasRole> list = roleDao.lambdaQuery()
.eq(SaasRole::getFromPreRoleId, preRole)
.eq(SaasRole::getFromPreRoleId, preRole.getId())
.list();
if (CollectionUtils.isEmpty(list)) {
log.info("未找到preRole下发的role preRole:{}", JSONUtil.toJsonStr(preRole));
@ -221,33 +240,42 @@ public class CMSRoleJobHandler extends IJobHandler {
.set(SaasRoleUserRelation::getRoleId,newRole.getId())
.update();
// 清除老的权限集
oldPermissionGroup.forEach(e ->{
saasPermissionGroupDao.lambdaUpdate()
.eq(BaseEntity::getId, e.getId())
.set(BaseEntity::getIsDelete, e.getId())
.update();
});
// 清除老的权限集权限关联关系
oldPgroupPermissionRelation.forEach(e ->{
pgroupPermissionRelationDao.lambdaUpdate()
.eq(BaseEntity::getId, e.getId())
.set(BaseEntity::getIsDelete, e.getId())
.update();
});
// 清除老的角色
roleDao.lambdaUpdate()
.in(BaseEntity::getId,oldRoleId)
.set(BaseEntity::getIsDelete,1)
.update();
// 清除老的角色和权限集关联关系
roleGroupRelationDao.lambdaUpdate()
.in(SaasRoleGroupRelation::getRoleId,oldRoleId)
.set(BaseEntity::getIsDelete,1)
.update();
// 补充待删除数据
deleteRoleId.addAll(list.stream().map(BaseEntity::getId).collect(Collectors.toSet()));
deleteSaasPermissionGroupId.addAll(oldPermissionGroup.stream().map(BaseEntity::getId).collect(Collectors.toSet()));
deletePgroupPermissionRelationId.addAll(oldPgroupPermissionRelation.stream().map(BaseEntity::getId).collect(Collectors.toSet()));
});
}
/**
* 删除历史数据
* @param deleteSaasPermissionGroupId
* @param deletePgroupPermissionRelationId
* @param deleteRoleId
*/
private void delete(Set<Long> deleteSaasPermissionGroupId, Set<Long> deletePgroupPermissionRelationId, Set<Long> deleteRoleId) {
// 清除老的权限集
saasPermissionGroupDao.lambdaUpdate()
.in(BaseEntity::getId,deleteSaasPermissionGroupId)
.set(BaseEntity::getIsDelete,1)
.update();
// 清除老的权限集权限关联关系
pgroupPermissionRelationDao.lambdaUpdate()
.in(BaseEntity::getId,deletePgroupPermissionRelationId)
.set(BaseEntity::getIsDelete,1)
.update();
// 清除老的角色
roleDao.lambdaUpdate()
.in(BaseEntity::getId,deleteRoleId)
.set(BaseEntity::getIsDelete,1)
.update();
// 清除老的角色和权限集关联关系
pgroupRoleRelationDao.lambdaUpdate()
.in(SaasPgroupRoleRelation::getRoleId,deleteRoleId)
.set(BaseEntity::getIsDelete,1)
.update();
}
private Integer tranceOuTypeBit(Long ouTypeBit) {
Integer ouType;
if (ouTypeBit == 1) {

View File

@ -4,10 +4,13 @@ import cn.axzo.basics.common.constant.enums.TableIsDeleteEnum;
import cn.axzo.pokonyan.config.mybatisplus.BaseEntity;
import cn.axzo.tyr.client.common.enums.RoleTypeEnum;
import cn.axzo.tyr.client.model.enums.IdentityType;
import cn.axzo.tyr.client.model.req.RoleWithUserQueryReq;
import cn.axzo.tyr.server.repository.entity.SaasRole;
import cn.axzo.tyr.server.repository.entity.SaasRoleWithUser;
import cn.axzo.tyr.server.repository.mapper.SaasRoleMapper;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;
@ -91,5 +94,9 @@ public class SaasRoleDao extends ServiceImpl<SaasRoleMapper, SaasRole> {
.list();
}
public Page<SaasRole> pageQueryForOUWorkspace(RoleWithUserQueryReq req) {
IPage<SaasRole> page = new Page<>(req.getPage(), req.getPageSize());
return this.baseMapper.pageQueryForOUWorkspace(page, req.getOuId(), req.getWorkspaceId(), req.getWorkspaceJoinType());
}
}

View File

@ -4,6 +4,8 @@ import cn.axzo.tyr.client.model.enums.IdentityType;
import cn.axzo.tyr.server.repository.entity.SaasRole;
import cn.axzo.tyr.server.repository.entity.SaasRoleWithUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@ -17,5 +19,7 @@ public interface SaasRoleMapper extends BaseMapper<SaasRole> {
List<SaasRoleWithUser> listSuperAdminByWorkspace(List<Long> workspaceIds);
List<SaasRoleWithUser> listRoleUserByPermissionGroup(List<Long> permissionGroupIds, Set<Long> workspaceIds);
Page<SaasRole> pageQueryForOUWorkspace(IPage<SaasRole> page, Long ouId, Long workspaceId, Integer workspaceJoinType);
}

View File

@ -448,13 +448,9 @@ public class RoleServiceImpl implements RoleService {
@Override
public PageResp<RoleWithUserRes> queryRoleWithUser(RoleWithUserQueryReq req) {
//按role进行分页查询
Page<SaasRole> rolePage = saasRoleDao
.page(new Page<>(req.getPage(), req.getPageSize()),
new LambdaQueryWrapper<SaasRole>()
.eq(SaasRole::getOwnerOuId, req.getOuId())
.eq(SaasRole::getWorkspaceId, req.getWorkspaceId())
.ne(SaasRole::getRoleType, RoleTypeEnum.SUPER_ADMIN.getValue()));
page2Default(req);
//按role进行分页查询 -
Page<SaasRole> rolePage = saasRoleDao.pageQueryForOUWorkspace(req);
if (CollectionUtil.isEmpty(rolePage.getRecords())) {
return PageResp.list(req.getPage(), req.getPageSize(), 0L, Collections.emptyList());
}
@ -489,6 +485,15 @@ public class RoleServiceImpl implements RoleService {
return PageResp.list(req.getPage(), req.getPageSize(), rolePage.getTotal(), resultData);
}
private void page2Default(RoleWithUserQueryReq req) {
if (req.getPage() == null) {
req.setPage(1L);
}
if (req.getPageSize() == null) {
req.setPageSize(10L);
}
}
@Override
public List<SaasRoleVO> queryRoleByRoleTypes(QueryByIdentityIdTypeReq req, List<String> roleTypes) {
List<Long> roleIds = roleUserRelationDao.query(req.getIdentityId(), req.getIdentityType(),

View File

@ -90,4 +90,20 @@
</select>
<select id="pageQueryForOUWorkspace" resultType="cn.axzo.tyr.server.repository.entity.SaasRole">
SELECT
r.*
FROM
saas_role r
LEFT JOIN saas_role_group_relation rg ON r.id = rg.role_id
LEFT JOIN saas_role_group g ON rg.saas_role_group_id = g.id
WHERE
r.is_delete = 0
AND (
( r.workspace_id = #{workspaceId} AND r.owner_ou_id = #{ouId} AND r.role_type = 'common' )
OR
( r.owner_ou_id = - 1 AND FIND_IN_SET( #{workspaceJoinType},g.ou_type_code) > 0)
)
</select>
</mapper>

View File

@ -10,8 +10,11 @@ import cn.axzo.tyr.client.model.req.RoleWithUserQueryReq;
import cn.axzo.tyr.client.model.res.RoleWithUserRes;
import cn.axzo.tyr.server.controller.role.SaasRoleController;
import cn.axzo.tyr.server.repository.dao.SaasFeatureDao;
import cn.axzo.tyr.server.repository.dao.SaasRoleDao;
import cn.axzo.tyr.server.repository.dao.SaasRoleUserRelationDao;
import cn.axzo.tyr.server.repository.entity.SaasRole;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ -36,6 +39,9 @@ public class RoleUserTest {
@Autowired
private SaasRoleController controller;
@Autowired
private SaasRoleDao saasRoleDao;
@Test
public void testList() {
saasRoleUserRelationDao.deleteByUser(BaseWorkspaceModel.builder()
@ -59,5 +65,17 @@ public class RoleUserTest {
System.out.println(JSON.toJSONString(result));
}
@Test
public void testPageQueryForOUWorkspace() {
RoleWithUserQueryReq req = new RoleWithUserQueryReq();
req.setOuId(5195L);
req.setWorkspaceId(371L);
req.setWorkspaceJoinType(2);
req.setPage(1L);
req.setPageSize(20L);
Page<SaasRole> page = saasRoleDao.pageQueryForOUWorkspace(req);
System.out.println(JSON.toJSONString(page));
}
}