mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 10:55:37 +00:00
Clean helper plugin
Signed-off-by: soupette <cyril@strapi.io>
This commit is contained in:
parent
cedd532e54
commit
2e17674688
@ -3,7 +3,7 @@ import { Redirect } from 'react-router-dom';
|
||||
import PropTypes from 'prop-types';
|
||||
import useNotification from '../../hooks/useNotification';
|
||||
import useRBACProvider from '../../hooks/useRBACProvider';
|
||||
import hasPermissions from '../../old/utils/hasPermissions';
|
||||
import hasPermissions from '../../utils/hasPermissions';
|
||||
import LoadingIndicatorPage from '../LoadingIndicatorPage';
|
||||
|
||||
const CheckPagePermissions = ({ permissions, children }) => {
|
||||
|
||||
@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import useNotification from '../../hooks/useNotification';
|
||||
|
||||
import hasPermissions from '../../old/utils/hasPermissions';
|
||||
import hasPermissions from '../../utils/hasPermissions';
|
||||
import useRBACProvider from '../../hooks/useRBACProvider';
|
||||
|
||||
// NOTE: this component is very similar to the CheckPagePermissions
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { useCallback, useEffect, useMemo, useReducer, useRef } from 'react';
|
||||
import hasPermissions from '../../old/utils/hasPermissions';
|
||||
import hasPermissions from '../../utils/hasPermissions';
|
||||
|
||||
import generateResultsObject from './utils/generateResultsObject';
|
||||
import reducer from './reducer';
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { getType, getOtherInfos } from './content-manager/utils/getAttributeInfos';
|
||||
|
||||
// Assets
|
||||
export { default as colors } from './old/assets/styles/colors';
|
||||
export { default as sizes } from './old/assets/styles/sizes';
|
||||
@ -134,18 +135,14 @@ export { default as NotificationsProvider } from './providers/NotificationsProvi
|
||||
export { default as StrapiAppProvider } from './providers/StrapiAppProvider';
|
||||
|
||||
// Utils
|
||||
export { default as auth } from './old/utils/auth';
|
||||
export { default as cleanData } from './old/utils/cleanData';
|
||||
export { default as difference } from './old/utils/difference';
|
||||
export { default as contentManagementUtilRemoveFieldsFromData } from './content-manager/utils/contentManagementUtilRemoveFieldsFromData';
|
||||
|
||||
export { default as dateFormats } from './old/utils/dateFormats';
|
||||
export { default as dateToUtcTime } from './old/utils/dateToUtcTime';
|
||||
export { default as formatComponentData } from './content-manager/utils/formatComponentData';
|
||||
export { default as hasPermissions } from './old/utils/hasPermissions';
|
||||
export { findMatchingPermissions } from './old/utils/hasPermissions';
|
||||
export { default as translatedErrors } from './old/utils/translatedErrors';
|
||||
|
||||
export { darken } from './old/utils/colors';
|
||||
export { default as getFileExtension } from './utils/getFileExtension/getFileExtension';
|
||||
|
||||
export { default as getFilterType } from './old/utils/getFilterType';
|
||||
export { default as getQueryParameters } from './old/utils/getQueryParameters';
|
||||
export { default as validateInput } from './old/utils/inputsValidations';
|
||||
@ -157,15 +154,6 @@ export { default as getYupInnerErrors } from './old/utils/getYupInnerErrors';
|
||||
export { default as generateFiltersFromSearch } from './old/utils/generateFiltersFromSearch';
|
||||
export { default as generateSearchFromFilters } from './old/utils/generateSearchFromFilters';
|
||||
export { default as generateSearchFromObject } from './old/utils/generateSearchFromObject';
|
||||
export { default as prefixFileUrlWithBackendUrl } from './utils/prefixFileUrlWithBackendUrl/prefixFileUrlWithBackendUrl';
|
||||
export { default as prefixPluginTranslations } from './old/utils/prefixPluginTranslations';
|
||||
|
||||
export { default as pxToRem } from './utils/pxToRem';
|
||||
export { default as to } from './utils/await-to-js';
|
||||
|
||||
// NEW UTILS
|
||||
export { default as setHexOpacity } from './utils/setHexOpacity';
|
||||
export { default as customEllipsis } from './utils/customEllipsis';
|
||||
|
||||
// SVGS
|
||||
export { default as LayoutIcon } from './old/svgs/Layout';
|
||||
@ -208,3 +196,18 @@ export { default as ContentManagerEditViewDataManagerContext } from './content-m
|
||||
export { default as useCMEditViewDataManager } from './content-manager/hooks/useCMEditViewDataManager';
|
||||
export { getType };
|
||||
export { getOtherInfos };
|
||||
|
||||
// Utils
|
||||
export { default as auth } from './utils/auth';
|
||||
export { default as hasPermissions } from './utils/hasPermissions';
|
||||
export { default as prefixFileUrlWithBackendUrl } from './utils/prefixFileUrlWithBackendUrl/prefixFileUrlWithBackendUrl';
|
||||
export { default as prefixPluginTranslations } from './utils/prefixPluginTranslations';
|
||||
export { default as pxToRem } from './utils/pxToRem';
|
||||
export { default as to } from './utils/await-to-js';
|
||||
export { default as setHexOpacity } from './utils/setHexOpacity';
|
||||
export { default as customEllipsis } from './utils/customEllipsis';
|
||||
export { default as translatedErrors } from './utils/translatedErrors';
|
||||
export { default as formatComponentData } from './content-manager/utils/formatComponentData';
|
||||
export { findMatchingPermissions } from './utils/hasPermissions';
|
||||
export { default as contentManagementUtilRemoveFieldsFromData } from './content-manager/utils/contentManagementUtilRemoveFieldsFromData';
|
||||
export { default as getFileExtension } from './utils/getFileExtension/getFileExtension';
|
||||
|
||||
@ -3,7 +3,7 @@ import { useIntl } from 'react-intl';
|
||||
import { Inputs } from '@buffetjs/custom';
|
||||
import PropTypes from 'prop-types';
|
||||
import { isObject } from 'lodash';
|
||||
import translatedErrors from '../../utils/translatedErrors';
|
||||
import translatedErrors from '../../../utils/translatedErrors';
|
||||
|
||||
const IntlInput = ({
|
||||
description,
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import 'whatwg-fetch';
|
||||
import _ from 'lodash';
|
||||
import auth from './auth';
|
||||
import auth from '../../utils/auth';
|
||||
|
||||
/**
|
||||
* Parses the JSON returned by a network request
|
||||
|
||||
@ -10,7 +10,7 @@ Async await wrapper for easy error handling
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
```js
|
||||
import { to } from '@strapi/helper-plugin';
|
||||
|
||||
const [err, response] = await to(promise);
|
||||
|
||||
@ -10,7 +10,7 @@ This hook is used to have a custom ellipsis for large url
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
```js
|
||||
import { customEllipsis } from '@strapi/helper-plugin';
|
||||
|
||||
const ContentBox = ({ title, subtitle, icon, iconBackground, endAction }) => {
|
||||
|
||||
@ -10,12 +10,12 @@ This hook is used to create URL aware of the backend. Practical to resolve asset
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
```js
|
||||
import { getFileExtension } from '@strapi/helper-plugin';
|
||||
|
||||
const Compo = () => {
|
||||
const ext = getFileExtension('.png')
|
||||
const ext = getFileExtension('.png');
|
||||
|
||||
return <div>{ext}</div>;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
<!--- hasPermissions.stories.mdx --->
|
||||
|
||||
import { Meta } from '@storybook/addon-docs';
|
||||
|
||||
<Meta title="utils/hasPermissions" />
|
||||
|
||||
# hasPermissions
|
||||
|
||||
This util is used to check if a logged in user has the right to perform an action.
|
||||
|
||||
> It is best to also import the useRBACProvider hook in order to retrieve all the permissions of a user.
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import { hasPermissions, useRBACProvider } from '@strapi/helper-plugin';
|
||||
|
||||
const Compo = () => {
|
||||
const { allPermissions } = useRBACProvider();
|
||||
const [{ isLoading, canAccess }, setState] = useState({ isLoading: true, canAccess: false });
|
||||
|
||||
useEffect(() => {
|
||||
const checkPermission = async () => {
|
||||
try {
|
||||
const canAccess = await hasPermissions(allPermissions, [
|
||||
{
|
||||
action: 'plugin::content-manager.components.configure-layout',
|
||||
subject: null,
|
||||
},
|
||||
]);
|
||||
|
||||
setState({ isLoading: false, canAccess });
|
||||
} catch (err) {
|
||||
setState({ isLoading: false });
|
||||
}
|
||||
};
|
||||
|
||||
checkPermission();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
console.log({ isLoading, canAccess });
|
||||
|
||||
return null;
|
||||
};
|
||||
```
|
||||
@ -1,5 +1,5 @@
|
||||
import { isEmpty, pickBy, transform } from 'lodash';
|
||||
import request from './request';
|
||||
import request from '../../old/utils/request';
|
||||
|
||||
const findMatchingPermissions = (userPermissions, permissions) => {
|
||||
return transform(
|
||||
@ -27,6 +27,12 @@ const formatPermissionsForRequest = permissions =>
|
||||
const shouldCheckPermissions = permissions =>
|
||||
!isEmpty(permissions) && permissions.every(perm => !isEmpty(perm.conditions));
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object[]} userPermissions array of the user's permissions
|
||||
* @param {Object[]} permissions array of permissions that we want to find from the user's permissions
|
||||
* @returns Boolean
|
||||
*/
|
||||
const hasPermissions = async (userPermissions, permissions, signal) => {
|
||||
if (!permissions || !permissions.length) {
|
||||
return true;
|
||||
@ -2,7 +2,7 @@ import hasPermissions, {
|
||||
findMatchingPermissions,
|
||||
formatPermissionsForRequest,
|
||||
shouldCheckPermissions,
|
||||
} from '../hasPermissions';
|
||||
} from '../index';
|
||||
import hasPermissionsTestData from './hasPermissionsTestData';
|
||||
|
||||
describe('STRAPI-HELPER_PLUGIN | utils ', () => {
|
||||
@ -6,16 +6,16 @@ import { Meta } from '@storybook/addon-docs';
|
||||
|
||||
# prefixFileUrlWithBackendUrl
|
||||
|
||||
This hook is used to create URL aware of the backend. Practical to resolve assets.
|
||||
This util is used to create URL aware of the backend. Practical to resolve assets.
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
```js
|
||||
import { prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin';
|
||||
|
||||
const Compo = () => {
|
||||
const url = prefixFileUrlWithBackendUrl('/somewhere-i-belong')
|
||||
const url = prefixFileUrlWithBackendUrl('/somewhere-i-belong');
|
||||
|
||||
return <div>{url}</div>;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
@ -10,7 +10,7 @@ Converts px to rem.
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
```js
|
||||
import { pxToRem } from '@strapi/helper-plugin';
|
||||
import { Box } from '@strapi/parts';
|
||||
import styled from 'styled-components'
|
||||
|
||||
@ -10,7 +10,7 @@ This hook is used in order to display a notification in the admin panel.
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
```js
|
||||
import { setHexOpacity } from '@strapi/helper-plugin';
|
||||
|
||||
const BoxCustom = styled.div`
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user