feat:(REQ-2720) 修改角色、产品的权限包括父节点
This commit is contained in:
parent
2cee917948
commit
9e85790cc7
@ -3,13 +3,18 @@ package cn.axzo.tyr.client.model.res;
|
||||
import cn.axzo.tyr.client.model.base.FeatureResourceExtraDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ -143,4 +148,10 @@ public class SaasFeatureResourceResp {
|
||||
* 唯一编码,用于pre环境菜单同步
|
||||
*/
|
||||
private String uniCode;
|
||||
|
||||
public List<Long> resolvePath() {
|
||||
return Optional.ofNullable(this.getPath())
|
||||
.map(e -> Arrays.stream(e.split(",")).map(Long::valueOf).collect(Collectors.toList()))
|
||||
.orElseGet(Lists::newArrayList);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,25 +5,22 @@ import cn.axzo.framework.rocketmq.EventConsumer;
|
||||
import cn.axzo.framework.rocketmq.EventHandler;
|
||||
import cn.axzo.tyr.client.model.req.PageProductFeatureRelationReq;
|
||||
import cn.axzo.tyr.client.model.req.PageSaasFeatureResourceReq;
|
||||
import cn.axzo.tyr.client.model.req.QueryPermissionByIdsReq;
|
||||
import cn.axzo.tyr.client.model.res.SaasFeatureResourceResp;
|
||||
import cn.axzo.tyr.client.model.res.SimplePermissionPointResp;
|
||||
import cn.axzo.tyr.server.event.payload.ProductPermissionCreatedPayload;
|
||||
import cn.axzo.tyr.server.repository.dao.SaasFeatureDao;
|
||||
import cn.axzo.tyr.server.repository.entity.SaasFeature;
|
||||
import cn.axzo.tyr.server.repository.entity.SaasProductModuleFeatureRelation;
|
||||
import cn.axzo.tyr.server.service.PermissionPointService;
|
||||
import cn.axzo.tyr.server.service.ProductFeatureRelationService;
|
||||
import cn.axzo.tyr.server.service.ProductPermissionCacheService;
|
||||
import cn.axzo.tyr.server.service.SaasFeatureResourceService;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -53,7 +50,7 @@ public class CacheProductPermissionHandler implements EventHandler, Initializing
|
||||
@Autowired
|
||||
private SaasFeatureResourceService saasFeatureResourceService;
|
||||
@Autowired
|
||||
private PermissionPointService permissionPointService;
|
||||
private SaasFeatureDao saasFeatureDao;
|
||||
|
||||
@Override
|
||||
public void onEvent(Event event, EventConsumer.Context context) {
|
||||
@ -100,8 +97,12 @@ public class CacheProductPermissionHandler implements EventHandler, Initializing
|
||||
// 新的菜单树是是把有权限点的父节点也存进去了,所以直接解析
|
||||
Map<Long, SaasFeatureResourceResp> featureResources = listSaasFeatureResource(productPermissions);
|
||||
|
||||
Map<Long, SaasFeatureResourceResp> parentFeatureResources = listParentSaasFeatureResource(featureResources);
|
||||
|
||||
// 旧的菜单树只存储了权限点信息,没有把父节点存进去,需要解析父节点进行存储
|
||||
Map<Long, SimplePermissionPointResp> saasFeatures = listSaasFeature(productPermissions);
|
||||
Map<Long, SaasFeature> saasFeatures = listSaasFeature(productPermissions);
|
||||
|
||||
Map<Long, SaasFeature> parentSaasFeatures = listParentSaasFeature(saasFeatures);
|
||||
|
||||
return productPermissions.stream()
|
||||
.collect(Collectors.groupingBy(SaasProductModuleFeatureRelation::getProductModuleId))
|
||||
@ -117,44 +118,14 @@ public class CacheProductPermissionHandler implements EventHandler, Initializing
|
||||
List<ProductPermissionCacheService.PermissionDTO> permissions = productFeatureRelations.stream()
|
||||
.map(relation -> {
|
||||
if (Objects.equals(relation.getType(), NEW_FEATURE)) {
|
||||
SaasFeatureResourceResp featureResource = featureResources.get(relation.getFeatureId());
|
||||
// 菜单节点是不会关联元素code,所以缓存的featureCode使用菜单编码
|
||||
if (Objects.isNull(featureResource)) {
|
||||
return null;
|
||||
}
|
||||
Set<String> featureCodes = Optional.ofNullable(featureResource.getFeatureCodes())
|
||||
.orElseGet(() -> Sets.newHashSet(featureResource.getUniCode()));
|
||||
|
||||
if (CollectionUtils.isEmpty(featureCodes)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return featureCodes.stream()
|
||||
.map(featureCode -> ProductPermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureCode(featureCode)
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.cooperateType(relation.getDictCode())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
return resolveFeatureResourcePermission(relation, featureResources, parentFeatureResources);
|
||||
}
|
||||
|
||||
SimplePermissionPointResp saasFeature = saasFeatures.get(relation.getFeatureId());
|
||||
if (Objects.isNull(saasFeature)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Lists.newArrayList(ProductPermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(saasFeature.getId())
|
||||
.featureCode(saasFeature.getCode())
|
||||
.featureType(saasFeature.getFeatureType())
|
||||
.terminal(saasFeature.getTerminal())
|
||||
.cooperateType(relation.getDictCode())
|
||||
.build());
|
||||
return resolveFeaturePermission(relation, saasFeatures, parentSaasFeatures);
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(permissions)) {
|
||||
@ -171,6 +142,88 @@ public class CacheProductPermissionHandler implements EventHandler, Initializing
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<ProductPermissionCacheService.PermissionDTO> resolveFeaturePermission(SaasProductModuleFeatureRelation relation, Map<Long, SaasFeature> saasFeatures, Map<Long, SaasFeature> parentSaasFeatures) {
|
||||
SaasFeature saasFeature = saasFeatures.get(relation.getFeatureId());
|
||||
if (Objects.isNull(saasFeature)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<ProductPermissionCacheService.PermissionDTO> permissionDTOS = Lists.newArrayList(ProductPermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(saasFeature.getId())
|
||||
.featureCode(saasFeature.getFeatureCode())
|
||||
.featureType(saasFeature.getFeatureType())
|
||||
.terminal(saasFeature.getTerminal())
|
||||
.cooperateType(relation.getDictCode())
|
||||
.build());
|
||||
|
||||
List<ProductPermissionCacheService.PermissionDTO> parentPermissions = saasFeature.splitPath().stream()
|
||||
.map(parentSaasFeatures::get)
|
||||
.filter(Objects::nonNull)
|
||||
.map(f -> ProductPermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(f.getId())
|
||||
.featureCode(f.getFeatureCode())
|
||||
.featureType(f.getFeatureType())
|
||||
.terminal(f.getTerminal())
|
||||
.cooperateType(relation.getDictCode())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
permissionDTOS.addAll(parentPermissions);
|
||||
return permissionDTOS;
|
||||
}
|
||||
|
||||
private List<ProductPermissionCacheService.PermissionDTO> resolveFeatureResourcePermission(SaasProductModuleFeatureRelation relation, Map<Long, SaasFeatureResourceResp> featureResources, Map<Long, SaasFeatureResourceResp> parentFeatureResources) {
|
||||
SaasFeatureResourceResp featureResource = featureResources.get(relation.getFeatureId());
|
||||
// 菜单节点是不会关联元素code,所以缓存的featureCode使用菜单编码
|
||||
if (Objects.isNull(featureResource)) {
|
||||
return null;
|
||||
}
|
||||
Set<String> featureCodes = Optional.ofNullable(featureResource.getFeatureCodes())
|
||||
.orElseGet(() -> Sets.newHashSet(featureResource.getUniCode()));
|
||||
|
||||
if (CollectionUtils.isEmpty(featureCodes)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<ProductPermissionCacheService.PermissionDTO> permissionDTOS = featureCodes.stream()
|
||||
.map(featureCode -> ProductPermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureCode(featureCode)
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.cooperateType(relation.getDictCode())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<ProductPermissionCacheService.PermissionDTO> parentPermissions = featureResource.resolvePath().stream()
|
||||
.map(parentFeatureResources::get)
|
||||
.filter(Objects::nonNull)
|
||||
.map(f -> {
|
||||
|
||||
Set<String> parentFeatureCodes = Optional.ofNullable(f.getFeatureCodes())
|
||||
.orElseGet(() -> Sets.newHashSet(f.getUniCode()));
|
||||
if (CollectionUtils.isEmpty(parentFeatureCodes)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parentFeatureCodes.stream()
|
||||
.map(featureCode -> ProductPermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureCode(featureCode)
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.cooperateType(relation.getDictCode())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
permissionDTOS.addAll(parentPermissions);
|
||||
return permissionDTOS;
|
||||
}
|
||||
|
||||
|
||||
private Map<Long, SaasFeatureResourceResp> listSaasFeatureResource(List<SaasProductModuleFeatureRelation> productPermissions) {
|
||||
|
||||
@ -184,36 +237,36 @@ public class CacheProductPermissionHandler implements EventHandler, Initializing
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
// 存在pre环境更改了节点的父节点,可能导致产品没有父节点的权限,这里补齐父节点的权限
|
||||
PageSaasFeatureResourceReq pageSaasFeatureResourceReq = PageSaasFeatureResourceReq.builder()
|
||||
.ids(featureIds)
|
||||
.needFeatureCodes(true)
|
||||
.build();
|
||||
List<SaasFeatureResourceResp> features = saasFeatureResourceService.list(pageSaasFeatureResourceReq);
|
||||
|
||||
if (CollectionUtils.isEmpty(features)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
List<Long> parentIds = features.stream()
|
||||
.map(SaasFeatureResourceResp::getPath)
|
||||
.flatMap(path -> Arrays.stream(path.split(",")))
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.map(Long::valueOf)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<SaasFeatureResourceResp> parentFeatures = saasFeatureResourceService.list(PageSaasFeatureResourceReq.builder()
|
||||
.ids(parentIds)
|
||||
.needFeatureCodes(true)
|
||||
.build());
|
||||
features.addAll(parentFeatures);
|
||||
|
||||
return features.stream()
|
||||
return saasFeatureResourceService.list(pageSaasFeatureResourceReq).stream()
|
||||
.collect(Collectors.toMap(SaasFeatureResourceResp::getId, Function.identity(), (f, s) -> s));
|
||||
}
|
||||
|
||||
private Map<Long, SimplePermissionPointResp> listSaasFeature(List<SaasProductModuleFeatureRelation> productPermissions) {
|
||||
private Map<Long, SaasFeatureResourceResp> listParentSaasFeatureResource(Map<Long, SaasFeatureResourceResp> productPermissions) {
|
||||
|
||||
List<Long> parentIds = productPermissions.values().stream()
|
||||
.map(SaasFeatureResourceResp::resolvePath)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(parentIds)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
// 存在pre环境更改了节点的父节点,可能导致产品没有父节点的权限,这里补齐父节点的权限
|
||||
PageSaasFeatureResourceReq pageSaasFeatureResourceReq = PageSaasFeatureResourceReq.builder()
|
||||
.ids(parentIds)
|
||||
.needFeatureCodes(true)
|
||||
.build();
|
||||
return saasFeatureResourceService.list(pageSaasFeatureResourceReq).stream()
|
||||
.collect(Collectors.toMap(SaasFeatureResourceResp::getId, Function.identity(), (f, s) -> s));
|
||||
}
|
||||
|
||||
private Map<Long, SaasFeature> listSaasFeature(List<SaasProductModuleFeatureRelation> productPermissions) {
|
||||
|
||||
Set<Long> featureIds = productPermissions.stream()
|
||||
.filter(e -> Objects.equals(e.getType(), OLD_FEATURE))
|
||||
@ -224,12 +277,27 @@ public class CacheProductPermissionHandler implements EventHandler, Initializing
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
return permissionPointService.listPermissionByIds(
|
||||
QueryPermissionByIdsReq.builder()
|
||||
.ids(featureIds)
|
||||
.includeParent(true)
|
||||
.build())
|
||||
.stream()
|
||||
.collect(Collectors.toMap(SimplePermissionPointResp::getId, Function.identity(), (f, s) -> s));
|
||||
return saasFeatureDao.listByIds(featureIds).stream()
|
||||
.collect(Collectors.toMap(SaasFeature::getId, Function.identity()));
|
||||
}
|
||||
|
||||
private Map<Long, SaasFeature> listParentSaasFeature(Map<Long, SaasFeature> saasFeatures) {
|
||||
|
||||
if (CollectionUtils.isEmpty(saasFeatures)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
List<Long> parentIds = saasFeatures.values().stream()
|
||||
.map(SaasFeature::splitPath)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(parentIds)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
return saasFeatureDao.listByIds(parentIds).stream()
|
||||
.collect(Collectors.toMap(SaasFeature::getId, Function.identity()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ import cn.axzo.tyr.server.repository.entity.SaasProductModuleFeatureRelation;
|
||||
import cn.axzo.tyr.server.service.ProductFeatureRelationService;
|
||||
import cn.axzo.tyr.server.service.ProductSaasFeatureResourceCacheService;
|
||||
import cn.axzo.tyr.server.service.SaasFeatureResourceService;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -21,6 +22,7 @@ import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -95,6 +97,8 @@ public class CacheProductSaasFeatureResourceHandler implements EventHandler, Ini
|
||||
|
||||
Map<Long, SaasFeatureResourceResp> featureResources = listSaasFeatureResource(productPermissions);
|
||||
|
||||
Map<Long, SaasFeatureResourceResp> parentFeatureResources = listParentSaasFeatureResource(featureResources);
|
||||
|
||||
return productPermissions.stream()
|
||||
.collect(Collectors.groupingBy(SaasProductModuleFeatureRelation::getProductModuleId))
|
||||
.entrySet()
|
||||
@ -112,15 +116,41 @@ public class CacheProductSaasFeatureResourceHandler implements EventHandler, Ini
|
||||
if (Objects.isNull(featureResource) || StringUtils.isBlank(featureResource.getUniCode())) {
|
||||
return null;
|
||||
}
|
||||
return ProductSaasFeatureResourceCacheService.FeatureResourceDTO.builder()
|
||||
|
||||
List<ProductSaasFeatureResourceCacheService.FeatureResourceDTO> featureResourceDTOS = Lists.newArrayList(ProductSaasFeatureResourceCacheService.FeatureResourceDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.uniCode(featureResource.getUniCode())
|
||||
.cooperateType(relation.getDictCode())
|
||||
.build();
|
||||
.build());
|
||||
List<ProductSaasFeatureResourceCacheService.FeatureResourceDTO> parentPermissions = featureResource.resolvePath().stream()
|
||||
.map(parentFeatureResources::get)
|
||||
.filter(Objects::nonNull)
|
||||
.map(f -> {
|
||||
|
||||
if (StringUtils.isBlank(f.getUniCode())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ProductSaasFeatureResourceCacheService.FeatureResourceDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.uniCode(featureResource.getUniCode())
|
||||
.cooperateType(relation.getDictCode())
|
||||
.build();
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
featureResourceDTOS.addAll(parentPermissions);
|
||||
|
||||
return featureResourceDTOS;
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.filter(f -> FEATURE_RESOURCE_TYPES.contains(f.getFeatureType()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(productFeatureResources)) {
|
||||
@ -154,28 +184,28 @@ public class CacheProductSaasFeatureResourceHandler implements EventHandler, Ini
|
||||
.ids(featureIds)
|
||||
.needFeatureCodes(true)
|
||||
.build();
|
||||
List<SaasFeatureResourceResp> features = saasFeatureResourceService.list(pageSaasFeatureResourceReq);
|
||||
return saasFeatureResourceService.list(pageSaasFeatureResourceReq).stream()
|
||||
.collect(Collectors.toMap(SaasFeatureResourceResp::getId, Function.identity(), (f, s) -> s));
|
||||
}
|
||||
|
||||
if (CollectionUtils.isEmpty(features)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
private Map<Long, SaasFeatureResourceResp> listParentSaasFeatureResource(Map<Long, SaasFeatureResourceResp> productPermissions) {
|
||||
|
||||
List<Long> parentIds = features.stream()
|
||||
.map(SaasFeatureResourceResp::getPath)
|
||||
.flatMap(path -> Arrays.stream(path.split(",")))
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.map(Long::valueOf)
|
||||
List<Long> parentIds = productPermissions.values().stream()
|
||||
.map(SaasFeatureResourceResp::resolvePath)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<SaasFeatureResourceResp> parentFeatures = saasFeatureResourceService.list(PageSaasFeatureResourceReq.builder()
|
||||
if (CollectionUtils.isEmpty(parentIds)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
// 存在pre环境更改了节点的父节点,可能导致产品没有父节点的权限,这里补齐父节点的权限
|
||||
PageSaasFeatureResourceReq pageSaasFeatureResourceReq = PageSaasFeatureResourceReq.builder()
|
||||
.ids(parentIds)
|
||||
.needFeatureCodes(true)
|
||||
.build());
|
||||
features.addAll(parentFeatures);
|
||||
|
||||
return features.stream()
|
||||
.filter(e -> FEATURE_RESOURCE_TYPES.contains(e.getFeatureType()))
|
||||
.build();
|
||||
return saasFeatureResourceService.list(pageSaasFeatureResourceReq).stream()
|
||||
.collect(Collectors.toMap(SaasFeatureResourceResp::getId, Function.identity(), (f, s) -> s));
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,26 +4,23 @@ import cn.axzo.framework.rocketmq.Event;
|
||||
import cn.axzo.framework.rocketmq.EventConsumer;
|
||||
import cn.axzo.framework.rocketmq.EventHandler;
|
||||
import cn.axzo.tyr.client.model.req.PageSaasFeatureResourceReq;
|
||||
import cn.axzo.tyr.client.model.req.QueryPermissionByIdsReq;
|
||||
import cn.axzo.tyr.client.model.res.SaasFeatureResourceResp;
|
||||
import cn.axzo.tyr.client.model.res.SaasPermissionRelationRes;
|
||||
import cn.axzo.tyr.client.model.res.SaasRoleRes;
|
||||
import cn.axzo.tyr.client.model.res.SimplePermissionPointResp;
|
||||
import cn.axzo.tyr.server.event.payload.RolePermissionCreatedPayload;
|
||||
import cn.axzo.tyr.server.service.PermissionPointService;
|
||||
import cn.axzo.tyr.server.repository.dao.SaasFeatureDao;
|
||||
import cn.axzo.tyr.server.repository.entity.SaasFeature;
|
||||
import cn.axzo.tyr.server.service.RolePermissionCacheService;
|
||||
import cn.axzo.tyr.server.service.RoleService;
|
||||
import cn.axzo.tyr.server.service.SaasFeatureResourceService;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -53,7 +50,7 @@ public class CacheRolePermissionHandler implements EventHandler, InitializingBea
|
||||
@Autowired
|
||||
private SaasFeatureResourceService saasFeatureResourceService;
|
||||
@Autowired
|
||||
private PermissionPointService permissionPointService;
|
||||
private SaasFeatureDao saasFeatureDao;
|
||||
|
||||
@Override
|
||||
public void onEvent(Event event, EventConsumer.Context context) {
|
||||
@ -97,7 +94,12 @@ public class CacheRolePermissionHandler implements EventHandler, InitializingBea
|
||||
|
||||
Map<Long, SaasFeatureResourceResp> featureResources = listSaasFeatureResource(roles);
|
||||
|
||||
Map<Long, SimplePermissionPointResp> saasFeatures = listSaasFeature(roles);
|
||||
Map<Long, SaasFeatureResourceResp> parentFeatureResources = listParentSaasFeatureResource(featureResources);
|
||||
|
||||
Map<Long, SaasFeature> saasFeatures = listSaasFeature(roles);
|
||||
|
||||
Map<Long, SaasFeature> parentSaasFeatures = listParentSaasFeature(saasFeatures);
|
||||
|
||||
|
||||
return roles.stream()
|
||||
.map(e -> {
|
||||
@ -109,40 +111,13 @@ public class CacheRolePermissionHandler implements EventHandler, InitializingBea
|
||||
.distinct()
|
||||
.map(permissionRelation -> {
|
||||
if (Objects.equals(permissionRelation.getType(), NEW_FEATURE)) {
|
||||
SaasFeatureResourceResp featureResource = featureResources.get(permissionRelation.getFeatureId());
|
||||
// 菜单节点是不会关联元素code,所以缓存的featureCode使用菜单编码
|
||||
if (Objects.isNull(featureResource)) {
|
||||
return null;
|
||||
}
|
||||
Set<String> featureCodes = Optional.ofNullable(featureResource.getFeatureCodes())
|
||||
.orElseGet(() -> Sets.newHashSet(featureResource.getUniCode()));
|
||||
|
||||
if (CollectionUtils.isEmpty(featureCodes)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return featureCodes.stream()
|
||||
.map(featureCode -> RolePermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureCode(featureCode)
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
return resolveFeatureResourcePermission(permissionRelation, featureResources, parentFeatureResources);
|
||||
}
|
||||
SimplePermissionPointResp saasFeature = saasFeatures.get(permissionRelation.getFeatureId());
|
||||
if (Objects.isNull(saasFeature)) {
|
||||
return null;
|
||||
}
|
||||
return Lists.newArrayList(RolePermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(saasFeature.getId())
|
||||
.featureCode(saasFeature.getCode())
|
||||
.featureType(saasFeature.getFeatureType())
|
||||
.terminal(saasFeature.getTerminal())
|
||||
.build());
|
||||
return resolveFeaturePermission(permissionRelation, saasFeatures, parentSaasFeatures);
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(permissions)) {
|
||||
@ -159,6 +134,85 @@ public class CacheRolePermissionHandler implements EventHandler, InitializingBea
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static List<RolePermissionCacheService.PermissionDTO> resolveFeaturePermission(SaasPermissionRelationRes permissionRelation, Map<Long, SaasFeature> saasFeatures, Map<Long, SaasFeature> parentSaasFeatures) {
|
||||
SaasFeature saasFeature = saasFeatures.get(permissionRelation.getFeatureId());
|
||||
if (Objects.isNull(saasFeature)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<RolePermissionCacheService.PermissionDTO> permissionDTOS = Lists.newArrayList(RolePermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(saasFeature.getId())
|
||||
.featureCode(saasFeature.getFeatureCode())
|
||||
.featureType(saasFeature.getFeatureType())
|
||||
.terminal(saasFeature.getTerminal())
|
||||
.build());
|
||||
|
||||
List<RolePermissionCacheService.PermissionDTO> parentPermissions = saasFeature.splitPath().stream()
|
||||
.map(parentSaasFeatures::get)
|
||||
.filter(Objects::nonNull)
|
||||
.map(f -> RolePermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(saasFeature.getId())
|
||||
.featureCode(saasFeature.getFeatureCode())
|
||||
.featureType(saasFeature.getFeatureType())
|
||||
.terminal(saasFeature.getTerminal())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
permissionDTOS.addAll(parentPermissions);
|
||||
return permissionDTOS;
|
||||
}
|
||||
|
||||
private static List<RolePermissionCacheService.PermissionDTO> resolveFeatureResourcePermission(SaasPermissionRelationRes permissionRelation, Map<Long, SaasFeatureResourceResp> featureResources, Map<Long, SaasFeatureResourceResp> parentFeatureResources) {
|
||||
SaasFeatureResourceResp featureResource = featureResources.get(permissionRelation.getFeatureId());
|
||||
// 菜单节点是不会关联元素code,所以缓存的featureCode使用菜单编码
|
||||
if (Objects.isNull(featureResource)) {
|
||||
return null;
|
||||
}
|
||||
Set<String> featureCodes = Optional.ofNullable(featureResource.getFeatureCodes())
|
||||
.orElseGet(() -> Sets.newHashSet(featureResource.getUniCode()));
|
||||
|
||||
if (CollectionUtils.isEmpty(featureCodes)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<RolePermissionCacheService.PermissionDTO> permissionDTOS = featureCodes.stream()
|
||||
.map(featureCode -> RolePermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureCode(featureCode)
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<RolePermissionCacheService.PermissionDTO> parentPermissions = featureResource.resolvePath().stream()
|
||||
.map(parentFeatureResources::get)
|
||||
.filter(Objects::nonNull)
|
||||
.map(f -> {
|
||||
|
||||
Set<String> parentFeatureCodes = Optional.ofNullable(f.getFeatureCodes())
|
||||
.orElseGet(() -> Sets.newHashSet(f.getUniCode()));
|
||||
if (CollectionUtils.isEmpty(parentFeatureCodes)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parentFeatureCodes.stream()
|
||||
.map(featureCode -> RolePermissionCacheService.PermissionDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureCode(featureCode)
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
permissionDTOS.addAll(parentPermissions);
|
||||
|
||||
return permissionDTOS;
|
||||
}
|
||||
|
||||
|
||||
private Map<Long, SaasFeatureResourceResp> listSaasFeatureResource(List<SaasRoleRes> roles) {
|
||||
|
||||
@ -175,36 +229,37 @@ public class CacheRolePermissionHandler implements EventHandler, InitializingBea
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
// 存在pre环境更改了节点的父节点,可能导致产品没有父节点的权限,这里补齐父节点的权限
|
||||
|
||||
PageSaasFeatureResourceReq pageSaasFeatureResourceReq = PageSaasFeatureResourceReq.builder()
|
||||
.ids(featureIds)
|
||||
.needFeatureCodes(true)
|
||||
.build();
|
||||
List<SaasFeatureResourceResp> features = saasFeatureResourceService.list(pageSaasFeatureResourceReq);
|
||||
|
||||
if (CollectionUtils.isEmpty(features)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
List<Long> parentIds = features.stream()
|
||||
.map(SaasFeatureResourceResp::getPath)
|
||||
.flatMap(path -> Arrays.stream(path.split(",")))
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.map(Long::valueOf)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<SaasFeatureResourceResp> parentFeatures = saasFeatureResourceService.list(PageSaasFeatureResourceReq.builder()
|
||||
.ids(parentIds)
|
||||
.needFeatureCodes(true)
|
||||
.build());
|
||||
features.addAll(parentFeatures);
|
||||
|
||||
return features.stream()
|
||||
return saasFeatureResourceService.list(pageSaasFeatureResourceReq).stream()
|
||||
.collect(Collectors.toMap(SaasFeatureResourceResp::getId, Function.identity(), (f, s) -> s));
|
||||
}
|
||||
|
||||
private Map<Long, SimplePermissionPointResp> listSaasFeature(List<SaasRoleRes> roles) {
|
||||
private Map<Long, SaasFeatureResourceResp> listParentSaasFeatureResource(Map<Long, SaasFeatureResourceResp> productPermissions) {
|
||||
|
||||
List<Long> parentIds = productPermissions.values().stream()
|
||||
.map(SaasFeatureResourceResp::resolvePath)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(parentIds)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
// 存在pre环境更改了节点的父节点,可能导致产品没有父节点的权限,这里补齐父节点的权限
|
||||
PageSaasFeatureResourceReq pageSaasFeatureResourceReq = PageSaasFeatureResourceReq.builder()
|
||||
.ids(parentIds)
|
||||
.needFeatureCodes(true)
|
||||
.build();
|
||||
return saasFeatureResourceService.list(pageSaasFeatureResourceReq).stream()
|
||||
.collect(Collectors.toMap(SaasFeatureResourceResp::getId, Function.identity(), (f, s) -> s));
|
||||
}
|
||||
|
||||
private Map<Long, SaasFeature> listSaasFeature(List<SaasRoleRes> roles) {
|
||||
|
||||
Set<Long> featureIds = roles.stream()
|
||||
.filter(e -> !CollectionUtils.isEmpty(e.getPermissionRelations()))
|
||||
@ -218,12 +273,28 @@ public class CacheRolePermissionHandler implements EventHandler, InitializingBea
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
return permissionPointService.listPermissionByIds(
|
||||
QueryPermissionByIdsReq.builder()
|
||||
.ids(featureIds)
|
||||
.includeParent(true)
|
||||
.build())
|
||||
return saasFeatureDao.listByIds(featureIds)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(SimplePermissionPointResp::getId, Function.identity(), (f, s) -> s));
|
||||
.collect(Collectors.toMap(SaasFeature::getId, Function.identity(), (f, s) -> s));
|
||||
}
|
||||
|
||||
private Map<Long, SaasFeature> listParentSaasFeature(Map<Long, SaasFeature> saasFeatures) {
|
||||
|
||||
if (CollectionUtils.isEmpty(saasFeatures)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
List<Long> parentIds = saasFeatures.values().stream()
|
||||
.map(SaasFeature::splitPath)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(parentIds)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
return saasFeatureDao.listByIds(parentIds).stream()
|
||||
.collect(Collectors.toMap(SaasFeature::getId, Function.identity()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,7 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -96,6 +95,8 @@ public class CacheRoleSaasFeatureResourceHandler implements EventHandler, Initia
|
||||
|
||||
Map<Long, SaasFeatureResourceResp> featureResources = listSaasFeatureResource(roles);
|
||||
|
||||
Map<Long, SaasFeatureResourceResp> parentFeatureResources = listParentSaasFeatureResource(featureResources);
|
||||
|
||||
return roles.stream()
|
||||
.map(e -> {
|
||||
if (CollectionUtils.isEmpty(e.getPermissionRelations())) {
|
||||
@ -109,14 +110,39 @@ public class CacheRoleSaasFeatureResourceHandler implements EventHandler, Initia
|
||||
if (Objects.isNull(featureResource) || StringUtils.isBlank(featureResource.getUniCode())) {
|
||||
return null;
|
||||
}
|
||||
return RoleSaasFeatureResourceCacheService.SaasFeatureResourceDTO.builder()
|
||||
|
||||
List<RoleSaasFeatureResourceCacheService.SaasFeatureResourceDTO> featureResourceDTOS = Lists.newArrayList(RoleSaasFeatureResourceCacheService.SaasFeatureResourceDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.uniCode(featureResource.getUniCode())
|
||||
.build();
|
||||
.build());
|
||||
List<RoleSaasFeatureResourceCacheService.SaasFeatureResourceDTO> parentPermissions = featureResource.resolvePath().stream()
|
||||
.map(parentFeatureResources::get)
|
||||
.filter(Objects::nonNull)
|
||||
.map(f -> {
|
||||
|
||||
if (StringUtils.isBlank(f.getUniCode())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return RoleSaasFeatureResourceCacheService.SaasFeatureResourceDTO.builder()
|
||||
.featureId(featureResource.getId())
|
||||
.featureType(featureResource.getFeatureType())
|
||||
.terminal(featureResource.getTerminal())
|
||||
.uniCode(featureResource.getUniCode())
|
||||
.build();
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
featureResourceDTOS.addAll(parentPermissions);
|
||||
|
||||
return featureResourceDTOS;
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.filter(f -> FEATURE_RESOURCE_TYPES.contains(f.getFeatureType()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(permissions)) {
|
||||
@ -152,28 +178,28 @@ public class CacheRoleSaasFeatureResourceHandler implements EventHandler, Initia
|
||||
.ids(featureIds)
|
||||
.needFeatureCodes(true)
|
||||
.build();
|
||||
List<SaasFeatureResourceResp> features = saasFeatureResourceService.list(pageSaasFeatureResourceReq);
|
||||
return saasFeatureResourceService.list(pageSaasFeatureResourceReq).stream()
|
||||
.collect(Collectors.toMap(SaasFeatureResourceResp::getId, Function.identity(), (f, s) -> s));
|
||||
}
|
||||
|
||||
if (CollectionUtils.isEmpty(features)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
private Map<Long, SaasFeatureResourceResp> listParentSaasFeatureResource(Map<Long, SaasFeatureResourceResp> productPermissions) {
|
||||
|
||||
List<Long> parentIds = features.stream()
|
||||
.map(SaasFeatureResourceResp::getPath)
|
||||
.flatMap(path -> Arrays.stream(path.split(",")))
|
||||
.filter(StringUtils::isNotBlank)
|
||||
.map(Long::valueOf)
|
||||
List<Long> parentIds = productPermissions.values().stream()
|
||||
.map(SaasFeatureResourceResp::resolvePath)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<SaasFeatureResourceResp> parentFeatures = saasFeatureResourceService.list(PageSaasFeatureResourceReq.builder()
|
||||
if (CollectionUtils.isEmpty(parentIds)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
// 存在pre环境更改了节点的父节点,可能导致产品没有父节点的权限,这里补齐父节点的权限
|
||||
PageSaasFeatureResourceReq pageSaasFeatureResourceReq = PageSaasFeatureResourceReq.builder()
|
||||
.ids(parentIds)
|
||||
.needFeatureCodes(true)
|
||||
.build());
|
||||
features.addAll(parentFeatures);
|
||||
|
||||
return features.stream()
|
||||
.filter(e -> FEATURE_RESOURCE_TYPES.contains(e.getFeatureType()))
|
||||
.build();
|
||||
return saasFeatureResourceService.list(pageSaasFeatureResourceReq).stream()
|
||||
.collect(Collectors.toMap(SaasFeatureResourceResp::getId, Function.identity(), (f, s) -> s));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user