|
|
@@ -2,7 +2,7 @@ import { PermTree } from '@/pages/Admin/_shared/PermTree';
|
|
|
import { calcUserPermDelta } from '@/pages/Admin/_shared/PermTree/lib/permUtils';
|
|
|
import { fetchPermList } from '@/services/perm';
|
|
|
import { fetchRoleDetail } from '@/services/role';
|
|
|
-import { fetchSetUserPerms, fetchUserDetail } from '@/services/user';
|
|
|
+import { fetchGetUserPerms, fetchSetUserPerms, fetchUserDetail } from '@/services/user';
|
|
|
import { Button, Drawer, Spin, message } from 'antd';
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
@@ -25,27 +25,36 @@ export const UserPermDrawer = ({ userId, productCode, open, onClose }: UserPermD
|
|
|
setLoading(true);
|
|
|
|
|
|
const load = async () => {
|
|
|
- const [userRes, permRes] = await Promise.all([
|
|
|
+ const [userRes, permRes, userPermsRes] = await Promise.all([
|
|
|
fetchUserDetail({ id: userId }),
|
|
|
fetchPermList({ productCode, pageSize: 9999 }),
|
|
|
+ fetchGetUserPerms({ userId }),
|
|
|
]);
|
|
|
const perms = permRes.data?.list ?? [];
|
|
|
setAllPerms(perms);
|
|
|
|
|
|
- const codeToId = new Map(perms.map((p) => [p.code, p.id]));
|
|
|
-
|
|
|
- const userPermCodes = userRes.data?.perms ?? [];
|
|
|
- const userPermIds = userPermCodes.flatMap((code) => {
|
|
|
- const id = codeToId.get(code);
|
|
|
- return id !== undefined ? [id] : [];
|
|
|
- });
|
|
|
- setCheckedIds(userPermIds);
|
|
|
+ // 用 GetUserPerms 返回的覆盖项重建 checkedIds:
|
|
|
+ // ALLOW 项 = 用户独有授权,加入选中; DENY 项 = 用户主动撤销,不加入。
|
|
|
+ // 角色继承的权限直接从 roleDetails 读出,最终 checkedIds = roleInherited ∪ ALLOW - DENY。
|
|
|
+ const idToId = new Map(perms.map((p) => [p.id, p.id]));
|
|
|
+ const userPerms = userPermsRes.data?.perms ?? [];
|
|
|
+ const allowIds = new Set(
|
|
|
+ userPerms
|
|
|
+ .filter((p) => p.effect === 'ALLOW')
|
|
|
+ .map((p) => p.permId)
|
|
|
+ .filter((id) => idToId.has(id)),
|
|
|
+ );
|
|
|
+ const denyIds = new Set(userPerms.filter((p) => p.effect === 'DENY').map((p) => p.permId));
|
|
|
|
|
|
const roleIds = userRes.data?.roleIds ?? [];
|
|
|
const roleDetails = await Promise.all(roleIds.map((id) => fetchRoleDetail({ id })));
|
|
|
const inherited = new Set<number>();
|
|
|
roleDetails.forEach((r) => (r.data?.permIds ?? []).forEach((id) => inherited.add(id)));
|
|
|
setRoleInheritedIds([...inherited]);
|
|
|
+
|
|
|
+ // 有效选中 = 角色继承 + ALLOW - DENY
|
|
|
+ const effective = [...inherited, ...allowIds].filter((id) => !denyIds.has(id));
|
|
|
+ setCheckedIds([...new Set(effective)]);
|
|
|
};
|
|
|
|
|
|
load().finally(() => setLoading(false));
|