Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passes the user to the restricted function of the block settings #6271

Merged
merged 8 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/source/development/how-to-restrict-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ The function has this signature:
block: BlockConfigBase;
navRoot: Content;
contentType: string;
user: Object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wesleybl @davisagli The update in the docs are great but you forgot to update @plone/types :)

restricted:
| ((args: {
properties: Content;
block: BlockConfigBase; // TODO: This has to be extendable
navRoot: Content;
contentType: string;
}) => boolean)
| boolean;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take care:

  restricted:
    | ((args: {
        properties: Content;
        block: BlockConfigBase; // TODO: This has to be extendable
        navRoot: Content;
        contentType: string;
        user: User;
      }) => boolean)
    | boolean;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sneridagh Er, I haven't really bothered to learn much about typescript yet. I kind of assumed that some linter would yell at me if the types need to be updated. Is that not right? If not, how do I know when there are type declarations that need to be updated?

}) => 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.
`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`):

Expand Down
1 change: 1 addition & 0 deletions packages/volto-slate/news/6264.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pass the `user` object to the `restricted` function of the block settings. @wesleybl
6 changes: 5 additions & 1 deletion packages/volto-slate/src/blocks/Text/SlashMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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);
Expand All @@ -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 })
davisagli marked this conversation as resolved.
Show resolved Hide resolved
: !item.restricted,
)
.filter((block) => Boolean(block.title && block.id))
Expand Down Expand Up @@ -152,6 +155,7 @@ const PersistentSlashMenu = ({ editor }) => {
properties,
slashCommand,
hasAllowedBlocks,
user,
],
);

Expand Down
Loading