Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature](RuleTemplate): add permission check for global rule template #597

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ModalName } from '../../../data/ModalName';
import { useDispatch, useSelector } from 'react-redux';
import { SystemRole } from '@actiontech/shared/lib/enum';
import { createSpySuccessResponse } from '@actiontech/shared/lib/testUtil/mockApi';
import { mockUsePermission } from '@actiontech/shared/lib/testUtil/mockHook/mockUsePermission';

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
Expand Down Expand Up @@ -182,4 +183,20 @@ describe('sqle/RuleTemplate/List', () => {
expect(screen.getByText('克隆规则模板')).toBeInTheDocument();
expect(screen.getByText('导出规则模板')).toBeInTheDocument();
});

it('should not render public rule template list when user in not admin', async () => {
mockUseCurrentUser({
...mockCurrentUserReturn,
userRoles: {
...mockCurrentUserReturn.userRoles,
[SystemRole.admin]: false,
[SystemRole.globalManager]: false,
[SystemRole.globalViewing]: false
}
});
customRender();

await act(async () => jest.advanceTimersByTime(3000));
expect(screen.queryByText('公共规则模板')).not.toBeInTheDocument();
});
});
56 changes: 41 additions & 15 deletions packages/sqle/src/page/RuleTemplate/RuleTemplateList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { useTranslation } from 'react-i18next';
import { Space } from 'antd';
import { PageHeader, EmptyBox, SegmentedTabs } from '@actiontech/shared';
import {
PageHeader,
EmptyBox,
SegmentedTabs,
SegmentedTabsProps
} from '@actiontech/shared';
import { TableRefreshButton } from '@actiontech/shared/lib/components/ActiontechTable';
import { useState } from 'react';
import { useCurrentProject } from '@actiontech/shared/lib/features';
import { useMemo, useState } from 'react';
import {
PERMISSIONS,
PermissionsConstantType,
useCurrentProject,
usePermission
} from '@actiontech/shared/lib/features';
import { EnumTemplateType } from './index.type';
import ProjectTable from './ProjectTable';
import EventEmitter from '../../../utils/EventEmitter';
Expand All @@ -26,6 +36,33 @@ const RuleTemplateList = () => {

const [activeKey, setActiveKey] = useState(EnumTemplateType.project);

const { checkPagePermission } = usePermission();

const tabItems = useMemo<SegmentedTabsProps['items']>(() => {
const items: Array<
SegmentedTabsProps['items'][0] & { permission?: PermissionsConstantType }
> = [
{
label: t('ruleTemplate.ruleTemplateTitle.project'),
value: EnumTemplateType.project,
children: <ProjectTable />
},
{
label: t('ruleTemplate.ruleTemplateTitle.common'),
value: EnumTemplateType.common,
children: <CommonTable />,
permission: PERMISSIONS.PAGES.SQLE.RULE_MANAGEMENT
}
];

return items.filter((item) => {
if (item.permission === undefined) {
return true;
}
return checkPagePermission(item.permission);
});
}, [checkPagePermission, t]);

return (
<>
<PageHeader
Expand All @@ -47,18 +84,7 @@ const RuleTemplateList = () => {
<SegmentedTabs
activeKey={activeKey}
onChange={setActiveKey}
items={[
{
label: t('ruleTemplate.ruleTemplateTitle.project'),
value: EnumTemplateType.project,
children: <ProjectTable />
},
{
label: t('ruleTemplate.ruleTemplateTitle.common'),
value: EnumTemplateType.common,
children: <CommonTable />
}
]}
items={tabItems}
/>
<CloneRuleTemplate />
<ExportProjectRuleTemplate />
Expand Down
Loading