|
|
@ -1,28 +1,26 @@ |
|
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
import com.epmet.commons.tools.constant.OpeScopeConstant; |
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.utils.CpUserDetailRedis; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.dto.CustomerAgencyDTO; |
|
|
|
import com.epmet.dto.GovStaffRoleDTO; |
|
|
|
import com.epmet.dto.form.StaffPermCacheFormDTO; |
|
|
|
import com.epmet.dto.OperationScopeDTO; |
|
|
|
import com.epmet.dto.form.StaffRoleFormDTO; |
|
|
|
import com.epmet.dto.result.RoleOperationResultDTO; |
|
|
|
import com.epmet.dto.result.RoleOpeScopeResultDTO; |
|
|
|
import com.epmet.feign.EpmetUserFeignClient; |
|
|
|
import com.epmet.feign.GovAccessFeignClient; |
|
|
|
import com.epmet.feign.GovOrgFeignClient; |
|
|
|
import com.epmet.service.AccessService; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
@Service |
|
|
|
public class AccessServiceImpl implements AccessService { |
|
|
@ -38,6 +36,10 @@ public class AccessServiceImpl implements AccessService { |
|
|
|
@Autowired |
|
|
|
private GovOrgFeignClient govOrgFeignClient; |
|
|
|
|
|
|
|
public static final String ORG_RELATION_SAME = "same"; |
|
|
|
public static final String ORG_RELATION_SUB = "sub"; |
|
|
|
public static final String ORG_RELATION_SUP = "sup"; |
|
|
|
|
|
|
|
public List<GovStaffRoleDTO> queryGovStaffRoles(String staffId, String orgId) { |
|
|
|
List<GovStaffRoleDTO> roleDTOS = new ArrayList<>(); |
|
|
|
StaffRoleFormDTO formDTO = new StaffRoleFormDTO(); |
|
|
@ -51,55 +53,222 @@ public class AccessServiceImpl implements AccessService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Set<String> listOpeKeysByStaffId(String app, String client, String staffId, String agencyId, String gridId) { |
|
|
|
List<GovStaffRoleDTO> roleDTOS = new ArrayList<>(); |
|
|
|
public Set<String> listOpeKeysByStaffId(String app, String client, String staffId, String currAgencyId, String currGridId) { |
|
|
|
|
|
|
|
// 1.拿到所属组织机构信息
|
|
|
|
Result<CustomerAgencyDTO> agencyByStaffRst = govOrgFeignClient.getAgencyByStaff(staffId); |
|
|
|
if (!agencyByStaffRst.success()) { |
|
|
|
logger.error("根据StaffId查询所属单位出错,StaffId:{}, 错误信息:{}", staffId, agencyByStaffRst.getMsg()); |
|
|
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); |
|
|
|
} |
|
|
|
CustomerAgencyDTO belongAgency = agencyByStaffRst.getData(); |
|
|
|
if (belongAgency == null) { |
|
|
|
logger.error("根据StaffId查询所属单位结果为空,StaffId:{}", staffId); |
|
|
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); |
|
|
|
} |
|
|
|
|
|
|
|
// 2.拿到当前所处机关单位信息
|
|
|
|
Result<CustomerAgencyDTO> currAgencyRst = govOrgFeignClient.getAgencyById(currAgencyId); |
|
|
|
CustomerAgencyDTO currAgencyDto = currAgencyRst.getData(); |
|
|
|
if (!currAgencyRst.success() || currAgencyDto == null) { |
|
|
|
logger.error("根据当前机构id[{}]查询pids失败:{}", currAgencyId, currAgencyRst.getMsg()); |
|
|
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); |
|
|
|
} |
|
|
|
|
|
|
|
// 拿到该用户所属的组织机构的orgIdPath,拿到当前组织机构的orgIdPath,看当前组织机构的orgIdPath是否在用户所属的组织机构的orgIdPath下
|
|
|
|
|
|
|
|
// 查询机关单位中的角色
|
|
|
|
roleDTOS.addAll(queryGovStaffRoles(staffId, agencyId)); |
|
|
|
// 查询网格中的角色
|
|
|
|
roleDTOS.addAll(queryGovStaffRoles(staffId, gridId)); |
|
|
|
List<GovStaffRoleDTO> roleDTOS = queryGovStaffRoles(staffId, belongAgency.getId()); |
|
|
|
|
|
|
|
// 拼装操作key列表
|
|
|
|
Set<String> opeKeys = new HashSet<>(); |
|
|
|
//Set<String> opeKeys = new HashSet<>();
|
|
|
|
// 角色ID列表
|
|
|
|
Set<String> roleIds = new HashSet<>(); |
|
|
|
//Set<String> roleIds = new HashSet<>();
|
|
|
|
|
|
|
|
// 当前组织和所属组织的orgId路径,以及他们的上下级关系
|
|
|
|
String belongOrgIdPath = getOrgIdPath(belongAgency); |
|
|
|
String currOrgIdPath = getOrgIdPath(currAgencyDto); |
|
|
|
String currOrgRelation = getCurrOrgRelation(belongOrgIdPath, currOrgIdPath); |
|
|
|
|
|
|
|
HashSet<String> filtedOps = new HashSet<>(); |
|
|
|
roleDTOS.forEach(roleDto -> { |
|
|
|
String roleId = roleDto.getId(); |
|
|
|
Result<List<RoleOperationResultDTO>> result = govAccessFeignClient.listOperationsByRoleId(roleId); |
|
|
|
if (result.getCode() != 0) { |
|
|
|
// 找出该角色的所有功能操作列表
|
|
|
|
Result<Set<RoleOpeScopeResultDTO>> result = govAccessFeignClient.listRoleAllOperationScopesByRoleId(roleId); |
|
|
|
if (!result.success()) { |
|
|
|
// 获取operation异常
|
|
|
|
logger.error("调用GovAccess,根据RoleId查询Operation列表失败:{}", result.getMsg()); |
|
|
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); |
|
|
|
} |
|
|
|
List<RoleOperationResultDTO> roleOperations = result.getData(); |
|
|
|
// 角色id
|
|
|
|
roleIds.add(roleDto.getId()); |
|
|
|
if (!CollectionUtils.isEmpty(roleOperations)) { |
|
|
|
roleOperations.forEach(roleOpe -> { |
|
|
|
if (roleOpe != null) { |
|
|
|
opeKeys.add(roleOpe.getOperationKey()); |
|
|
|
} |
|
|
|
Set<RoleOpeScopeResultDTO> roleOperations = result.getData(); |
|
|
|
filtedOps.addAll(filterOpesByScope(currOrgRelation, roleOperations)); |
|
|
|
}); |
|
|
|
return filtedOps; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据上下级关系过滤出要返回的权限列表 |
|
|
|
* |
|
|
|
* @param currOrgRelation |
|
|
|
* @param roleOperations |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private HashSet<String> filterOpesByScope(String currOrgRelation, Set<RoleOpeScopeResultDTO> roleOperations) { |
|
|
|
HashSet<String> opeKeys = new HashSet<>(); |
|
|
|
roleOperations.stream().forEach(opeScope -> { |
|
|
|
String scopeKey = opeScope.getScopeKey(); |
|
|
|
switch (currOrgRelation) { |
|
|
|
case ORG_RELATION_SAME:// 就在所在机构下
|
|
|
|
if (OpeScopeConstant.ORG_CURR.equals(scopeKey) |
|
|
|
|| OpeScopeConstant.ORG_CURR_AND_SUB.equals(scopeKey) |
|
|
|
|| OpeScopeConstant.ORG_CURR_AND_SUP.equals(scopeKey)) { |
|
|
|
opeKeys.add(opeScope.getOperationKey()); |
|
|
|
} |
|
|
|
break; |
|
|
|
case ORG_RELATION_SUB:// 所在机构的子级
|
|
|
|
if (OpeScopeConstant.ORG_CURR_SUB.equals(scopeKey) |
|
|
|
|| OpeScopeConstant.ORG_CURR_AND_SUB.equals(scopeKey)) { |
|
|
|
opeKeys.add(opeScope.getOperationKey()); |
|
|
|
} |
|
|
|
break; |
|
|
|
case ORG_RELATION_SUP:// 所在机构的上级
|
|
|
|
if (OpeScopeConstant.ORG_CURR_SUP.equals(scopeKey) |
|
|
|
|| OpeScopeConstant.ORG_CURR_AND_SUP.equals(scopeKey)) { |
|
|
|
opeKeys.add(opeScope.getOperationKey()); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
}); |
|
|
|
return opeKeys; |
|
|
|
} |
|
|
|
|
|
|
|
// 查询该直属机关的orgIdPath
|
|
|
|
Result<CustomerAgencyDTO> agencyById = govOrgFeignClient.getAgencyById(agencyId); |
|
|
|
if (!agencyById.success() || agencyById.getData() == null) { |
|
|
|
logger.error("根据当前机构id[{}]查询pids失败:{}", agencyId, agencyById.getMsg()); |
|
|
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); |
|
|
|
/** |
|
|
|
* 获取当前所处机关与所属机关的关系 |
|
|
|
*/ |
|
|
|
private String getCurrOrgRelation(String belongOrgPath, String currAgencyPath) { |
|
|
|
if (belongOrgPath.equals(currAgencyPath)) { |
|
|
|
// 当前机关即该人员所属机关
|
|
|
|
return ORG_RELATION_SAME; |
|
|
|
} else if (currAgencyPath.indexOf(belongOrgPath.concat(":")) > -1) { |
|
|
|
// 说明当前所在机关是人员所属机关子级,只取出scope为sub的
|
|
|
|
return ORG_RELATION_SUB; |
|
|
|
} else if (belongOrgPath.indexOf(currAgencyPath.concat(":")) > -1) { |
|
|
|
// 说明当前所在机关是人员所属机关的上级
|
|
|
|
return ORG_RELATION_SUP; |
|
|
|
} |
|
|
|
return belongOrgPath; |
|
|
|
} |
|
|
|
|
|
|
|
// 将最新权限缓存到redis,为了尽量统一操作入口,调用gov-access接口实现
|
|
|
|
StaffPermCacheFormDTO staffPermCacheFormDTO = new StaffPermCacheFormDTO(); |
|
|
|
staffPermCacheFormDTO.setApp(app); |
|
|
|
staffPermCacheFormDTO.setClient(client); |
|
|
|
staffPermCacheFormDTO.setStaffId(staffId); |
|
|
|
staffPermCacheFormDTO.setPermissions(opeKeys); |
|
|
|
staffPermCacheFormDTO.setRoleIdList(roleIds); |
|
|
|
staffPermCacheFormDTO.setGridId(gridId); |
|
|
|
// 拼接orgIdPath
|
|
|
|
staffPermCacheFormDTO.setOrgIdPath(String.format("%s:%s", agencyById.getData().getPids(), agencyId)); |
|
|
|
govAccessFeignClient.updatePermissionCache(staffPermCacheFormDTO); |
|
|
|
return opeKeys; |
|
|
|
/** |
|
|
|
* 获取组织的orgId路径 |
|
|
|
*/ |
|
|
|
public String getOrgIdPath(CustomerAgencyDTO agency) { |
|
|
|
// 拼接单位ID全路径
|
|
|
|
if ("0".equals(agency.getPid())) { |
|
|
|
// 顶级组织
|
|
|
|
return agency.getId(); |
|
|
|
} else { |
|
|
|
return agency.getPids().concat(":").concat(agency.getId()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//private boolean filterCurrOrgKeysByScope(Set<String> roleIds, String opeKey) {
|
|
|
|
// for (String roleId : roleIds) {
|
|
|
|
// Result<Set<RoleOpeScopeResultDTO>> result = govAccessFeignClient.listRoleAllOperationScopesByRoleId(roleId);
|
|
|
|
// if (!result.success()) {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
// Set<RoleOpeScopeResultDTO> scopeDTOS = result.getData();
|
|
|
|
// Set<String> scopeKeys = scopeDTOS.stream()
|
|
|
|
// .filter(scope -> scope.getScopeKey().equals(opeKey))
|
|
|
|
// .map(scope -> scope.getScopeKey())
|
|
|
|
// .collect(Collectors.toSet());
|
|
|
|
// if (scopeKeys.contains(OpeScopeConstant.ORG_CURR)
|
|
|
|
// || scopeKeys.contains(OpeScopeConstant.ORG_CURR_AND_SUB)
|
|
|
|
// || scopeKeys.contains(OpeScopeConstant.ORG_CURR_AND_SUP)) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
//}
|
|
|
|
|
|
|
|
//private boolean filterSubOrgKeysByScope(Set<String> roleIds, String opeKey) {
|
|
|
|
// for (String roleId : roleIds) {
|
|
|
|
// OperationScopeFormDTO opeScopeFormDTO = new OperationScopeFormDTO();
|
|
|
|
// opeScopeFormDTO.setRoleId(roleId);
|
|
|
|
// opeScopeFormDTO.setOperationKey(opeKey);
|
|
|
|
// Result<Set<OperationScopeDTO>> result = govAccessFeignClient.getOperationScopesByRoleId(opeScopeFormDTO);
|
|
|
|
// if (!result.success()) {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Set<OperationScopeDTO> scopeDTOS = result.getData();
|
|
|
|
// Set<String> scopeKeys = scopeDTOS.stream().map(scope -> scope.getScopeKey()).collect(Collectors.toSet());
|
|
|
|
// if (scopeKeys.contains(OpeScopeConstant.ORG_CURR_SUB)
|
|
|
|
// || scopeKeys.contains(OpeScopeConstant.ORG_CURR_AND_SUB)) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
//}
|
|
|
|
|
|
|
|
//private boolean filterSupOrgKeysByScope(Set<String> roleIds, String opeKey) {
|
|
|
|
// for (String roleId : roleIds) {
|
|
|
|
// OperationScopeFormDTO opeScopeFormDTO = new OperationScopeFormDTO();
|
|
|
|
// opeScopeFormDTO.setRoleId(roleId);
|
|
|
|
// opeScopeFormDTO.setOperationKey(opeKey);
|
|
|
|
// Result<Set<OperationScopeDTO>> result = govAccessFeignClient.getOperationScopesByRoleId(opeScopeFormDTO);
|
|
|
|
// if (!result.success()) {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Set<OperationScopeDTO> scopeDTOS = result.getData();
|
|
|
|
// Set<String> scopeKeys = scopeDTOS.stream().map(scope -> scope.getScopeKey()).collect(Collectors.toSet());
|
|
|
|
// if (scopeKeys.contains(OpeScopeConstant.ORG_CURR_SUP)
|
|
|
|
// || scopeKeys.contains(OpeScopeConstant.ORG_CURR_AND_SUP)) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
//}
|
|
|
|
|
|
|
|
/** |
|
|
|
* 过滤有效范围 |
|
|
|
* |
|
|
|
* @param scopeDTOS |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private HashSet<String> filteScopes(Set<OperationScopeDTO> scopeDTOS) { |
|
|
|
HashMap<String, OperationScopeDTO> filtedScopes = new HashMap<>(); |
|
|
|
|
|
|
|
for (OperationScopeDTO scope : scopeDTOS) { |
|
|
|
String scopeIndex = scope.getScopeIndex(); |
|
|
|
if (StringUtils.isBlank(scopeIndex)) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
String[] currArr = scopeIndex.split("_"); |
|
|
|
if ("0".equals(currArr[1])) { |
|
|
|
// 为0,说明没有包含关系,直接放入
|
|
|
|
filtedScopes.put(scopeIndex, scope); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
OperationScopeDTO tempScope = filtedScopes.get(currArr[0]); |
|
|
|
if (tempScope != null) { |
|
|
|
// 已经有ac开头的了
|
|
|
|
String tempScopeIndex = tempScope.getScopeIndex(); |
|
|
|
if (Integer.valueOf(currArr[1]) < Integer.valueOf(tempScopeIndex.split("_")[1])) { |
|
|
|
filtedScopes.put(currArr[0], scope); |
|
|
|
} |
|
|
|
} else { |
|
|
|
filtedScopes.put(currArr[0], scope); |
|
|
|
} |
|
|
|
} |
|
|
|
HashSet<String> scopeStrs = new HashSet<>(); |
|
|
|
Set<Map.Entry<String, OperationScopeDTO>> entries = filtedScopes.entrySet(); |
|
|
|
for (Map.Entry<String, OperationScopeDTO> entry : entries) { |
|
|
|
scopeStrs.add(entry.getValue().getScopeKey()); |
|
|
|
} |
|
|
|
return scopeStrs; |
|
|
|
} |
|
|
|
} |
|
|
|