2021-09-21 18:47:18 +02:00
|
|
|
import React, { useMemo } from 'react';
|
2021-09-13 12:24:11 +02:00
|
|
|
import { useIntl } from 'react-intl';
|
2021-09-21 18:47:18 +02:00
|
|
|
import { Box, Select, Option, GridItem, H3, Text, Stack } from '@strapi/parts';
|
|
|
|
import { get, isEmpty, takeRight, toLower, without } from 'lodash';
|
|
|
|
import { useUsersPermissions } from '../../contexts/UsersPermissionsContext';
|
|
|
|
import BoundRoute from '../BoundRoute';
|
2020-08-06 16:27:51 +02:00
|
|
|
|
|
|
|
const Policies = () => {
|
2021-09-13 12:24:11 +02:00
|
|
|
const { formatMessage } = useIntl();
|
2021-09-21 18:47:18 +02:00
|
|
|
const { modifiedData, selectedAction, routes, policies, onChange } = useUsersPermissions();
|
|
|
|
const path = without(selectedAction.split('.'), 'controllers');
|
|
|
|
const controllerRoutes = get(routes, path[0]);
|
|
|
|
const displayedRoutes = isEmpty(controllerRoutes)
|
|
|
|
? []
|
|
|
|
: controllerRoutes.filter(o => toLower(o.handler) === toLower(takeRight(path, 2).join('.')));
|
|
|
|
|
|
|
|
const inputName = `${selectedAction}.policy`;
|
|
|
|
|
|
|
|
const value = useMemo(() => {
|
|
|
|
return get(modifiedData, inputName, '');
|
|
|
|
}, [inputName, modifiedData]);
|
2020-08-06 16:27:51 +02:00
|
|
|
|
|
|
|
return (
|
2021-09-13 12:24:11 +02:00
|
|
|
<GridItem
|
|
|
|
col={5}
|
|
|
|
background="neutral150"
|
|
|
|
paddingTop={6}
|
|
|
|
paddingBottom={6}
|
|
|
|
paddingLeft={7}
|
|
|
|
paddingRight={7}
|
|
|
|
style={{ minHeight: '100%' }}
|
|
|
|
>
|
2021-09-21 18:47:18 +02:00
|
|
|
<H3>
|
|
|
|
{formatMessage({
|
|
|
|
id: 'users-permissions.Policies.header.title',
|
|
|
|
defaultMessage: 'Advanced settings',
|
|
|
|
})}
|
|
|
|
</H3>
|
|
|
|
{selectedAction ? (
|
2021-09-22 14:48:06 +02:00
|
|
|
<Stack size={6} paddingTop={6}>
|
2021-09-21 18:47:18 +02:00
|
|
|
<Select
|
|
|
|
value={value}
|
2021-09-22 14:48:06 +02:00
|
|
|
name={inputName}
|
|
|
|
onChange={newValue => onChange({ target: { name: inputName, value: newValue } })}
|
2021-09-21 18:47:18 +02:00
|
|
|
label={formatMessage({
|
|
|
|
id: 'Policies.InputSelect.label',
|
|
|
|
defaultMessage: 'Allow to perform this action for:',
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{policies.map(policy => (
|
|
|
|
<Option value={policy.value} key={policy.value}>
|
|
|
|
{policy.label}
|
|
|
|
</Option>
|
2021-09-22 14:48:06 +02:00
|
|
|
))}
|
2021-09-21 18:47:18 +02:00
|
|
|
</Select>
|
2021-09-22 14:48:06 +02:00
|
|
|
{displayedRoutes.map((route, key) => (
|
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
|
|
|
<BoundRoute key={key} route={route} />
|
|
|
|
))}
|
2021-09-21 18:47:18 +02:00
|
|
|
</Stack>
|
|
|
|
) : (
|
|
|
|
<Box paddingTop={2}>
|
|
|
|
<Text as="p" textColor="neutral600">
|
|
|
|
{formatMessage({
|
|
|
|
id: 'users-permissions.Policies.header.hint',
|
|
|
|
defaultMessage:
|
|
|
|
"Select the application's actions or the plugin's actions and click on the cog icon to display the bound route",
|
|
|
|
})}
|
|
|
|
</Text>
|
|
|
|
</Box>
|
|
|
|
)}
|
2021-09-13 12:24:11 +02:00
|
|
|
</GridItem>
|
2020-08-06 16:27:51 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Policies;
|