From 29239b9d36ea26961ee86fa2d482ddf34a946bea Mon Sep 17 00:00:00 2001 From: wesleybl Date: Mon, 26 Aug 2024 16:27:17 -0300 Subject: [PATCH 1/7] Passes the `user` to the `restricted` function of the block settings The restricted function is called inside a useMemo. So we can't call useSelector on it. To work around this, we get the user before useMemo and pass it to the restricted function. --- packages/volto-slate/news/6264.feature | 1 + packages/volto-slate/src/blocks/Text/SlashMenu.jsx | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 packages/volto-slate/news/6264.feature diff --git a/packages/volto-slate/news/6264.feature b/packages/volto-slate/news/6264.feature new file mode 100644 index 0000000000..fd95720125 --- /dev/null +++ b/packages/volto-slate/news/6264.feature @@ -0,0 +1 @@ +Passes the `user` to the `restricted` function of the block settings. @wesleybl diff --git a/packages/volto-slate/src/blocks/Text/SlashMenu.jsx b/packages/volto-slate/src/blocks/Text/SlashMenu.jsx index 6fd5ed2f30..870836ed41 100644 --- a/packages/volto-slate/src/blocks/Text/SlashMenu.jsx +++ b/packages/volto-slate/src/blocks/Text/SlashMenu.jsx @@ -4,6 +4,7 @@ import { filter, isEmpty } from 'lodash'; import { Menu } from 'semantic-ui-react'; import { useIntl, FormattedMessage } from 'react-intl'; import { Icon } from '@plone/volto/components'; +import { useSelector } from 'react-redux'; const emptySlateBlock = () => ({ value: [ @@ -108,6 +109,8 @@ const PersistentSlashMenu = ({ editor }) => { } = props; const disableNewBlocks = data?.disableNewBlocks || detached; + const user = useSelector((state) => state.users?.user); + const [slashMenuSelected, setSlashMenuSelected] = React.useState(0); const hasAllowedBlocks = !isEmpty(allowedBlocks); @@ -122,7 +125,7 @@ const PersistentSlashMenu = ({ editor }) => { hasAllowedBlocks ? allowedBlocks.includes(item.id) : typeof item.restricted === 'function' - ? !item.restricted({ properties, block: item }) + ? !item.restricted({ properties, block: item, user: user }) : !item.restricted, ) .filter((block) => Boolean(block.title && block.id)) @@ -152,6 +155,7 @@ const PersistentSlashMenu = ({ editor }) => { properties, slashCommand, hasAllowedBlocks, + user, ], ); From 34364ac908971cac56c211dc20713c5eb59ba578 Mon Sep 17 00:00:00 2001 From: wesleybl Date: Tue, 27 Aug 2024 10:03:16 -0300 Subject: [PATCH 2/7] Fix news --- packages/volto-slate/news/6264.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/volto-slate/news/6264.feature b/packages/volto-slate/news/6264.feature index fd95720125..19ad619d3a 100644 --- a/packages/volto-slate/news/6264.feature +++ b/packages/volto-slate/news/6264.feature @@ -1 +1 @@ -Passes the `user` to the `restricted` function of the block settings. @wesleybl +Pass the `user` to the `restricted` function of the block settings. @wesleybl From caf546386d1bd78f6253dbe72757d7392f6a1811 Mon Sep 17 00:00:00 2001 From: wesleybl Date: Thu, 29 Aug 2024 09:53:24 -0300 Subject: [PATCH 3/7] Document `user` parameter in `restricted` function --- docs/source/development/how-to-restrict-blocks.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/source/development/how-to-restrict-blocks.md b/docs/source/development/how-to-restrict-blocks.md index 15fee224ed..0b2d83e79f 100644 --- a/docs/source/development/how-to-restrict-blocks.md +++ b/docs/source/development/how-to-restrict-blocks.md @@ -22,12 +22,13 @@ The function has this signature: block: BlockConfigBase; navRoot: Content; contentType: string; + user: Object }) => boolean; } ``` Where `properties` is the current object data and `block` is the block being evaluated in `BlockChooser`. -`navRoot` is the nearest navigation root object and `contentType` is the current content type. +`navRoot` is the nearest navigation root object and `contentType` is the current content type. `user` is an object that represents the currently authenticated user. In the following configuration example, you can restrict a block so that it cannot be added unless the content type is `News Item` or the content item is in a specific path in the content tree (`/folder`): From 462c65e98abfedd96029c65b3775fe6e56d1eda6 Mon Sep 17 00:00:00 2001 From: wesleybl Date: Thu, 29 Aug 2024 09:55:26 -0300 Subject: [PATCH 4/7] Update news --- packages/volto-slate/news/6264.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/volto-slate/news/6264.feature b/packages/volto-slate/news/6264.feature index 19ad619d3a..6d6fcda450 100644 --- a/packages/volto-slate/news/6264.feature +++ b/packages/volto-slate/news/6264.feature @@ -1 +1 @@ -Pass the `user` to the `restricted` function of the block settings. @wesleybl +Pass the `user` object to the `restricted` function of the block settings. @wesleybl From f02742431911f5403dbcd8f460296c1d5dee48bb Mon Sep 17 00:00:00 2001 From: Steve Piercy Date: Thu, 29 Aug 2024 16:31:06 -0700 Subject: [PATCH 5/7] Apply suggestions from code review Improve flow by removing awkward phrasing. --- docs/source/development/how-to-restrict-blocks.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/source/development/how-to-restrict-blocks.md b/docs/source/development/how-to-restrict-blocks.md index 0b2d83e79f..ed274ee37b 100644 --- a/docs/source/development/how-to-restrict-blocks.md +++ b/docs/source/development/how-to-restrict-blocks.md @@ -27,8 +27,11 @@ The function has this signature: } ``` -Where `properties` is the current object data and `block` is the block being evaluated in `BlockChooser`. -`navRoot` is the nearest navigation root object and `contentType` is the current content type. `user` is an object that represents the currently authenticated user. +`properties` is the current object data. +`block` is the block being evaluated in `BlockChooser`. +`navRoot` is the nearest navigation root object. +`contentType` is the current content type. +`user` is an object that represents the currently authenticated user. In the following configuration example, you can restrict a block so that it cannot be added unless the content type is `News Item` or the content item is in a specific path in the content tree (`/folder`): From 7928c694e9778695979191ccafbeeb3712c58267 Mon Sep 17 00:00:00 2001 From: wesleybl Date: Thu, 5 Sep 2024 16:59:01 -0300 Subject: [PATCH 6/7] Pass `user`, `navRoot` and `contentType` to the `restricted` function of the block settings --- packages/volto-slate/news/6264.feature | 2 +- packages/volto-slate/src/blocks/Text/SlashMenu.jsx | 10 +++++++++- packages/volto/news/6264.feature | 1 + .../components/manage/BlockChooser/BlockChooser.jsx | 10 +++++++++- 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 packages/volto/news/6264.feature diff --git a/packages/volto-slate/news/6264.feature b/packages/volto-slate/news/6264.feature index 6d6fcda450..02fa752b0f 100644 --- a/packages/volto-slate/news/6264.feature +++ b/packages/volto-slate/news/6264.feature @@ -1 +1 @@ -Pass the `user` object to the `restricted` function of the block settings. @wesleybl +Pass `user`, `navRoot` and `contentType` objects to the `restricted` function of the block settings. @wesleybl diff --git a/packages/volto-slate/src/blocks/Text/SlashMenu.jsx b/packages/volto-slate/src/blocks/Text/SlashMenu.jsx index 870836ed41..c65c1faa34 100644 --- a/packages/volto-slate/src/blocks/Text/SlashMenu.jsx +++ b/packages/volto-slate/src/blocks/Text/SlashMenu.jsx @@ -106,6 +106,8 @@ const PersistentSlashMenu = ({ editor }) => { selected, allowedBlocks, detached, + navRoot, + contentType, } = props; const disableNewBlocks = data?.disableNewBlocks || detached; @@ -125,7 +127,13 @@ const PersistentSlashMenu = ({ editor }) => { hasAllowedBlocks ? allowedBlocks.includes(item.id) : typeof item.restricted === 'function' - ? !item.restricted({ properties, block: item, user: user }) + ? !item.restricted({ + properties, + block: item, + navRoot, + contentType, + user, + }) : !item.restricted, ) .filter((block) => Boolean(block.title && block.id)) diff --git a/packages/volto/news/6264.feature b/packages/volto/news/6264.feature new file mode 100644 index 0000000000..e8a31aaf20 --- /dev/null +++ b/packages/volto/news/6264.feature @@ -0,0 +1 @@ +Pass the `user`, `navRoot` and `contentType` objects to the `restricted` function of the block settings. @wesleybl diff --git a/packages/volto/src/components/manage/BlockChooser/BlockChooser.jsx b/packages/volto/src/components/manage/BlockChooser/BlockChooser.jsx index 69b3d87c38..80c5142e81 100644 --- a/packages/volto/src/components/manage/BlockChooser/BlockChooser.jsx +++ b/packages/volto/src/components/manage/BlockChooser/BlockChooser.jsx @@ -1,4 +1,5 @@ import React from 'react'; +import { useSelector } from 'react-redux'; import PropTypes from 'prop-types'; import { filter, map, groupBy, isEmpty } from 'lodash'; import { Accordion, Button } from 'semantic-ui-react'; @@ -35,6 +36,7 @@ const BlockChooser = ({ contentType, }) => { const intl = useIntl(); + const user = useSelector((state) => state.users?.user); const hasAllowedBlocks = !isEmpty(allowedBlocks); const filteredBlocksConfig = filter(blocksConfig, (item) => { @@ -57,7 +59,13 @@ const BlockChooser = ({ // depending on this function, given properties (current present blocks) and the // block being evaluated return typeof item.restricted === 'function' - ? !item.restricted({ properties, block: item, navRoot, contentType }) + ? !item.restricted({ + properties, + block: item, + navRoot, + contentType, + user, + }) : !item.restricted; } } From e96e6b5c56c68f89ec09882ded0ad45ce9ba8515 Mon Sep 17 00:00:00 2001 From: wesleybl Date: Thu, 5 Sep 2024 17:08:18 -0300 Subject: [PATCH 7/7] Fix dependency array in useMemo --- packages/volto-slate/src/blocks/Text/SlashMenu.jsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/volto-slate/src/blocks/Text/SlashMenu.jsx b/packages/volto-slate/src/blocks/Text/SlashMenu.jsx index c65c1faa34..2a26c39cfe 100644 --- a/packages/volto-slate/src/blocks/Text/SlashMenu.jsx +++ b/packages/volto-slate/src/blocks/Text/SlashMenu.jsx @@ -163,6 +163,8 @@ const PersistentSlashMenu = ({ editor }) => { properties, slashCommand, hasAllowedBlocks, + navRoot, + contentType, user, ], );