feat:(REQ-2699) 修改角色权限时,增加roleId,方便快速准确更新角色权限

This commit is contained in:
lilong 2024-08-27 11:17:04 +08:00
parent e3a970dc32
commit a2dede09af
6 changed files with 49 additions and 19 deletions

View File

@ -52,9 +52,6 @@ public class FeatureResourceTreeSaveReq extends BaseFeatureResourceDO {
/** 页面组件对象 **/
private List<FeatureComponentSaveReq> componentSaveReqList;
/** 页面及组件权限对象 **/
private List<RolePermissionSaveReq> permissions;
@NotNull(message = "操作人ID不能为空")
private Long operatorId;

View File

@ -26,6 +26,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -80,7 +81,9 @@ public class CacheRolePermissionHandler implements InitializingBean {
// }
RoleService.ListSaasRoleParam listSaasRoleParam = RoleService.ListSaasRoleParam.builder()
// .roleIds(Lists.newArrayList(payload.getRoleIds()))
.roleIds(Optional.ofNullable(payload.getRoleIds())
.map(Lists::newArrayList)
.orElse(null))
.needPermissionRelation(true)
.build();
List<SaasRoleRes> roles = roleService.list(listSaasRoleParam);

View File

@ -24,6 +24,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -68,12 +69,16 @@ public class CacheRoleSaasFeatureResourceHandler implements InitializingBean {
log.info("begin cached role saasFeatureResource handler rocketmq event: {}", event);
RolePermissionCreatedPayload payload = event.normalizedData(RolePermissionCreatedPayload.class);
if (CollectionUtils.isEmpty(payload.getRoleIds())) {
return;
}
// 影响角色权限入口的代码没法简单重构导致发送的roleIds可能不准确所以一旦有角色权限的更新事件后全量更新角色权限角色权限数量不多
// 后续收口了代码就准确根据角色去更新缓存
// if (CollectionUtils.isEmpty(payload.getRoleIds())) {
// return;
// }
RoleService.ListSaasRoleParam listSaasRoleParam = RoleService.ListSaasRoleParam.builder()
.roleIds(Lists.newArrayList(payload.getRoleIds()))
.roleIds(Optional.ofNullable(payload.getRoleIds())
.map(Lists::newArrayList)
.orElse(null))
.needPermissionRelation(true)
.type(NEW_FEATURE)
.build();

View File

@ -353,13 +353,11 @@ public class FeatureResourceSyncServiceImpl implements FeatureResourceSyncServic
logResourceBindRoleDO.setRoleCodes(saasRoles.stream().filter(e -> existRoleIds.contains(e.getId())).map(SaasRole::getRoleCode).collect(Collectors.toList()));
}
// 同步这里不要求效率没有角色id全部刷新也不容易找这个
Event event = Event.builder()
.targetType(ROLE_PERMISSION_TARGET_TYPE)
.eventCode(ROLE_PERMISSION_CREATED.getEventCode())
.data(RolePermissionCreatedPayload.builder()
.roleIds(saasRoles.stream()
.map(SaasRole::getId)
.collect(Collectors.toSet()))
.build())
.build();
mqProducer.send(event);

View File

@ -1338,9 +1338,43 @@ public class RoleServiceImpl extends ServiceImpl<SaasRoleMapper, SaasRole>
return saasRoleGroupRes;
}
private Set<Long> resolveUpsertRoleIds(List<FeatureRoleRelationReq.RelationRoleSettings> req) {
if (CollectionUtils.isEmpty(req)) {
return Collections.emptySet();
}
Set<Long> newRoleIds = req.stream()
.map(FeatureRoleRelationReq.RelationRoleSettings::getRoleIds)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
List<Long> featureIds = req.stream()
.map(FeatureRoleRelationReq.RelationRoleSettings::getFeatureId)
.collect(Collectors.toList());
PagePgroupPermissionRelationReq pagePgroupPermissionRelationReq = PagePgroupPermissionRelationReq.builder()
.featureIds(featureIds)
.build();
List<Long> groupIds = saasPgroupPermissionRelationService.list(pagePgroupPermissionRelationReq).stream()
.map(SaasPgroupPermissionRelation::getGroupId)
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(groupIds)) {
return newRoleIds;
}
Set<Long> oldRoleIds = saasPgroupRoleRelationDao.listByGroupIds(groupIds).stream()
.map(SaasPgroupRoleRelation::getRoleId)
.collect(Collectors.toSet());
newRoleIds.addAll(oldRoleIds);
return newRoleIds;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void saveOrUpdateFeatureRoleRelation(List<FeatureRoleRelationReq.RelationRoleSettings> req, Long operatorId) {
// 查询权限点的历史角色发送mq
Set<Long> allRoleIds = resolveUpsertRoleIds(req);
for (FeatureRoleRelationReq.RelationRoleSettings item : req) {
saasFeatureResourceService.updateFeatureAuthType(item.getFeatureId(), item.getAuthType());
if (CollectionUtil.isEmpty(item.getRoleIds()) || item.getAuthType() == 0) {
@ -1417,6 +1451,7 @@ public class RoleServiceImpl extends ServiceImpl<SaasRoleMapper, SaasRole>
.targetType("saasFeatureResourceId")
.eventCode(ROLE_PERMISSION_CREATED.getEventCode())
.data(RolePermissionCreatedPayload.builder()
.roleIds(allRoleIds)
.build())
.build());
}

View File

@ -314,14 +314,6 @@ public class SaasFeatureResourceServiceImpl extends ServiceImpl<SaasFeatureResou
.build())
.build();
mqProducer.send(event);
// 待收口后这个事件需要放在角色权限的upsert方法里
mqProducer.send(Event.builder()
.targetType("saasFeatureResourceId")
.eventCode(ROLE_PERMISSION_CREATED.getEventCode())
.data(RolePermissionCreatedPayload.builder()
.build())
.build());
return baseResource.getId();
}