Init renaming hook useUser

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2021-05-24 14:46:22 +02:00
parent ac0bea0102
commit fb0c6e4ef2
28 changed files with 608 additions and 29 deletions

View File

@ -0,0 +1,52 @@
{
"routes": [
{
"method": "GET",
"path": "/taaaaas",
"handler": "taaaaa.find",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/taaaaas/count",
"handler": "taaaaa.count",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/taaaaas/:id",
"handler": "taaaaa.findOne",
"config": {
"policies": []
}
},
{
"method": "POST",
"path": "/taaaaas",
"handler": "taaaaa.create",
"config": {
"policies": []
}
},
{
"method": "PUT",
"path": "/taaaaas/:id",
"handler": "taaaaa.update",
"config": {
"policies": []
}
},
{
"method": "DELETE",
"path": "/taaaaas/:id",
"handler": "taaaaa.delete",
"config": {
"policies": []
}
}
]
}

View File

@ -0,0 +1,8 @@
'use strict';
/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-controllers)
* to customize this controller
*/
module.exports = {};

View File

@ -0,0 +1,8 @@
'use strict';
/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#lifecycle-hooks)
* to customize this model
*/
module.exports = {};

View File

@ -0,0 +1,18 @@
{
"kind": "collectionType",
"collectionName": "taaaaas",
"info": {
"name": "taaaaa"
},
"options": {
"increments": true,
"timestamps": true,
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
"eee": {
"type": "string"
}
}
}

View File

@ -0,0 +1,8 @@
'use strict';
/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services)
* to customize this service
*/
module.exports = {};

View File

@ -0,0 +1,52 @@
{
"routes": [
{
"method": "GET",
"path": "/tatas",
"handler": "tata.find",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/tatas/count",
"handler": "tata.count",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/tatas/:id",
"handler": "tata.findOne",
"config": {
"policies": []
}
},
{
"method": "POST",
"path": "/tatas",
"handler": "tata.create",
"config": {
"policies": []
}
},
{
"method": "PUT",
"path": "/tatas/:id",
"handler": "tata.update",
"config": {
"policies": []
}
},
{
"method": "DELETE",
"path": "/tatas/:id",
"handler": "tata.delete",
"config": {
"policies": []
}
}
]
}

View File

@ -0,0 +1,8 @@
'use strict';
/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-controllers)
* to customize this controller
*/
module.exports = {};

View File

@ -0,0 +1,8 @@
'use strict';
/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#lifecycle-hooks)
* to customize this model
*/
module.exports = {};

View File

@ -0,0 +1,22 @@
{
"kind": "collectionType",
"collectionName": "tatas",
"info": {
"name": "tata",
"description": ""
},
"options": {
"increments": true,
"timestamps": true,
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
"eee": {
"type": "string"
},
"eeee": {
"type": "string"
}
}
}

View File

@ -0,0 +1,8 @@
'use strict';
/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services)
* to customize this service
*/
module.exports = {};

View File

@ -0,0 +1,14 @@
'use strict';
const adminPermissions = require('./permissions/admin-permissions');
const cmPermissions = require('./permissions/content-manager-permissions');
const ctbPermissions = require('./permissions/content-type-builder-permissions');
const permissions = [...adminPermissions, ...cmPermissions, ...ctbPermissions];
module.exports = {
adminPermissions,
cmPermissions,
ctbPermissions,
permissions,
};

View File

