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

Fix: Include permission management on permanently delete, rename, and restore. #62754

Merged
Changes from all 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
94 changes: 68 additions & 26 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
@@ -307,34 +307,29 @@ const trashPostAction = {
},
};

function useTrashPostAction( postType ) {
function useCanUserEligibilityCheckPostType( capability, resource, action ) {
const registry = useRegistry();
const { resource, cachedCanUserResolvers } = useSelect(
( select ) => {
const { getPostType, getCachedResolvers } = select( coreStore );
return {
resource: getPostType( postType )?.rest_base || '',
cachedCanUserResolvers: getCachedResolvers().canUser,
};
},
[ postType ]
);
return useMemo(
() => ( {
...trashPostAction,
...action,
isEligible( item ) {
return (
trashPostAction.isEligible( item ) &&
action.isEligible( item ) &&
registry
.select( coreStore )
.canUser( 'delete', resource, item.id )
.canUser( capability, resource, item.id )
Copy link
Member

Choose a reason for hiding this comment

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

I guess it doesn't really happen in the case, but the problem here is that isEligible will not be called if the user capability changes, right? So maybe all of these need to be precalculated/subscribed to at some point so the buttons re-render.

);
},
} ),
// We are making this use memo depend on cachedCanUserResolvers as a way to make the component using this hook re-render
// when user capabilities are resolved. This makes sure the isEligible function is re-evaluated.
// eslint-disable-next-line react-hooks/exhaustive-deps
[ registry, resource, cachedCanUserResolvers ]
[ action, registry, capability, resource ]
);
}

function useTrashPostAction( resource ) {
return useCanUserEligibilityCheckPostType(
'delete',
resource,
trashPostAction
);
}

@@ -428,6 +423,14 @@ const permanentlyDeletePostAction = {
},
};

function usePermanentlyDeletePostAction( resource ) {
return useCanUserEligibilityCheckPostType(
'delete',
resource,
permanentlyDeletePostAction
);
}

const restorePostAction = {
id: 'restore',
label: __( 'Restore' ),
@@ -535,6 +538,14 @@ const restorePostAction = {
},
};

function useRestorePostAction( resource ) {
return useCanUserEligibilityCheckPostType(
'update',
resource,
restorePostAction
);
}

const viewPostAction = {
id: 'view-post',
label: __( 'View' ),
@@ -694,6 +705,14 @@ const renamePostAction = {
},
};

function useRenamePostAction( resource ) {
return useCanUserEligibilityCheckPostType(
'update',
resource,
renamePostAction
);
}

