From 02dea27959216c2e839159662a0140ae65b2eef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Thu, 21 Nov 2024 23:10:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor(data):=20=E6=94=AF=E6=8C=81=E5=A4=9A?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E6=95=B0=E6=8D=AE=E6=9D=83=E9=99=90=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datascope/PigxDefaultDataScopeHandle.java | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/PigxDefaultDataScopeHandle.java diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/PigxDefaultDataScopeHandle.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/PigxDefaultDataScopeHandle.java new file mode 100644 index 000000000..9d7a45ff9 --- /dev/null +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/PigxDefaultDataScopeHandle.java @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.data.datascope; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import com.pig4cloud.pigx.admin.api.entity.SysDept; +import com.pig4cloud.pigx.admin.api.entity.SysRole; +import com.pig4cloud.pigx.admin.api.feign.RemoteDataScopeService; +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import com.pig4cloud.pigx.common.core.constant.enums.UserTypeEnum; +import com.pig4cloud.pigx.common.core.util.RetOps; +import com.pig4cloud.pigx.common.security.service.PigxUser; +import com.pig4cloud.pigx.common.security.util.SecurityUtils; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.GrantedAuthority; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * @author lengleng + * @date 2019-09-07 + *

+ * 默认data scope 判断处理器 + */ + +/** + * @author lengleng + * @date 2019-09-07 + *

+ * 默认data scope 判断处理器 + */ +@RequiredArgsConstructor +public class PigxDefaultDataScopeHandle implements DataScopeHandle { + + private final RemoteDataScopeService dataScopeService; + + @Override + public Boolean calcScope(DataScope dataScope) { + // 对于TOC客户端,不进行数据权限校验 + PigxUser user = SecurityUtils.getUser(); + if (UserTypeEnum.TOC.getStatus().equals(user.getUserType())) { + return true; + } + + // 获取用户角色ID列表 + List roleIdList = user.getAuthorities().stream() + .map(GrantedAuthority::getAuthority) + .filter(authority -> authority.startsWith(SecurityConstants.ROLE)) + .map(authority -> authority.split(StrUtil.UNDERLINE)[1]) + .collect(Collectors.toList()); + if (CollUtil.isEmpty(roleIdList)) { + return false; + } + + // 获取角色列表 + List roleList = RetOps.of(dataScopeService.getRoleList(roleIdList)) + .getData() + .orElseGet(Collections::emptyList); + if (CollUtil.isEmpty(roleList)) { + return false; + } + + // 处理数据权限 + return processDataScope(user, dataScope, roleList); + } + + + /** + * 处理数据权限 + */ + private boolean processDataScope(PigxUser user, DataScope dataScope, List roleList) { + List deptList = dataScope.getDeptList(); + + for (SysRole role : roleList) { + Integer dsType = role.getDsType(); + + // 处理不同数据权限类型 + switch (Objects.requireNonNull(DataScopeTypeEnum.getByType(dsType))) { + case ALL: + return true; + case CUSTOM: + handleCustomScope(role, deptList); + break; + case OWN_CHILD_LEVEL: + handleOwnChildLevelScope(user, deptList); + break; + case OWN_LEVEL: + handleOwnLevelScope(user, deptList); + break; + case SELF_LEVEL: + handleSelfLevelScope(user, dataScope); + break; + default: + break; + } + } + + return false; + } + + /** + * 处理自定义数据权限 + */ + private void handleCustomScope(SysRole role, List deptList) { + if (StrUtil.isNotBlank(role.getDsScope())) { + deptList.addAll(Arrays.stream(role.getDsScope().split(StrUtil.COMMA)) + .map(Long::parseLong) + .collect(Collectors.toList())); + } + } + + /** + * 处理本级及下级数据权限 + */ + private void handleOwnChildLevelScope(PigxUser user, List deptList) { + List descendantDeptIds = RetOps.of(dataScopeService.getDescendantList(user.getDeptId())) + .getData() + .orElseGet(Collections::emptyList) + .stream() + .map(SysDept::getDeptId) + .collect(Collectors.toList()); + deptList.addAll(descendantDeptIds); + } + + /** + * 处理本级数据权限 + */ + private void handleOwnLevelScope(PigxUser user, List deptList) { + deptList.add(user.getDeptId()); + } + + /** + * 处理个人数据权限 + */ + private void handleSelfLevelScope(PigxUser user, DataScope dataScope) { + dataScope.setUsername(user.getUsername()); + } +}