@ -0,0 +1,125 @@
'use strict';
const adminPermissions = [
{
id: 169,
action: 'admin::provider-login.read',
subject: null,
properties: {},
conditions: [],
},
{
id: 170,
action: 'admin::provider-login.update',
subject: null,
properties: {},
conditions: [],
},
{
id: 171,
action: 'admin::marketplace.read',
subject: null,
properties: {},
conditions: [],
},
{
id: 172,
action: 'admin::marketplace.plugins.install',
subject: null,
properties: {},
conditions: [],
},
{
id: 173,
action: 'admin::marketplace.plugins.uninstall',
subject: null,
properties: {},
conditions: [],
},
{
id: 174,
action: 'admin::webhooks.create',
subject: null,
properties: {},
conditions: [],
},
{
id: 175,
action: 'admin::webhooks.read',
subject: null,
properties: {},
conditions: [],
},
{
id: 176,
action: 'admin::webhooks.update',
subject: null,
properties: {},
conditions: [],
},
{
id: 177,
action: 'admin::webhooks.delete',
subject: null,
properties: {},
conditions: [],
},
{
id: 178,
action: 'admin::users.create',
subject: null,
properties: {},
conditions: [],
},
{
id: 179,
action: 'admin::users.read',
subject: null,
properties: {},
conditions: [],
},
{
id: 180,
action: 'admin::users.update',
subject: null,
properties: {},
conditions: [],
},
{
id: 181,
action: 'admin::users.delete',
subject: null,
properties: {},
conditions: [],
},
{
id: 182,
action: 'admin::roles.create',
subject: null,
properties: {},
conditions: [],
},
{
id: 183,
action: 'admin::roles.read',
subject: null,
properties: {},
conditions: [],
},
{
id: 184,
action: 'admin::roles.update',
subject: null,
properties: {},
conditions: [],
},
{
id: 185,
action: 'admin::roles.delete',
subject: null,
properties: {},
conditions: [],
},
];
module.exports = adminPermissions;

View File

@ -0,0 +1,63 @@
'use strict';
const cmPermissions = [
{
id: 2817,
action: 'plugins::content-manager.single-types.configure-view',
subject: null,
properties: {},
conditions: [],
},
{
id: 2818,
action: 'plugins::content-manager.collection-types.configure-view',
subject: null,
properties: {},
conditions: [],
},
{
id: 2819,
action: 'plugins::content-manager.components.configure-layout',
subject: null,
properties: {},
conditions: [],
},
{
action: 'plugins::content-manager.explorer.create',
subject: 'foo',
properties: {
fields: ['f1'],
},
conditions: [],
},
{
action: 'plugins::content-manager.explorer.create',
subject: 'foo',
properties: {
fields: ['f2'],
},
conditions: [],
},
{
action: 'plugins::content-manager.explorer.read',
subject: 'foo',
properties: {
fields: ['f1'],
},
conditions: [],
},
{
action: 'plugins::content-manager.explorer.delete',
subject: 'bar',
},
{
action: 'plugins::content-manager.explorer.update',
subject: 'bar',
properties: {
fields: ['f1'],
},
conditions: [],
},
];
module.exports = cmPermissions;

View File

@ -0,0 +1,42 @@
'use strict';
const ctbPermissions = [
{
id: 2820,
action: 'plugins::content-type-builder.read',
subject: null,
properties: {},
conditions: [],
},
{ id: 2821, action: 'plugins::upload.read', subject: null, properties: {}, conditions: [] },
{
id: 2822,
action: 'plugins::upload.assets.create',
subject: null,
properties: {},
conditions: [],
},
{
id: 2823,
action: 'plugins::upload.assets.update',
subject: null,
properties: {},
conditions: [],
},
{
id: 2824,
action: 'plugins::upload.assets.download',
subject: null,
properties: {},
conditions: [],
},
{
id: 2825,
action: 'plugins::upload.assets.copy-link',
subject: null,
properties: {},
conditions: [],
},
];
module.exports = ctbPermissions;

View File

@ -0,0 +1,7 @@
'use strict';
const fixtures = require('./fixtures');
module.exports = {
fixtures,
};

View File

@ -0,0 +1,12 @@
{
"name": "@strapi/admin-test-utils",
"version": "1.0.0",
"private": true,
"description": "Test utilities for the Strapi administration panel",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Strapi Team",
"license": "MIT"
}

View File

