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

Site Editor: Add user capability check for the Export feature #69107

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,12 @@ export const canUser =
resourcePath =
entityConfig.baseURL + ( resource.id ? '/' + resource.id : '' );
} else {
resourcePath = `/wp/v2/${ resource }` + ( id ? '/' + id : '' );
// Allows checking permissions for non-default (`/wp/v2`) namespaces
// such as `/wp-block-editor/v1`.
const isCustomNamespace = !! resource?.startsWith( '/' );
resourcePath =
( isCustomNamespace ? resource : `/wp/v2/${ resource }` ) +
( id ? '/' + id : '' );
Comment on lines +520 to +525
Copy link
Member Author

Choose a reason for hiding this comment

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

This was resolved mainly by entity resource object support, but a couple of REST APIs that use custom namespaces remain in the core. While not ideal, using an entity registry for the export endpoint doesn't make sense.

@youknowriad, @tyxla, @jsnajdr I would love to hear your thoughts on this. Should I extract it into a separate PR?

}

let response;
Expand Down
22 changes: 22 additions & 0 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,28 @@ describe( 'canUser', () => {
true
);
} );

it( 'allows checking permissions for non-default namespaces', async () => {
triggerFetch.mockImplementation( () => ( {
headers: new Map( [ [ 'allow', 'GET' ] ] ),
} ) );

await canUser(
'read',
'/wp-block-editor/v1/export'
)( { dispatch, registry, resolveSelect } );

expect( triggerFetch ).toHaveBeenCalledWith( {
path: '/wp-block-editor/v1/export',
method: 'OPTIONS',
parse: false,
} );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'read//wp-block-editor/v1/export',
true
);
} );
} );

describe( 'getAutosaves', () => {
Expand Down
8 changes: 1 addition & 7 deletions packages/edit-site/src/components/more-menu/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { privateApis as editorPrivateApis } from '@wordpress/editor';

/**
Expand All @@ -15,14 +13,10 @@ import { unlock } from '../../lock-unlock';
const { ToolsMoreMenuGroup, PreferencesModal } = unlock( editorPrivateApis );

export default function MoreMenu() {
const isBlockBasedTheme = useSelect( ( select ) => {
return select( coreStore ).getCurrentTheme().is_block_theme;
}, [] );

return (
<>
<ToolsMoreMenuGroup>
{ isBlockBasedTheme && <SiteExport /> }
<SiteExport />
<WelcomeGuideMenuItem />
</ToolsMoreMenuGroup>
<PreferencesModal />
Expand Down
14 changes: 13 additions & 1 deletion packages/edit-site/src/components/more-menu/site-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ import { __, _x } from '@wordpress/i18n';
import { MenuItem } from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { download } from '@wordpress/icons';
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { downloadBlob } from '@wordpress/blob';
import { store as noticesStore } from '@wordpress/notices';

export default function SiteExport() {
const { isBlockBasedTheme, canExport } = useSelect( ( select ) => {
const { canUser, getCurrentTheme } = select( coreStore );
return {
isBlockBasedTheme: getCurrentTheme().is_block_theme,
canExport: canUser( 'read', '/wp-block-editor/v1/export' ) ?? false,
};
}, [] );
const { createErrorNotice } = useDispatch( noticesStore );

if ( ! isBlockBasedTheme || ! canExport ) {
return null;
}

async function handleExport() {
try {
const response = await apiFetch( {
Expand Down
Loading