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

feat(plugin): allowing referer-restriction to dynamically adapt to the BE rules #2001

Merged
merged 5 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion web/src/components/Plugin/PluginDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const PluginDetail: React.FC<Props> = ({
const [UIForm] = Form.useForm();
const data = initialData[name] || {};
const pluginType = pluginList.find((item) => item.name === name)?.originType;
const pluginSchema = pluginList.find((item) => item.name === name)?.schema;
const [content, setContent] = useState<string>(JSON.stringify(data, null, 2));
const [monacoMode, setMonacoMode] = useState<PluginComponent.MonacoLanguage>(monacoModeList.JSON);
const modeOptions: { label: string; value: string }[] = [
Expand Down Expand Up @@ -411,7 +412,7 @@ const PluginDetail: React.FC<Props> = ({
</Button>
]}
/>
{Boolean(monacoMode === monacoModeList.UIForm) && <PluginForm name={name} form={UIForm} renderForm={!(pluginType === 'auth' && schemaType !== 'consumer')} />}
{Boolean(monacoMode === monacoModeList.UIForm) && <PluginForm name={name} schema={pluginSchema} form={UIForm} renderForm={!(pluginType === 'auth' && schemaType !== 'consumer')} />}
<div style={{ display: monacoMode === monacoModeList.UIForm ? 'none' : 'unset' }}>
<Editor
value={content}
Expand Down
5 changes: 3 additions & 2 deletions web/src/components/Plugin/UI/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ import Cors from './cors';

type Props = {
name: string,
schema: Record<string, any> | undefined,
form: FormInstance,
renderForm: boolean
}

export const PLUGIN_UI_LIST = ['api-breaker', 'basic-auth', 'cors', 'limit-req', 'limit-conn', 'proxy-mirror', 'referer-restriction', 'limit-count'];

export const PluginForm: React.FC<Props> = ({ name, renderForm, form }) => {
export const PluginForm: React.FC<Props> = ({ name, schema, renderForm, form }) => {

const { formatMessage } = useIntl();

Expand All @@ -58,7 +59,7 @@ export const PluginForm: React.FC<Props> = ({ name, renderForm, form }) => {
case 'limit-conn':
return <LimitConn form={form} />
case 'referer-restriction':
return <RefererRestriction form={form} />
return <RefererRestriction form={form} schema={schema} />
default:
return null;
}
Expand Down
14 changes: 9 additions & 5 deletions web/src/components/Plugin/UI/referer-restriction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';

type Props = {
form: FormInstance;
schema: Record<string, any> | undefined;
};

const FORM_ITEM_LAYOUT = {
Expand All @@ -45,13 +46,16 @@ const removeBtnStyle = {
alignItems: 'center',
};

const RefererRestriction: React.FC<Props> = ({ form }) => {
const RefererRestriction: React.FC<Props> = ({ form, schema }) => {
const { formatMessage } = useIntl()
const properties = schema?.properties
const whiteMinLen = properties.whitelist.minItems
const whiteInit = Array(whiteMinLen).join('.').split('.')
return (
<Form
form={form}
{...FORM_ITEM_LAYOUT}
initialValues={{ whitelist: [''] }}
initialValues={{ whitelist: whiteInit }}
>
<Form.List name="whitelist">
{(fields, { add, remove }) => {
Expand All @@ -76,7 +80,7 @@ const RefererRestriction: React.FC<Props> = ({ form }) => {
message: formatMessage({
id: 'page.route.form.itemRulesPatternMessage.domain',
}),
pattern: new RegExp(/^\*?[0-9a-zA-Z-._]+$/, 'g')
pattern: new RegExp(`${properties.whitelist.items.pattern}`, 'g')
}, {
required: true,
message: `${formatMessage({ id: 'component.global.pleaseEnter' })} whitelist`
Expand All @@ -86,7 +90,7 @@ const RefererRestriction: React.FC<Props> = ({ form }) => {
</Form.Item>
</Col>
<Col style={{ ...removeBtnStyle, marginLeft: -10 }}>
{fields.length > 1 ? (
{fields.length > whiteMinLen ? (
liuxiran marked this conversation as resolved.
Show resolved Hide resolved
<MinusCircleOutlined
className="dynamic-delete-button"
onClick={() => {
Expand Down Expand Up @@ -119,7 +123,7 @@ const RefererRestriction: React.FC<Props> = ({ form }) => {
tooltip={formatMessage({ id: 'component.pluginForm.referer-restriction.bypass_missing.tooltip' })}
valuePropName="checked"
>
<Switch />
<Switch defaultChecked={properties.bypass_missing.default} />
</Form.Item>
</Form >
);
Expand Down