fix: 增加用户流域电站权限逻辑
This commit is contained in:
parent
3ea58d0266
commit
05a3d23f58
@ -0,0 +1,31 @@
|
||||
package com.yfd.platform.qgc_base.domain.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 当前登录用户的数据权限范围
|
||||
*/
|
||||
@Data
|
||||
public class CurrentUserDataScopeVo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 是否为统一管理的超级管理员
|
||||
*/
|
||||
private boolean managedAdmin;
|
||||
|
||||
/**
|
||||
* 当前用户有权限的电站编码
|
||||
*/
|
||||
private Set<String> stationCodes = new LinkedHashSet<>();
|
||||
|
||||
/**
|
||||
* 当前用户有权限的流域编码
|
||||
*/
|
||||
private Set<String> rvcdCodes = new LinkedHashSet<>();
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package com.yfd.platform.qgc_base.service;
|
||||
|
||||
import com.yfd.platform.qgc_base.domain.vo.CurrentUserDataScopeVo;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 当前登录用户数据权限公共服务
|
||||
*/
|
||||
public interface ICurrentUserDataScopeService {
|
||||
|
||||
/**
|
||||
* 获取当前用户完整的数据权限范围
|
||||
*/
|
||||
CurrentUserDataScopeVo getCurrentUserDataScope();
|
||||
|
||||
/**
|
||||
* 获取当前用户有权限的电站编码
|
||||
*/
|
||||
Set<String> getCurrentUserAuthorizedStationCodes();
|
||||
|
||||
/**
|
||||
* 获取当前用户有权限的流域编码
|
||||
*/
|
||||
Set<String> getCurrentUserAuthorizedRvcdCodes();
|
||||
}
|
||||
@ -0,0 +1,161 @@
|
||||
package com.yfd.platform.qgc_base.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.yfd.platform.qgc_base.domain.SdEngInfoBH;
|
||||
import com.yfd.platform.qgc_base.domain.vo.CurrentUserDataScopeVo;
|
||||
import com.yfd.platform.qgc_base.mapper.SdEngInfoBHMapper;
|
||||
import com.yfd.platform.qgc_base.service.ICurrentUserDataScopeService;
|
||||
import com.yfd.platform.qgc_data.domain.SysUserDataScope;
|
||||
import com.yfd.platform.qgc_data.mapper.SysUserDataScopeMapper;
|
||||
import com.yfd.platform.system.service.IAdminAuthService;
|
||||
import com.yfd.platform.utils.SecurityUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 当前登录用户数据权限公共服务实现
|
||||
*/
|
||||
@Service
|
||||
public class CurrentUserDataScopeServiceImpl implements ICurrentUserDataScopeService {
|
||||
|
||||
private static final int QUERY_BATCH_SIZE = 500;
|
||||
|
||||
@Resource
|
||||
private SysUserDataScopeMapper sysUserDataScopeMapper;
|
||||
|
||||
@Resource
|
||||
private SdEngInfoBHMapper sdEngInfoBHMapper;
|
||||
|
||||
@Resource
|
||||
private IAdminAuthService adminAuthService;
|
||||
|
||||
@Override
|
||||
public CurrentUserDataScopeVo getCurrentUserDataScope() {
|
||||
CurrentUserDataScopeVo scopeVo = new CurrentUserDataScopeVo();
|
||||
if (adminAuthService.isCurrentManagedAdmin()) {
|
||||
scopeVo.setManagedAdmin(true);
|
||||
return scopeVo;
|
||||
}
|
||||
|
||||
String userId = SecurityUtils.getUserId();
|
||||
if (!StringUtils.hasText(userId)) {
|
||||
return scopeVo;
|
||||
}
|
||||
|
||||
List<SysUserDataScope> validPermissions = sysUserDataScopeMapper.selectValidPermissions(userId);
|
||||
if (validPermissions == null || validPermissions.isEmpty()) {
|
||||
return scopeVo;
|
||||
}
|
||||
|
||||
Set<String> directStationCodes = collectOrgIdsByType(validPermissions, "STATION");
|
||||
Set<String> directRvcdCodes = collectOrgIdsByType(validPermissions, "RVCD");
|
||||
Set<String> stationCodes = new LinkedHashSet<>();
|
||||
Set<String> rvcdCodes = new LinkedHashSet<>();
|
||||
|
||||
if (!directStationCodes.isEmpty()) {
|
||||
List<SdEngInfoBH> stationList = listStationsByStcds(directStationCodes);
|
||||
stationCodes.addAll(extractStationCodes(stationList));
|
||||
rvcdCodes.addAll(extractLegacyBasinCodes(stationList));
|
||||
}
|
||||
|
||||
if (!directRvcdCodes.isEmpty()) {
|
||||
List<SdEngInfoBH> stationList = listStationsByReachcds(directRvcdCodes);
|
||||
stationCodes.addAll(extractStationCodes(stationList));
|
||||
rvcdCodes.addAll(extractLegacyBasinCodes(stationList));
|
||||
}
|
||||
|
||||
scopeVo.setStationCodes(stationCodes);
|
||||
scopeVo.setRvcdCodes(rvcdCodes);
|
||||
return scopeVo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getCurrentUserAuthorizedStationCodes() {
|
||||
return getCurrentUserDataScope().getStationCodes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getCurrentUserAuthorizedRvcdCodes() {
|
||||
return getCurrentUserDataScope().getRvcdCodes();
|
||||
}
|
||||
|
||||
private Set<String> collectOrgIdsByType(List<SysUserDataScope> scopes, String orgType) {
|
||||
Set<String> orgIds = new LinkedHashSet<>();
|
||||
for (SysUserDataScope scope : scopes) {
|
||||
if (scope == null || !orgType.equalsIgnoreCase(scope.getOrgType())) {
|
||||
continue;
|
||||
}
|
||||
if (StringUtils.hasText(scope.getOrgId())) {
|
||||
orgIds.add(scope.getOrgId());
|
||||
}
|
||||
}
|
||||
return orgIds;
|
||||
}
|
||||
|
||||
private List<SdEngInfoBH> listStationsByStcds(Set<String> stationCodes) {
|
||||
return listStationsByColumn(stationCodes, QueryDimension.STATION);
|
||||
}
|
||||
|
||||
private List<SdEngInfoBH> listStationsByReachcds(Set<String> rvcdCodes) {
|
||||
return listStationsByColumn(rvcdCodes, QueryDimension.REACH);
|
||||
}
|
||||
|
||||
private List<SdEngInfoBH> listStationsByColumn(Set<String> codes, QueryDimension queryDimension) {
|
||||
List<SdEngInfoBH> result = new ArrayList<>();
|
||||
if (codes == null || codes.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
|
||||
List<String> codeList = new ArrayList<>(codes);
|
||||
for (int i = 0; i < codeList.size(); i += QUERY_BATCH_SIZE) {
|
||||
int end = Math.min(i + QUERY_BATCH_SIZE, codeList.size());
|
||||
List<String> batchCodes = codeList.subList(i, end);
|
||||
LambdaQueryWrapper<SdEngInfoBH> wrapper = new LambdaQueryWrapper<SdEngInfoBH>()
|
||||
.select(SdEngInfoBH::getStcd, SdEngInfoBH::getReachcd, SdEngInfoBH::getEnnm);
|
||||
if (queryDimension == QueryDimension.STATION) {
|
||||
wrapper.in(SdEngInfoBH::getStcd, batchCodes);
|
||||
} else {
|
||||
wrapper.in(SdEngInfoBH::getReachcd, batchCodes);
|
||||
}
|
||||
result.addAll(sdEngInfoBHMapper.selectList(wrapper));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Set<String> extractStationCodes(List<SdEngInfoBH> stationList) {
|
||||
Set<String> stationCodes = new LinkedHashSet<>();
|
||||
if (stationList == null || stationList.isEmpty()) {
|
||||
return stationCodes;
|
||||
}
|
||||
for (SdEngInfoBH station : stationList) {
|
||||
if (station != null && StringUtils.hasText(station.getStcd())) {
|
||||
stationCodes.add(station.getStcd());
|
||||
}
|
||||
}
|
||||
return stationCodes;
|
||||
}
|
||||
|
||||
private Set<String> extractLegacyBasinCodes(List<SdEngInfoBH> stationList) {
|
||||
Set<String> rvcdCodes = new LinkedHashSet<>();
|
||||
if (stationList == null || stationList.isEmpty()) {
|
||||
return rvcdCodes;
|
||||
}
|
||||
for (SdEngInfoBH station : stationList) {
|
||||
if (station != null && StringUtils.hasText(station.getEnnm()) && StringUtils.hasText(station.getReachcd())) {
|
||||
rvcdCodes.add(station.getReachcd());
|
||||
}
|
||||
}
|
||||
return rvcdCodes;
|
||||
}
|
||||
|
||||
private enum QueryDimension {
|
||||
STATION,
|
||||
REACH
|
||||
}
|
||||
}
|
||||
@ -128,8 +128,26 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEng
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "engInfoCache#3600", keyGenerator = "cacheKeyGenerator")
|
||||
public List<SdEngInfoBH> selectForDropdown(SdEngInfoBHRequest sdEngInfoBHRequest) {
|
||||
List<SdEngInfoBH> allData = this.selectForDropdownCached(sdEngInfoBHRequest);
|
||||
|
||||
if (adminAuthService.isCurrentManagedAdmin()) {
|
||||
return allData;
|
||||
}
|
||||
Set<String> authorizedStations = getUserAuthorizedStationCodes();
|
||||
if (authorizedStations != null && !authorizedStations.isEmpty()) {
|
||||
return allData.stream()
|
||||
.filter(item -> authorizedStations.contains(item.getStcd()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 纯数据库查询(可缓存),不包含权限过滤逻辑
|
||||
*/
|
||||
@Cacheable(cacheNames = "engInfoCache#3600", keyGenerator = "cacheKeyGenerator")
|
||||
public List<SdEngInfoBH> selectForDropdownCached(SdEngInfoBHRequest sdEngInfoBHRequest) {
|
||||
String baseId = sdEngInfoBHRequest.getBaseId();
|
||||
String hbrvcd = sdEngInfoBHRequest.getHbrvcd();
|
||||
String ennm = sdEngInfoBHRequest.getEnnm();
|
||||
@ -140,27 +158,14 @@ public class SdEngInfoBHServiceImpl extends ServiceImpl<SdEngInfoBHMapper, SdEng
|
||||
LambdaQueryWrapper<SdEngInfoBH> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(StringUtils.hasText(baseId), SdEngInfoBH::getBaseId, baseId)
|
||||
.eq(StringUtils.hasText(hbrvcd), SdEngInfoBH::getHbrvcd, hbrvcd)
|
||||
.eq(StrUtil.isNotBlank(reachcd), SdEngInfoBH::getRvcd, reachcd)
|
||||
.eq(StrUtil.isNotBlank(rvcd), SdEngInfoBH::getReachcd, rvcd)
|
||||
.eq(StrUtil.isNotBlank(rvcd), SdEngInfoBH::getReachcd, rvcd)
|
||||
.eq(StrUtil.isNotBlank(reachcd), SdEngInfoBH::getReachcd, reachcd)
|
||||
.eq(StrUtil.isNotBlank(rvcd), SdEngInfoBH::getRvcd, rvcd)
|
||||
.in(rvcds != null && !rvcds.isEmpty(), SdEngInfoBH::getReachcd, rvcds)
|
||||
.in(hbrvcds != null && !hbrvcds.isEmpty(), SdEngInfoBH::getHbrvcd, hbrvcds)
|
||||
.like(StringUtils.hasText(ennm), SdEngInfoBH::getEnnm, ennm)
|
||||
.select(SdEngInfoBH::getStcd, SdEngInfoBH::getEnnm, SdEngInfoBH::getReachcdName, SdEngInfoBH::getYrgeb, SdEngInfoBH::getBaseId)
|
||||
.orderByAsc(SdEngInfoBH::getBaseId,SdEngInfoBH::getOrderIndex);
|
||||
|
||||
if(adminAuthService.isCurrentManagedAdmin()){
|
||||
return this.list(wrapper);
|
||||
}
|
||||
Set<String> authorizedStations = getUserAuthorizedStationCodes();
|
||||
if (authorizedStations != null && !authorizedStations.isEmpty()) {
|
||||
List<SdEngInfoBH> list = this.list(wrapper);
|
||||
return list.stream()
|
||||
.filter(item -> authorizedStations.contains(item.getStcd()))
|
||||
.collect(Collectors.toList());
|
||||
}else{
|
||||
return new ArrayList<>();
|
||||
}
|
||||
.orderByAsc(SdEngInfoBH::getBaseId, SdEngInfoBH::getOrderIndex);
|
||||
return this.list(wrapper);
|
||||
}
|
||||
@Override
|
||||
public Set<String> getUserAuthorizedStationCodes() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user