const useDuplicatePostAction = ( postType ) => {
const { userCanCreatePost } = useSelect(
( select ) => {
@@ -1038,23 +1057,36 @@ export const duplicateTemplatePartAction = {
};

export function usePostActions( { postType, onActionPerformed, context } ) {
const { defaultActions, postTypeObject, userCanCreatePostType } = useSelect(
const {
defaultActions,
postTypeObject,
userCanCreatePostType,
resource,
cachedCanUserResolvers,
} = useSelect(
( select ) => {
const { getPostType, canUser } = select( coreStore );
const { getPostType, canUser, getCachedResolvers } =
select( coreStore );
const { getEntityActions } = unlock( select( editorStore ) );
const _postTypeObject = getPostType( postType );
const resource = _postTypeObject?.rest_base || '';
const _resource = _postTypeObject?.rest_base || '';
return {
postTypeObject: _postTypeObject,
defaultActions: getEntityActions( 'postType', postType ),
userCanCreatePostType: canUser( 'create', resource ),
userCanCreatePostType: canUser( 'create', _resource ),
resource: _resource,
cachedCanUserResolvers: getCachedResolvers()?.canUser,
};
},
[ postType ]
);

const duplicatePostAction = useDuplicatePostAction( postType );
const trashPostActionForPostType = useTrashPostAction( postType );
const trashPostActionForPostType = useTrashPostAction( resource );
const permanentlyDeletePostActionForPostType =
usePermanentlyDeletePostAction( resource );
const renamePostActionForPostType = useRenamePostAction( resource );
const restorePostActionForPostType = useRestorePostAction( resource );
const isTemplateOrTemplatePart = [
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
@@ -1080,13 +1112,16 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
userCanCreatePostType &&
duplicateTemplatePartAction,
isPattern && userCanCreatePostType && duplicatePatternAction,
supportsTitle && renamePostAction,
supportsTitle && renamePostActionForPostType,
isPattern && exportPatternAsJSONAction,
isTemplateOrTemplatePart ? resetTemplateAction : restorePostAction,
isTemplateOrTemplatePart
? resetTemplateAction
: restorePostActionForPostType,
isTemplateOrTemplatePart || isPattern
? deletePostAction
: trashPostActionForPostType,
! isTemplateOrTemplatePart && permanentlyDeletePostAction,
! isTemplateOrTemplatePart &&
permanentlyDeletePostActionForPostType,
...defaultActions,
].filter( Boolean );
// Filter actions based on provided context. If not provided
@@ -1144,6 +1179,9 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
}

return actions;
// We are making this use memo depend on cachedCanUserResolvers as a way to make the component using this hook re-render
// when user capabilities are resolved. This makes sure the isEligible functions of actions dependent on capabilities are re-evaluated.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
defaultActions,
userCanCreatePostType,
@@ -1152,10 +1190,14 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
postTypeObject?.viewable,
duplicatePostAction,
trashPostActionForPostType,
restorePostActionForPostType,
renamePostActionForPostType,
permanentlyDeletePostActionForPostType,
onActionPerformed,
isLoaded,
supportsRevisions,
supportsTitle,
context,
cachedCanUserResolvers,
] );
}

Unchanged files with check annotations Beta

.getByRole( 'document', {
name: 'Empty block; start writing or type forward slash to choose a block',
} )
.click();

Check failure on line 29 in test/e2e/specs/site-editor/pages.spec.js

GitHub Actions / Playwright - 5

[chromium] › site-editor/pages.spec.js:93:2 › Pages › create a new page

1) [chromium] › site-editor/pages.spec.js:93:2 › Pages › create a new page, edit template and toggle page template preview TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for frameLocator('[name="editor-canvas"]').getByRole('document', { name: 'Block: Content' }).getByRole('document', { name: 'Empty block; start writing or type forward slash to choose a block' }) 27 | name: 'Empty block; start writing or type forward slash to choose a block', 28 | } ) > 29 | .click(); | ^ 30 | 31 | // Insert into Page Content using default block. 32 | await editor.canvas at addPageContent (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/pages.spec.js:29:4) at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/pages.spec.js:99:9

Check failure on line 29 in test/e2e/specs/site-editor/pages.spec.js

GitHub Actions / Playwright - 5

[chromium] › site-editor/pages.spec.js:93:2 › Pages › create a new page

1) [chromium] › site-editor/pages.spec.js:93:2 › Pages › create a new page, edit template and toggle page template preview Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for frameLocator('[name="editor-canvas"]').getByRole('document', { name: 'Block: Content' }).getByRole('document', { name: 'Empty block; start writing or type forward slash to choose a block' }) 27 | name: 'Empty block; start writing or type forward slash to choose a block', 28 | } ) > 29 | .click(); | ^ 30 | 31 | // Insert into Page Content using default block. 32 | await editor.canvas at addPageContent (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/pages.spec.js:29:4) at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/pages.spec.js:99:9

Check failure on line 29 in test/e2e/specs/site-editor/pages.spec.js

GitHub Actions / Playwright - 5

[chromium] › site-editor/pages.spec.js:93:2 › Pages › create a new page

1) [chromium] › site-editor/pages.spec.js:93:2 › Pages › create a new page, edit template and toggle page template preview Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for frameLocator('[name="editor-canvas"]').getByRole('document', { name: 'Block: Content' }).getByRole('document', { name: 'Empty block; start writing or type forward slash to choose a block' }) 27 | name: 'Empty block; start writing or type forward slash to choose a block', 28 | } ) > 29 | .click(); | ^ 30 | 31 | // Insert into Page Content using default block. 32 | await editor.canvas at addPageContent (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/pages.spec.js:29:4) at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/pages.spec.js:99:9
// Insert into Page Content using default block.
await editor.canvas