Skip to content

Commit

Permalink
Fix: Include permission management on permanently delete, rename, and…
Browse files Browse the repository at this point in the history
… restore post actions.
  • Loading branch information
jorgefilipecosta committed Jun 21, 2024
1 parent 33bc0da commit 72b7f6e
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ const trashPostAction = {
},
};

function useTrashPostAction( postType ) {
function useCanUserEligibilityCheckPostType( postType, capability, action ) {
const registry = useRegistry();
const { resource, cachedCanUserResolvers } = useSelect(
( select ) => {
Expand All @@ -321,20 +321,28 @@ function useTrashPostAction( 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 )
);
},
} ),
// 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, cachedCanUserResolvers ]
);
}

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

Expand Down Expand Up @@ -428,6 +436,14 @@ const permanentlyDeletePostAction = {
},
};

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

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

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

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

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

const useDuplicatePostAction = ( postType ) => {
const { userCanCreatePost } = useSelect(
( select ) => {
Expand Down Expand Up @@ -1052,6 +1084,10 @@ export function usePostActions( { postType, onActionPerformed, context } ) {

const duplicatePostAction = useDuplicatePostAction( postType );
const trashPostActionForPostType = useTrashPostAction( postType );
const permanentlyDeletePostActionForPostType =
usePermanentlyDeletePostAction( postType );
const renamePostActionForPostType = useRenamePostAction( postType );
const restorePostActionForPostType = useRestorePostAction( postType );
const isTemplateOrTemplatePart = [
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
Expand All @@ -1075,13 +1111,16 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
: false,
isTemplateOrTemplatePart && duplicateTemplatePartAction,
isPattern && 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
Expand Down Expand Up @@ -1146,6 +1185,9 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
postTypeObject?.viewable,
duplicatePostAction,
trashPostActionForPostType,
restorePostActionForPostType,
renamePostActionForPostType,
permanentlyDeletePostActionForPostType,
onActionPerformed,
isLoaded,
supportsRevisions,
Expand Down

0 comments on commit 72b7f6e

Please sign in to comment.