@ -1,5 +1,5 @@
import { useEffect, useRef } from 'react';
import { useUser, useNotification } from '@strapi/helper-plugin';
import { useNotification, useRBACProvider } from '@strapi/helper-plugin';
import { useSelector, useDispatch } from 'react-redux';
import getCtOrStLinks from './utils/getCtOrStLinks';
@ -14,7 +14,8 @@ const useMenuSections = (plugins, shouldUpdateStrapi) => {
const toggleNotification = useNotification();
const state = useSelector(selectMenuLinks);
const dispatch = useDispatch();
const { userPermissions } = useUser();
const { allPermissions } = useRBACProvider();
const { menu: settingsMenu } = useSettingsMenu(true);
// We are using a ref because we don't want our effect to have this in its dependencies array
const generalSectionLinksRef = useRef(state.generalSectionLinks);
@ -29,7 +30,7 @@ const useMenuSections = (plugins, shouldUpdateStrapi) => {
const toggleLoading = () => dispatch(toggleIsLoading());
const resolvePermissions = async (permissions = userPermissions) => {
const resolvePermissions = async (permissions = allPermissions) => {
const pluginsSectionLinks = toPluginLinks(pluginsRef.current);
const { authorizedCtLinks, authorizedStLinks, contentTypes } = await getCtOrStLinks(
permissions,
@ -56,8 +57,8 @@ const useMenuSections = (plugins, shouldUpdateStrapi) => {
const resolvePermissionsRef = useRef(resolvePermissions);
useEffect(() => {
resolvePermissionsRef.current(userPermissions);
}, [userPermissions, dispatch]);
resolvePermissionsRef.current(allPermissions);
}, [allPermissions, dispatch]);
return { state, generateMenu: resolvePermissionsRef.current, toggleLoading };
};

View File

@ -1,16 +1,13 @@
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { LoadingIndicatorPage } from '@strapi/helper-plugin';
import { LoadingIndicatorPage, RBACProviderContext } from '@strapi/helper-plugin';
import PropTypes from 'prop-types';
import { resetStore, setPermissions } from './actions';
// TODO
const RBACProvider = ({
children,
permissions,
// refetchPermissions
}) => {
const RBACProvider = ({ children, permissions, refetchPermissions }) => {
const { allPermissions } = useSelector(state => state.rbacProvider);
const dispatch = useDispatch();
useEffect(() => {
@ -25,7 +22,11 @@ const RBACProvider = ({
return <LoadingIndicatorPage />;
}
return children;
return (
<RBACProviderContext.Provider value={{ allPermissions, refetchPermissions }}>
{children}
</RBACProviderContext.Provider>
);
};
RBACProvider.propTypes = {

View File

@ -13,9 +13,7 @@ import { RESET_STORE, SET_PERMISSIONS } from './constants';
const initialState = {
allPermissions: null,
adminPermissions: {},
collectionTypesRelatedPermissions: {},
pluginsPermissions: {},
};
const reducer = (state = initialState, action) =>

View File

@ -0,0 +1,96 @@
import { permissions } from '../../../../../../../admin-test-utils/lib/fixtures';
import { setPermissions, resetStore } from '../actions';
import rbacProviderReducer, { initialState } from '../reducer';
describe('rbacProviderReducer', () => {
let state;
beforeEach(() => {
state = {
allPermissions: null,
collectionTypesRelatedPermissions: {},
};
});
it('returns the initial state', () => {
const expected = state;
expect(rbacProviderReducer(undefined, {})).toEqual(expected);
});
describe('resetStore', () => {
it('should reset the state to its initial value', () => {
state.allPermissions = true;
state.collectionTypesRelatedPermissions = true;
expect(rbacProviderReducer(state, resetStore())).toEqual(initialState);
});
});
describe('setPermissions', () => {
it('should set the allPermissions value correctly', () => {
const permissions = [{ action: 'test', subject: null }];
const expected = { ...state, allPermissions: permissions };
expect(rbacProviderReducer(state, setPermissions(permissions))).toEqual(expected);
});
it('should set the collectionTypesRelatedPermissions correctly', () => {
const expected = {
foo: {
'plugins::content-manager.explorer.create': [
{
action: 'plugins::content-manager.explorer.create',
subject: 'foo',
properties: {
fields: ['f1'],
},
conditions: [],
},
{
action: 'plugins::content-manager.explorer.create',
subject: 'foo',
properties: {
fields: ['f2'],
},
conditions: [],
},
],
'plugins::content-manager.explorer.read': [
{
action: 'plugins::content-manager.explorer.read',
subject: 'foo',
properties: {
fields: ['f1'],
},
conditions: [],
},
],
},
bar: {
'plugins::content-manager.explorer.delete': [
{
action: 'plugins::content-manager.explorer.delete',
subject: 'bar',
},
],
'plugins::content-manager.explorer.update': [
{
action: 'plugins::content-manager.explorer.update',
subject: 'bar',
properties: {
fields: ['f1'],
},
conditions: [],
},
],
},
};
expect(
rbacProviderReducer(state, setPermissions(permissions)).collectionTypesRelatedPermissions
).toEqual(expected);
});
});
});

View File

@ -8,9 +8,9 @@ import {
PopUpWarning,
useNotification,
useStrapiApp,
useUser,
useAutoReloadOverlayBlocker,
useAppInfos,
useRBACProvider,
} from '@strapi/helper-plugin';
import { useIntl } from 'react-intl';
import { useHistory, useLocation, useRouteMatch, Redirect } from 'react-router-dom';
@ -77,7 +77,7 @@ const DataManagerProvider = ({
const { autoReload } = useAppInfos();
const { formatMessage } = useIntl();
const { emitEvent } = useGlobalContext();
const { fetchUserPermissions } = useUser();
const { refetchPermissions } = useRBACProvider();
const { pathname } = useLocation();
const { push } = useHistory();
@ -518,7 +518,7 @@ const DataManagerProvider = ({
};
const updatePermissions = async () => {
await fetchUserPermissions();
await refetchPermissions();
};
const updateSchema = (data, schemaType, componentUID) => {

View File

@ -2,14 +2,15 @@ import React, { useEffect, useRef, useState } from 'react';
import { Redirect } from 'react-router-dom';
import PropTypes from 'prop-types';
import useNotification from '../../hooks/useNotification';
import useUser from '../../hooks/useUser';
import hasPermissions from '../../utils/hasPermissions';
import LoadingIndicatorPage from '../LoadingIndicatorPage';
import useRBACProvider from '../../hooks/useRBACProvider';
const CheckPagePermissions = ({ permissions, children }) => {
const abortController = new AbortController();
const { signal } = abortController;
const { userPermissions } = useUser();
const { allPermissions } = useRBACProvider();
const toggleNotification = useNotification();
const [state, setState] = useState({ isLoading: true, canAccess: false });
@ -20,7 +21,7 @@ const CheckPagePermissions = ({ permissions, children }) => {
try {
setState({ isLoading: true, canAccess: false });
const canAccess = await hasPermissions(userPermissions, permissions, signal);
const canAccess = await hasPermissions(allPermissions, permissions, signal);
if (isMounted.current) {
setState({ isLoading: false, canAccess });

View File

@ -1,14 +1,15 @@
import { useEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import useNotification from '../../hooks/useNotification';
import useUser from '../../hooks/useUser';
import hasPermissions from '../../utils/hasPermissions';
import useRBACProvider from '../../hooks/useRBACProvider';
// NOTE: this component is very similar to the CheckPagePermissions
// except that it does not handle redirections nor loading state
const CheckPermissions = ({ permissions, children }) => {
const { userPermissions } = useUser();
const { allPermissions } = useRBACProvider();
const toggleNotification = useNotification();
const [state, setState] = useState({ isLoading: true, canAccess: false });
const isMounted = useRef(true);
@ -20,7 +21,7 @@ const CheckPermissions = ({ permissions, children }) => {
try {
setState({ isLoading: true, canAccess: false });
const canAccess = await hasPermissions(userPermissions, permissions, signal);
const canAccess = await hasPermissions(allPermissions, permissions, signal);
if (isMounted.current) {
setState({ isLoading: false, canAccess });

View File

@ -0,0 +1,12 @@
/**
*
* RBACProviderContext
*
*
*/
import { createContext } from 'react';
const RBACProviderContext = createContext();
export default RBACProviderContext;

View File

@ -0,0 +1,12 @@
/**
*
* useRBACProvider
*
*/
import { useContext } from 'react';
import RBACProviderContext from '../../contexts/RBACProviderContext';
const useRBACProvider = () => useContext(RBACProviderContext);
export default useRBACProvider;

View File

@ -1,10 +1,10 @@
import { useCallback, useEffect, useMemo, useReducer, useRef } from 'react';
import hasPermissions from '../../utils/hasPermissions';
import useUser from '../useUser';
import generateResultsObject from './utils/generateResultsObject';
import reducer from './reducer';
import init from './init';
import useRBACProvider from '../useRBACProvider';
const useUserPermissions = (pluginPermissions, permissions) => {
const abortController = new AbortController();
@ -14,8 +14,8 @@ const useUserPermissions = (pluginPermissions, permissions) => {
const permissionNames = useMemo(() => {
return Object.keys(pluginPermissions);
}, [pluginPermissions]);
const { userPermissions } = useUser();
const currentUserPermissions = permissions || userPermissions;
const { allPermissions } = useRBACProvider();
const currentUserPermissions = permissions || allPermissions;
const [state, dispatch] = useReducer(reducer, {}, () => init(permissionNames));
const checkPermissionsRef = useRef();
const generateArrayOfPromisesRef = useRef();

View File

@ -113,6 +113,7 @@ export { default as OverlayBlockerContext } from './contexts/OverlayBlockerConte
export { GlobalContext, GlobalContextProvider, useGlobalContext } from './contexts/GlobalContext';
export { default as UserContext } from './contexts/UserContext';
export { default as ContentManagerEditViewDataManagerContext } from './contexts/ContentManagerEditViewDataManagerContext';
export { default as RBACProviderContext } from './contexts/RBACProviderContext';
// Hooks
export { default as useAppInfos } from './hooks/useAppInfos';
@ -126,6 +127,7 @@ export { default as useUserPermissions } from './hooks/useUserPermissions';
export { default as useQueryParams } from './hooks/useQueryParams';
export { default as useOverlayBlocker } from './hooks/useOverlayBlocker';
export { default as useAutoReloadOverlayBlocker } from './hooks/useAutoReloadOverlayBlocker';
export { default as useRBACProvider } from './hooks/useRBACProvider';
// Providers
export { default as LibraryProvider } from './providers/LibraryProvider';

View File

@ -1,6 +1,6 @@
import React, { useRef } from 'react';
import PropTypes from 'prop-types';
import { Modal, ModalFooter, TabPanel, useUser } from '@strapi/helper-plugin';
import { Modal, ModalFooter, TabPanel, useRBACProvider } from '@strapi/helper-plugin';
import { useIntl } from 'react-intl';
import { Button } from '@buffetjs/core';
import { Formik } from 'formik';
@ -16,8 +16,8 @@ const ModalCreate = ({ alreadyUsedLocales, onClose, isOpened }) => {
const { defaultLocales, isLoading } = useDefaultLocales();
const { isAdding, addLocale } = useAddLocale();
const { formatMessage } = useIntl();
const { refetchPermissions } = useRBACProvider();
const { fetchUserPermissions } = useUser();
const shouldUpdatePermissions = useRef(false);
if (isLoading) {
@ -32,7 +32,7 @@ const ModalCreate = ({ alreadyUsedLocales, onClose, isOpened }) => {
const handleClosed = async () => {
if (shouldUpdatePermissions.current) {
await fetchUserPermissions();
await refetchPermissions();
}
shouldUpdatePermissions.current = true;