Commit a1c2d15a by 张立顺

fix(store): enforce updated approval and listing permissions

1 parent ec5c2281
Pipeline #327 for a1c2d15a failed in 0 seconds
......@@ -16,6 +16,8 @@ export const P = {
/** 「审核」按钮挂在后端的审批通过动作码上 */
STORE_APPROVE_AUDIT: 'store:approve:pass',
STORE_APPROVE_REJECT: 'store:approve:reject',
/** 后端「撤回」按钮的权限码为 submit,不能用页面查看权限替代。 */
STORE_APPROVE_REVOKE: 'store:approve:submit',
// 门店列表(后端码:store:list:*)
/** 页面级裸资源:供 can() 判定页面可见性,不用于 canPoint */
STORE_LISTING: 'store:list',
......
......@@ -30,7 +30,7 @@ interface AccessContextValue {
roleCodes: string[];
setRole: (r: RoleKey) => void;
matrix: PermissionMatrix;
/** 页面级权限:统一以 points 为准(页面资源裸码 = 可见,:manage = 完全权) */
/** 页面级权限:门店审批与列表必须有精确 :view,其他资源沿用既有规则。 */
can: (resource: ResourceKey, required?: 'view' | 'full') => boolean;
/** 当前账号持有的权限点集合(后端返回,经适配器归一化) */
points: Set<string>;
......@@ -117,9 +117,12 @@ export function AccessProvider({ children }: { children: ReactNode }) {
setRole,
matrix,
can: (resource, required = 'view') => {
if (required === 'view' && (resource === 'store:approve' || resource === 'store:list')) {
return points.has(`${resource}:view`);
}
if (points.has(resource)) return true;
if (required === 'full') return points.has(`${resource}:manage`);
// 后端若只下发叶子权限码(如 store:list:export),亦视为该资源可见
// 其他资源仍兼容仅下发叶子权限码的情况。
return [...points].some((p) => p.startsWith(`${resource}:`));
},
points,
......
......@@ -8,6 +8,7 @@ import {
normalizePermissions,
saveLoginSession,
useAccess,
type ResourceKey,
} from '@/access';
import { AccessTestProvider } from '@/test/AccessTestProvider';
import { setToken } from '@/api/core/token';
......@@ -35,7 +36,67 @@ function PermissionProbe() {
);
}
function ResourceAccessProbe({ resource }: { resource: ResourceKey }) {
const { can } = useAccess();
return (
<>
<output aria-label="默认入口权限">{String(can(resource))}</output>
<output aria-label="查看入口权限">{String(can(resource, 'view'))}</output>
<output aria-label="完全权限">{String(can(resource, 'full'))}</output>
</>
);
}
describe('门店操作权限', () => {
it.each([
['store:approve', undefined, false, false],
['store:approve', 'store:approve', false, true],
['store:approve', 'store:approve:pass', false, false],
['store:approve', 'store:approve:reject', false, false],
['store:approve', 'store:approve:submit', false, false],
['store:approve', 'store:approve:resubmit', false, false],
['store:approve', 'store:approve:manage', false, true],
['store:approve', 'store:approve:view:extra', false, false],
['store:approve', 'store:approve:view', true, false],
['store:list', undefined, false, false],
['store:list', 'store:list', false, true],
['store:list', 'store:list:export', false, false],
['store:list', 'store:list:manage', false, true],
['store:list', 'store:list:view:extra', false, false],
['store:list', 'store:list:view', true, false],
] as const)(
'门店菜单与快捷入口 %s 仅接受精确 view,当前权限 %s',
(resource, permission, view, full) => {
render(
<AccessTestProvider permCodes={permission ? [permission] : []}>
<ResourceAccessProbe resource={resource} />
</AccessTestProvider>,
);
expect(screen.getByLabelText('默认入口权限')).toHaveTextContent(String(view));
expect(screen.getByLabelText('查看入口权限')).toHaveTextContent(String(view));
expect(screen.getByLabelText('完全权限')).toHaveTextContent(String(full));
},
);
it.each([
['system:user', 'system:user', true, true],
['system:user', 'system:user:save', true, false],
['system:user', 'system:user:manage', true, true],
['store:import', 'store:import:submit', true, false],
] as const)(
'门店入口权限收紧不改变其他资源 %s 的既有规则,当前权限 %s',
(resource, permission, view, full) => {
render(
<AccessTestProvider permCodes={[permission]}>
<ResourceAccessProbe resource={resource} />
</AccessTestProvider>,
);
expect(screen.getByLabelText('默认入口权限')).toHaveTextContent(String(view));
expect(screen.getByLabelText('查看入口权限')).toHaveTextContent(String(view));
expect(screen.getByLabelText('完全权限')).toHaveTextContent(String(full));
},
);
it('表格沿用登录 permCodes 显示审核与导出,无打回权限不显示打回', () => {
render(
<AccessTestProvider permCodes={['store:approve:pass', 'store:list:export']}>
......
import { render, screen, fireEvent, waitFor, within } from '@testing-library/react';
import { act, render, screen, fireEvent, waitFor, within } from '@testing-library/react';
import { App, ConfigProvider } from 'antd';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { beforeEach, describe, expect, it, vi } from 'vitest';
......@@ -75,20 +75,19 @@ function mount(permCodes = ['store:approve:view', 'store:approve:pass', 'store:a
const client = new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: 30_000 }, mutations: { retry: false } },
});
return {
client,
...render(
<ConfigProvider button={{ autoInsertSpace: false }}>
<App>
<QueryClientProvider client={client}>
<AccessTestProvider permCodes={permCodes}>
<StoreApprove />
</AccessTestProvider>
</QueryClientProvider>
</App>
</ConfigProvider>,
),
};
const page = (nextPermCodes: string[]) => (
<ConfigProvider button={{ autoInsertSpace: false }}>
<App>
<QueryClientProvider client={client}>
<AccessTestProvider permCodes={nextPermCodes}>
<StoreApprove />
</AccessTestProvider>
</QueryClientProvider>
</App>
</ConfigProvider>
);
const view = render(page(permCodes));
return { client, ...view, setPermissions: (next: string[]) => view.rerender(page(next)) };
}
// rc-util 在 test 环境把所有 aria-labelledby ID 固定为 test-id;以弹窗内确认按钮定位。
......@@ -362,7 +361,12 @@ describe('门店审批页面', () => {
});
it('只读查看无通过或打回入口,非本人列表无撤回入口', async () => {
mount();
mount([
'store:approve:view',
'store:approve:pass',
'store:approve:reject',
'store:approve:submit',
]);
fireEvent.click(await screen.findByRole('button', { name: '查看' }));
await screen.findByText('优加商务经理');
expect(screen.queryByRole('button', { name: '通过' })).not.toBeInTheDocument();
......@@ -370,14 +374,14 @@ describe('门店审批页面', () => {
expect(screen.queryByRole('button', { name: '撤回' })).not.toBeInTheDocument();
});
it('仅查看权限也允许本人待办撤回,确认后刷新状态与操作区', async () => {
it('查看加 submit 权限允许本人待办列表撤回,确认后刷新状态与操作区', async () => {
mine = [
{ ...row, listingId: '202', status: 'PENDING', statusLabel: '待审批', actionable: false },
];
api.revokeApproval.mockImplementation(async () => {
mine = [{ ...mine[0], status: 'REVOKED', statusLabel: '已撤回' }];
});
mount(['store:approve:view']);
mount(['store:approve:view', 'store:approve:submit']);
await screen.findByText('测试门店');
fireEvent.click(screen.getByRole('tab', { name: /我发起的/ }));
fireEvent.click(await screen.findByRole('button', { name: '撤回' }));
......@@ -391,6 +395,82 @@ describe('门店审批页面', () => {
expect(screen.queryByRole('button', { name: '撤回' })).not.toBeInTheDocument();
});
it.each([
{ label: '仅查看', permCodes: ['store:approve:view'], location: '列表' },
{ label: '仅查看', permCodes: ['store:approve:view'], location: '详情' },
{
label: '查看和重提',
permCodes: ['store:approve:view', 'store:approve:resubmit'],
location: '列表',
},
{
label: '查看和重提',
permCodes: ['store:approve:view', 'store:approve:resubmit'],
location: '详情',
},
])('$label 权限:本人待办 $location 不显示撤回入口', async ({ permCodes, location }) => {
mine = [{ ...row, listingId: '202', actionable: false }];
detail = { ...detail, ...mine[0] };
mount(permCodes);
await screen.findByText('测试门店');
fireEvent.click(screen.getByRole('tab', { name: /我发起的/ }));
await screen.findByRole('button', { name: '查看' });
if (location === '详情') {
fireEvent.click(screen.getByRole('button', { name: '查看' }));
const drawer = await screen.findByRole('dialog');
await within(drawer).findByText('优加商务经理');
expect(within(drawer).queryByRole('button', { name: '撤回' })).not.toBeInTheDocument();
expect(within(drawer).queryByRole('button', { name: '重新提交' })).not.toBeInTheDocument();
} else {
expect(screen.queryByRole('button', { name: '撤回' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: '重新提交' })).not.toBeInTheDocument();
}
expect(api.revokeApproval).not.toHaveBeenCalled();
});
it('查看加 submit 权限允许本人待审批详情撤回', async () => {
mine = [{ ...row, listingId: '202', actionable: false }];
detail = { ...detail, ...mine[0] };
mount(['store:approve:view', 'store:approve:submit']);
await screen.findByText('测试门店');
fireEvent.click(screen.getByRole('tab', { name: /我发起的/ }));
fireEvent.click(await screen.findByRole('button', { name: '查看' }));
const drawer = await screen.findByRole('dialog');
fireEvent.click(await within(drawer).findByRole('button', { name: '撤回' }));
fireEvent.click(within(actionDialog('确认撤回')).getByRole('button', { name: '确认撤回' }));
await waitFor(() => expect(api.revokeApproval).toHaveBeenCalledWith('202'));
});
it('拥有 resubmit 权限时已打回记录仍不显示重新提交', async () => {
detail = { ...detail, ...mine[0] };
mount(['store:approve:view', 'store:approve:resubmit']);
await screen.findByText('测试门店');
fireEvent.click(screen.getByRole('tab', { name: /我发起的/ }));
fireEvent.click(await screen.findByRole('button', { name: '查看' }));
const drawer = await screen.findByRole('dialog');
await within(drawer).findByText('优加商务经理');
expect(screen.queryByRole('button', { name: '重新提交' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: '撤回' })).not.toBeInTheDocument();
});
it('撤回确认打开后收回 submit 权限,不发起撤回请求', async () => {
mine = [{ ...row, listingId: '202', actionable: false }];
const { setPermissions } = mount(['store:approve:view', 'store:approve:submit']);
await screen.findByText('测试门店');
fireEvent.click(screen.getByRole('tab', { name: /我发起的/ }));
fireEvent.click(await screen.findByRole('button', { name: '撤回' }));
expect(
within(actionDialog('确认撤回')).getByRole('button', { name: '确认撤回' }),
).toBeInTheDocument();
setPermissions(['store:approve:view']);
expect(screen.queryByRole('button', { name: '撤回', hidden: true })).not.toBeInTheDocument();
await act(async () => {
const confirm = screen.queryByRole('button', { name: '确认撤回' });
if (confirm) fireEvent.click(confirm);
});
expect(api.revokeApproval).not.toHaveBeenCalled();
});
it('没有处置权限只显示查看,不渲染批量入口和选择框', async () => {
mount(['store:approve:view']);
await screen.findByText('测试门店');
......
......@@ -37,6 +37,9 @@ export default function StoreApprove() {
const allowed = canPoint(P.STORE_APPROVE_VIEW);
const canPass = canPoint(P.STORE_APPROVE_AUDIT);
const canReject = canPoint(P.STORE_APPROVE_REJECT);
const canRevoke = canPoint(P.STORE_APPROVE_REVOKE);
const canAct = (type: ApprovalAction['type']) =>
allowed && (type === 'revoke' ? canRevoke : type === 'reject' ? canReject : canPass);
const { message } = App.useApp();
const client = useQueryClient();
const [tab, setTab] = useState<ApproveTab>(() =>
......@@ -135,7 +138,7 @@ export default function StoreApprove() {
},
});
const openAction = (next: ApprovalAction) => {
if (mutation.isPending) return;
if (mutation.isPending || !canAct(next.type)) return;
setAction(next);
setConfirmed(false);
setReason('');
......@@ -155,11 +158,7 @@ export default function StoreApprove() {
setActionError('请填写 1–200 字的打回原因');
return;
}
if (
((action.type === 'pass' || action.type === 'batch') && !canPass) ||
(action.type === 'reject' && !canReject)
)
return;
if (!canAct(action.type)) return;
submissionLock.current = true;
mutation.mutate({ ...action, reason: reason.trim() });
};
......@@ -223,7 +222,7 @@ export default function StoreApprove() {
},
{
label: '撤回',
permission: P.STORE_APPROVE_VIEW,
permission: P.STORE_APPROVE_REVOKE,
disabled: mutation.isPending,
visible: (row) => tab === 'mine' && row.status === 'PENDING',
onClick: (row) => openAction({ type: 'revoke', records: [row] }),
......@@ -415,7 +414,7 @@ export default function StoreApprove() {
detail.data?.status === 'PENDING' &&
drawerRecord && (
<PermButton
permission={P.STORE_APPROVE_VIEW}
permission={P.STORE_APPROVE_REVOKE}
disabled={mutation.isPending}
onClick={() => openAction({ type: 'revoke', records: [drawerRecord] })}
>
......@@ -425,20 +424,22 @@ export default function StoreApprove() {
</div>
}
/>
<ApprovalActionModal
action={action}
spec={STORE_DICTIONARIES}
pending={mutation.isPending}
confirmed={confirmed}
reason={reason}
error={actionError}
onConfirmed={setConfirmed}
onReason={setReason}
onCancel={() => {
if (!mutation.isPending) setAction(undefined);
}}
onConfirm={confirmAction}
/>
{(!action || canAct(action.type)) && (
<ApprovalActionModal
action={action}
spec={STORE_DICTIONARIES}
pending={mutation.isPending}
confirmed={confirmed}
reason={reason}
error={actionError}
onConfirmed={setConfirmed}
onReason={setReason}
onCancel={() => {
if (!mutation.isPending) setAction(undefined);
}}
onConfirm={confirmAction}
/>
)}
</div>
);
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!