Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ujia
/
uj-store
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit a1c2d15a
authored
Oct 10, 2026
by
张立顺
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix(store): enforce updated approval and listing permissions
1 parent
ec5c2281
Pipeline
#327
for
a1c2d15a
failed
in 0 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
189 additions
and
42 deletions
src/access/points.ts
src/access/runtime/AccessProvider.tsx
src/components/__tests__/store-permissions.test.tsx
src/pages/StoreApprove/__tests__/StoreApprove.test.tsx
src/pages/StoreApprove/index.tsx
src/access/points.ts
View file @
a1c2d15
...
@@ -16,6 +16,8 @@ export const P = {
...
@@ -16,6 +16,8 @@ export const P = {
/** 「审核」按钮挂在后端的审批通过动作码上 */
/** 「审核」按钮挂在后端的审批通过动作码上 */
STORE_APPROVE_AUDIT
:
'store:approve:pass'
,
STORE_APPROVE_AUDIT
:
'store:approve:pass'
,
STORE_APPROVE_REJECT
:
'store:approve:reject'
,
STORE_APPROVE_REJECT
:
'store:approve:reject'
,
/** 后端「撤回」按钮的权限码为 submit,不能用页面查看权限替代。 */
STORE_APPROVE_REVOKE
:
'store:approve:submit'
,
// 门店列表(后端码:store:list:*)
// 门店列表(后端码:store:list:*)
/** 页面级裸资源:供 can() 判定页面可见性,不用于 canPoint */
/** 页面级裸资源:供 can() 判定页面可见性,不用于 canPoint */
STORE_LISTING
:
'store:list'
,
STORE_LISTING
:
'store:list'
,
...
...
src/access/runtime/AccessProvider.tsx
View file @
a1c2d15
...
@@ -30,7 +30,7 @@ interface AccessContextValue {
...
@@ -30,7 +30,7 @@ interface AccessContextValue {
roleCodes: string[];
roleCodes: string[];
setRole: (r: RoleKey) => void;
setRole: (r: RoleKey) => void;
matrix: PermissionMatrix;
matrix: PermissionMatrix;
/** 页面级权限:
统一以 points 为准(页面资源裸码 = 可见,:manage = 完全权)
*/
/** 页面级权限:
门店审批与列表必须有精确 :view,其他资源沿用既有规则。
*/
can: (resource: ResourceKey, required?: 'view' | 'full') => boolean;
can: (resource: ResourceKey, required?: 'view' | 'full') => boolean;
/** 当前账号持有的权限点集合(后端返回,经适配器归一化) */
/** 当前账号持有的权限点集合(后端返回,经适配器归一化) */
points: Set<string>;
points: Set<string>;
...
@@ -117,9 +117,12 @@ export function AccessProvider({ children }: { children: ReactNode }) {
...
@@ -117,9 +117,12 @@ export function AccessProvider({ children }: { children: ReactNode }) {
setRole,
setRole,
matrix,
matrix,
can: (resource, required = 'view') => {
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 (points.has(resource)) return true;
if (required === 'full') return points.has(`${resource}:manage`);
if (required === 'full') return points.has(`${resource}:manage`);
//
后端若只下发叶子权限码(如 store:list:export),亦视为该资源可见
//
其他资源仍兼容仅下发叶子权限码的情况。
return [...points].some((p) => p.startsWith(`${resource}:`));
return [...points].some((p) => p.startsWith(`${resource}:`));
},
},
points,
points,
...
...
src/components/__tests__/store-permissions.test.tsx
View file @
a1c2d15
...
@@ -8,6 +8,7 @@ import {
...
@@ -8,6 +8,7 @@ import {
normalizePermissions,
normalizePermissions,
saveLoginSession,
saveLoginSession,
useAccess,
useAccess,
type ResourceKey,
} from '@/access';
} from '@/access';
import { AccessTestProvider } from '@/test/AccessTestProvider';
import { AccessTestProvider } from '@/test/AccessTestProvider';
import { setToken } from '@/api/core/token';
import { setToken } from '@/api/core/token';
...
@@ -35,7 +36,67 @@ function PermissionProbe() {
...
@@ -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('门店操作权限', () => {
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 显示审核与导出,无打回权限不显示打回', () => {
it('表格沿用登录 permCodes 显示审核与导出,无打回权限不显示打回', () => {
render(
render(
<AccessTestProvider permCodes={['store:approve:pass', 'store:list:export']}>
<AccessTestProvider permCodes={['store:approve:pass', 'store:list:export']}>
...
...
src/pages/StoreApprove/__tests__/StoreApprove.test.tsx
View file @
a1c2d15
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 { App, ConfigProvider } from 'antd';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
...
@@ -75,20 +75,19 @@ function mount(permCodes = ['store:approve:view', 'store:approve:pass', 'store:a
...
@@ -75,20 +75,19 @@ function mount(permCodes = ['store:approve:view', 'store:approve:pass', 'store:a
const client = new QueryClient({
const client = new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: 30_000 }, mutations: { retry: false } },
defaultOptions: { queries: { retry: false, staleTime: 30_000 }, mutations: { retry: false } },
});
});
return {
const page = (nextPermCodes: string[]) => (
client,
<ConfigProvider button={{ autoInsertSpace: false }}>
...render(
<App>
<ConfigProvider button={{ autoInsertSpace: false }}>
<QueryClientProvider client={client}>
<App>
<AccessTestProvider permCodes={nextPermCodes}>
<QueryClientProvider client={client}>
<StoreApprove />
<AccessTestProvider permCodes={permCodes}>
</AccessTestProvider>
<StoreApprove />
</QueryClientProvider>
</AccessTestProvider>
</App>
</QueryClientProvider>
</ConfigProvider>
</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;以弹窗内确认按钮定位。
// rc-util 在 test 环境把所有 aria-labelledby ID 固定为 test-id;以弹窗内确认按钮定位。
...
@@ -362,7 +361,12 @@ describe('门店审批页面', () => {
...
@@ -362,7 +361,12 @@ describe('门店审批页面', () => {
});
});
it('只读查看无通过或打回入口,非本人列表无撤回入口', async () => {
it('只读查看无通过或打回入口,非本人列表无撤回入口', async () => {
mount();
mount([
'store:approve:view',
'store:approve:pass',
'store:approve:reject',
'store:approve:submit',
]);
fireEvent.click(await screen.findByRole('button', { name: '查看' }));
fireEvent.click(await screen.findByRole('button', { name: '查看' }));
await screen.findByText('优加商务经理');
await screen.findByText('优加商务经理');
expect(screen.queryByRole('button', { name: '通过' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: '通过' })).not.toBeInTheDocument();
...
@@ -370,14 +374,14 @@ describe('门店审批页面', () => {
...
@@ -370,14 +374,14 @@ describe('门店审批页面', () => {
expect(screen.queryByRole('button', { name: '撤回' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: '撤回' })).not.toBeInTheDocument();
});
});
it('
仅查看权限也允许本人待办
撤回,确认后刷新状态与操作区', async () => {
it('
查看加 submit 权限允许本人待办列表
撤回,确认后刷新状态与操作区', async () => {
mine = [
mine = [
{ ...row, listingId: '202', status: 'PENDING', statusLabel: '待审批', actionable: false },
{ ...row, listingId: '202', status: 'PENDING', statusLabel: '待审批', actionable: false },
];
];
api.revokeApproval.mockImplementation(async () => {
api.revokeApproval.mockImplementation(async () => {
mine = [{ ...mine[0], status: 'REVOKED', statusLabel: '已撤回' }];
mine = [{ ...mine[0], status: 'REVOKED', statusLabel: '已撤回' }];
});
});
mount(['store:approve:view']);
mount(['store:approve:view'
, 'store:approve:submit'
]);
await screen.findByText('测试门店');
await screen.findByText('测试门店');
fireEvent.click(screen.getByRole('tab', { name: /我发起的/ }));
fireEvent.click(screen.getByRole('tab', { name: /我发起的/ }));
fireEvent.click(await screen.findByRole('button', { name: '撤回' }));
fireEvent.click(await screen.findByRole('button', { name: '撤回' }));
...
@@ -391,6 +395,82 @@ describe('门店审批页面', () => {
...
@@ -391,6 +395,82 @@ describe('门店审批页面', () => {
expect(screen.queryByRole('button', { name: '撤回' })).not.toBeInTheDocument();
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 () => {
it('没有处置权限只显示查看,不渲染批量入口和选择框', async () => {
mount(['store:approve:view']);
mount(['store:approve:view']);
await screen.findByText('测试门店');
await screen.findByText('测试门店');
...
...
src/pages/StoreApprove/index.tsx
View file @
a1c2d15
...
@@ -37,6 +37,9 @@ export default function StoreApprove() {
...
@@ -37,6 +37,9 @@ export default function StoreApprove() {
const allowed = canPoint(P.STORE_APPROVE_VIEW);
const allowed = canPoint(P.STORE_APPROVE_VIEW);
const canPass = canPoint(P.STORE_APPROVE_AUDIT);
const canPass = canPoint(P.STORE_APPROVE_AUDIT);
const canReject = canPoint(P.STORE_APPROVE_REJECT);
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 { message } = App.useApp();
const client = useQueryClient();
const client = useQueryClient();
const [tab, setTab] = useState<ApproveTab>(() =>
const [tab, setTab] = useState<ApproveTab>(() =>
...
@@ -135,7 +138,7 @@ export default function StoreApprove() {
...
@@ -135,7 +138,7 @@ export default function StoreApprove() {
},
},
});
});
const openAction = (next: ApprovalAction) => {
const openAction = (next: ApprovalAction) => {
if (mutation.isPending) return;
if (mutation.isPending
|| !canAct(next.type)
) return;
setAction(next);
setAction(next);
setConfirmed(false);
setConfirmed(false);
setReason('');
setReason('');
...
@@ -155,11 +158,7 @@ export default function StoreApprove() {
...
@@ -155,11 +158,7 @@ export default function StoreApprove() {
setActionError('请填写 1–200 字的打回原因');
setActionError('请填写 1–200 字的打回原因');
return;
return;
}
}
if (
if (!canAct(action.type)) return;
((action.type === 'pass' || action.type === 'batch') && !canPass) ||
(action.type === 'reject' && !canReject)
)
return;
submissionLock.current = true;
submissionLock.current = true;
mutation.mutate({ ...action, reason: reason.trim() });
mutation.mutate({ ...action, reason: reason.trim() });
};
};
...
@@ -223,7 +222,7 @@ export default function StoreApprove() {
...
@@ -223,7 +222,7 @@ export default function StoreApprove() {
},
},
{
{
label: '撤回',
label: '撤回',
permission: P.STORE_APPROVE_
VIEW
,
permission: P.STORE_APPROVE_
REVOKE
,
disabled: mutation.isPending,
disabled: mutation.isPending,
visible: (row) => tab === 'mine' && row.status === 'PENDING',
visible: (row) => tab === 'mine' && row.status === 'PENDING',
onClick: (row) => openAction({ type: 'revoke', records: [row] }),
onClick: (row) => openAction({ type: 'revoke', records: [row] }),
...
@@ -415,7 +414,7 @@ export default function StoreApprove() {
...
@@ -415,7 +414,7 @@ export default function StoreApprove() {
detail.data?.status === 'PENDING' &&
detail.data?.status === 'PENDING' &&
drawerRecord && (
drawerRecord && (
<PermButton
<PermButton
permission={P.STORE_APPROVE_
VIEW
}
permission={P.STORE_APPROVE_
REVOKE
}
disabled={mutation.isPending}
disabled={mutation.isPending}
onClick={() => openAction({ type: 'revoke', records: [drawerRecord] })}
onClick={() => openAction({ type: 'revoke', records: [drawerRecord] })}
>
>
...
@@ -425,20 +424,22 @@ export default function StoreApprove() {
...
@@ -425,20 +424,22 @@ export default function StoreApprove() {
</div>
</div>
}
}
/>
/>
<ApprovalActionModal
{(!action || canAct(action.type)) && (
action={action}
<ApprovalActionModal
spec={STORE_DICTIONARIES}
action={action}
pending={mutation.isPending}
spec={STORE_DICTIONARIES}
confirmed={confirmed}
pending={mutation.isPending}
reason={reason}
confirmed={confirmed}
error={actionError}
reason={reason}
onConfirmed={setConfirmed}
error={actionError}
onReason={setReason}
onConfirmed={setConfirmed}
onCancel={() => {
onReason={setReason}
if (!mutation.isPending) setAction(undefined);
onCancel={() => {
}}
if (!mutation.isPending) setAction(undefined);
onConfirm={confirmAction}
}}
/>
onConfirm={confirmAction}
/>
)}
</div>
</div>
);
);
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment