feat(2046-permission): 修复角色查询

This commit is contained in:
zhansihu 2024-02-01 15:58:21 +08:00
parent e3f2c9f17b
commit 7a5ec1bfb8
3 changed files with 45 additions and 25 deletions

View File

@ -38,6 +38,8 @@ public class ListIdentityFromPermissionResp {
@NoArgsConstructor
public static class UserVO {
private Long ouId;
private Long identityId;
private Integer identityType;

View File

@ -619,11 +619,12 @@ public class TyrSaasAuthServiceImpl implements TyrSaasAuthService {
return result;
}
List<ListIdentityFromPermissionResp> matchedUsers = getWorkspaceUser(req.getWorkspaceId(), req.getOuId(), workspaceFeatureMap);
List<ListIdentityFromPermissionResp.UserVO> matchedUsers = getWorkspaceUser(req.getWorkspaceId(), req.getOuId(), workspaceFeatureMap);
if (CollectionUtil.isEmpty(matchedUsers)) {
return result;
}
return matchedUsers.get(0);
result.setUsers(matchedUsers);
return result;
}
private Map<Integer, Set<Long>> matchWorkspaceFeature(Long workspaceId, Set<Long> featureIds) {
@ -899,10 +900,25 @@ public class TyrSaasAuthServiceImpl implements TyrSaasAuthService {
}
//从相关角色查询用户-超管和普通角色
return getWorkspaceUser(req.getWorkspaceId(), null, workspaceFeatureMap);
List<ListIdentityFromPermissionResp.UserVO> users = getWorkspaceUser(req.getWorkspaceId(), null, workspaceFeatureMap);
if (CollectionUtil.isEmpty(users)) {
return Collections.emptyList();
}
//按ou分组返回
List<ListIdentityFromPermissionResp> result = new ArrayList<>();
Map<Long, List<ListIdentityFromPermissionResp.UserVO>> userMap = users.stream()
.collect(Collectors.groupingBy(ListIdentityFromPermissionResp.UserVO::getOuId));
for (Map.Entry<Long, List<ListIdentityFromPermissionResp.UserVO>> entry : userMap.entrySet()) {
result.add(ListIdentityFromPermissionResp.builder()
.workspaceId(req.getWorkspaceId())
.ouId(entry.getKey())
.users(entry.getValue())
.build());
}
return result;
}
private List<ListIdentityFromPermissionResp> getWorkspaceUser(Long workspaceId, Long ouId, Map<Integer, Set<Long>> workspaceFeatureMap) {
private List<ListIdentityFromPermissionResp.UserVO> getWorkspaceUser(Long workspaceId, Long ouId, Map<Integer, Set<Long>> workspaceFeatureMap) {
Set<Integer> productTypes = workspaceFeatureMap.keySet();
Set<Long> matchedFeatureIds = workspaceFeatureMap.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
@ -917,6 +933,11 @@ public class TyrSaasAuthServiceImpl implements TyrSaasAuthService {
matchedRoles.addAll(adminRoles);
}
Set<Long> superAdmins = adminRoles.stream()
.filter(r -> RoleTypeEnum.SUPER_ADMIN.getValue().equals(r.getRoleType()))
.map(SaasRole::getId)
.collect(Collectors.toSet());
//普通角色 权限点查角色 -- 不考虑 角色权限集例外
List<SaasRole> normalRoles = roleService.queryRoleByFeatures(matchedFeatureIds);
if (CollectionUtil.isEmpty(normalRoles)) {
@ -942,30 +963,27 @@ public class TyrSaasAuthServiceImpl implements TyrSaasAuthService {
return Collections.emptyList();
}
Map<Long, Set<String>> distinctMap = new HashMap<>();
//ouId -> resp : ou维度去重
Map<Long, ListIdentityFromPermissionResp> resultMap = new HashMap<>();
//ouId -> resp : ou-identityId-identityType维度去重
Map<String, ListIdentityFromPermissionResp.UserVO> distinctMap = new HashMap<>();
//组装去重
for (SaasRoleUserRelation relation : relations) {
Set<String> distinctSet = distinctMap.getOrDefault(relation.getOuId(), new HashSet<>());
boolean suc = distinctSet.add(relation.getIdentityId() + "-" + relation.getIdentityType());
distinctMap.put(relation.getOuId(), distinctSet);
if (!suc) {
continue;
String key = KeyUtil.buildKeyBySeparator(relation.getOuId(), relation.getId(), relation.getIdentityType());
ListIdentityFromPermissionResp.UserVO user = distinctMap.get(key);
if (user == null) {
user = ListIdentityFromPermissionResp.UserVO.builder()
.ouId(relation.getOuId())
.identityId(relation.getIdentityId())
.identityType(relation.getIdentityType())
.personalId(relation.getNaturalPersonId())
.build();
}
ListIdentityFromPermissionResp resp = resultMap.getOrDefault(relation.getOuId(), new ListIdentityFromPermissionResp());
ListIdentityFromPermissionResp.UserVO user = ListIdentityFromPermissionResp.UserVO.builder()
.identityId(relation.getIdentityId())
.identityType(relation.getIdentityType())
.personalId(relation.getNaturalPersonId())
.build();
resp.setWorkspaceId(workspaceId);
resp.setOuId(relation.getOuId());
resp.getUsers().add(user);
resultMap.put(relation.getOuId(), resp);
if (superAdmins.contains(relation.getRoleId())) {
//超管
user.setSuperAdmin(true);
}
distinctMap.put(key, user);
}
return new ArrayList<>(resultMap.values());
return new ArrayList<>(distinctMap.values());
}
}

View File

@ -116,7 +116,7 @@
</select>
<select id="listRoleByFeatures" resultType="cn.axzo.tyr.server.repository.entity.SaasRole">
SELECT DISTINCT r.id, r.`NAME`
SELECT DISTINCT r.id, r.`NAME`, r.product_unit_type AS productUnitType
FROM saas_pgroup_permission_relation pg, saas_pgroup_role_relation rg, saas_role r
WHERE pg.is_delete = 0 AND rg.is_delete = 0 AND r.is_delete = 0
AND pg.group_id = rg.group_id AND rg.role_id = r.id