Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ function resolveRecords( registry, menus ) {
function resolveReadPermission( registry, allowed ) {
const dispatch = registry.dispatch( coreStore );
dispatch.receiveUserPermission( 'read/postType/wp_navigation', allowed );
dispatch.startResolution( 'canUser', [ 'read', BASE_ENTITY ] );
dispatch.finishResolution( 'canUser', [ 'read', BASE_ENTITY ] );
dispatch.startResolution( 'canUser', [
'read',
{ kind: 'postType', name: 'wp_navigation' },
] );
dispatch.finishResolution( 'canUser', [
'read',
{ kind: 'postType', name: 'wp_navigation' },
] );
}

function resolveReadRecordPermission( registry, ref, allowed ) {
Expand Down
57 changes: 57 additions & 0 deletions packages/core-data/src/hooks/test/use-resource-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,63 @@ describe( 'useResourcePermissions', () => {
);
} );

it( 'normalizes id-less entity resources before resolving permissions', async () => {
let data;
triggerFetch.mockImplementation( ( options ) => {
if ( options.path === '/wp/v2/types?context=view' ) {
return {
wp_navigation: {
name: 'Navigation Menus',
slug: 'wp_navigation',
rest_base: 'navigation',
rest_namespace: 'wp/v2',
},
};
}
if (
options.path === '/wp/v2/navigation' &&
options.method === 'OPTIONS'
) {
return {
headers: new Headers( { allow: 'GET, POST' } ),
};
}
throw new Error(
`Unexpected request: ${ JSON.stringify( options ) }`
);
} );

const TestComponent = () => {
data = useResourcePermissions( {
kind: 'postType',
name: 'wp_navigation',
id: undefined,
} );
return <div />;
};
render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
);

await waitFor( () =>
expect( data ).toEqual( {
status: 'SUCCESS',
isResolving: false,
hasResolved: true,
canCreate: true,
canRead: true,
} )
);

expect(
triggerFetch.mock.calls.filter(
( [ options ] ) => options.path === '/wp/v2/navigation'
)
).toHaveLength( 1 );
} );

it( 'retrieves the relevant permissions for an entity', async () => {
let data;
const TestComponent = () => {
Expand Down
12 changes: 5 additions & 7 deletions packages/core-data/src/hooks/use-resource-permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,13 @@ function useResourcePermissions< IdType = void >(
( resolve ) => {
const hasId = isEntity ? !! resource.id : !! id;
const { canUser } = resolve( coreStore );
const create = canUser(
'create',
isEntity
? { kind: resource.kind, name: resource.name }
: resource
);
const collectionResource = isEntity
? { kind: resource.kind, name: resource.name }
: resource;
const create = canUser( 'create', collectionResource );

if ( ! hasId ) {
const read = canUser( 'read', resource );
const read = canUser( 'read', collectionResource );

const isResolving = create.isResolving || read.isResolving;
const hasResolved = create.hasResolved && read.hasResolved;
Expand Down
Loading