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 { getTrad } from '../../utils';
|
|
|
|
import { useUsersPermissions } from '../../contexts/UsersPermissionsContext';
|
|
|
|
import BoundRoute from '../BoundRoute';
|
|
|
|
import SizedInput from '../SizedInput';
|
|
|
|
import { Header, Wrapper, Sticky } from './Components';
|
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 baseTitle = 'users-permissions.Policies.header';
|
|
|
|
const title = !selectedAction ? 'hint' : 'title';
|
|
|
|
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 ? (
|
|
|
|
<Stack size={4} paddingTop={6}>
|
|
|
|
<Select
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
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>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</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
|
|
|
);
|
2021-09-21 18:47:18 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Wrapper className="col-md-5">
|
|
|
|
<Sticky className="container-fluid">
|
|
|
|
<div className="row">
|
|
|
|
<Header className="col-md-12">
|
|
|
|
<FormattedMessage id={`${baseTitle}.${title}`} />
|
|
|
|
</Header>
|
|
|
|
{selectedAction && (
|
|
|
|
<>
|
|
|
|
<SizedInput
|
|
|
|
type="select"
|
|
|
|
name={inputName}
|
|
|
|
onChange={onChange}
|
|
|
|
label={getTrad('Policies.InputSelect.label')}
|
|
|
|
options={policies}
|
|
|
|
value={value}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="row">
|
|
|
|
<Col size={{ xs: 12 }}>
|
|
|
|
{displayedRoutes.map((route, key) => (
|
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
|
|
|
<BoundRoute key={key} route={route} />
|
|
|
|
))}
|
|
|
|
</Col>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Sticky>
|
|
|
|
</Wrapper>
|
|
|
|
);
|
2020-08-06 16:27:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Policies;
|