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
28 changes: 14 additions & 14 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,21 +713,21 @@ export const canUser =
const permissions = getUserPermissionsFromAllowHeader(
response.headers?.get( 'allow' )
);
registry.batch( () => {
for ( const action of ALLOWED_RESOURCE_ACTIONS ) {
const key = getUserPermissionCacheKey( action, resource, id );

dispatch.receiveUserPermission( key, permissions[ action ] );

// Mark related action resolutions as finished.
if ( action !== requestedAction ) {
dispatch.finishResolution( 'canUser', [
action,
resource,
id,
] );
}
const receiveUserPermissionArgs = {};
const canUserResolutionsArgs = [];
for ( const action of ALLOWED_RESOURCE_ACTIONS ) {
receiveUserPermissionArgs[
getUserPermissionCacheKey( action, resource, id )
] = permissions[ action ];

// Mark related action resolutions as finished.
if ( action !== requestedAction ) {
canUserResolutionsArgs.push( [ action, resource, id ] );
}
}
registry.batch( () => {
dispatch.receiveUserPermissions( receiveUserPermissionArgs );
dispatch.finishResolutions( 'canUser', canUserResolutionsArgs );
} );
};

Expand Down
126 changes: 46 additions & 80 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,8 @@ describe( 'canUser', () => {
batch: ( callback ) => callback(),
};
dispatch = Object.assign( jest.fn(), {
receiveUserPermission: jest.fn(),
finishResolution: jest.fn(),
receiveUserPermissions: jest.fn(),
finishResolutions: jest.fn(),
} );
triggerFetch.mockReset();
} );
Expand All @@ -859,7 +859,7 @@ describe( 'canUser', () => {
parse: false,
} );

expect( dispatch.receiveUserPermission ).not.toHaveBeenCalled();
expect( dispatch.receiveUserPermissions ).not.toHaveBeenCalled();
} );

it( 'throws an error when an entity resource object is malformed', async () => {
Expand Down Expand Up @@ -888,9 +888,8 @@ describe( 'canUser', () => {
parse: false,
} );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/media',
false
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( { 'create/media': false } )
);
} );

Expand All @@ -911,9 +910,8 @@ describe( 'canUser', () => {
parse: false,
} );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/postType/attachment',
false
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( { 'create/postType/attachment': false } )
);
} );

Expand All @@ -933,9 +931,8 @@ describe( 'canUser', () => {
parse: false,
} );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/media',
true
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( { 'create/media': true } )
);
} );

Expand All @@ -956,9 +953,8 @@ describe( 'canUser', () => {
parse: false,
} );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/postType/attachment',
true
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( { 'create/postType/attachment': true } )
);
} );

Expand All @@ -979,9 +975,8 @@ describe( 'canUser', () => {
parse: false,
} );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/blocks/123',
true
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( { 'create/blocks/123': true } )
);
} );

Expand All @@ -1006,9 +1001,8 @@ describe( 'canUser', () => {
parse: false,
} );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/postType/wp_block/123',
true
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( { 'create/postType/wp_block/123': true } )
);
} );

Expand All @@ -1035,13 +1029,11 @@ describe( 'canUser', () => {

expect( triggerFetch ).toHaveBeenCalledTimes( 1 );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/blocks',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'read/blocks',
true
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( {
'create/blocks': true,
'read/blocks': true,
} )
);
} );

Expand Down Expand Up @@ -1076,13 +1068,11 @@ describe( 'canUser', () => {

expect( triggerFetch ).toHaveBeenCalledTimes( 1 );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/postType/wp_block',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'read/postType/wp_block',
true
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( {
'create/postType/wp_block': true,
'read/postType/wp_block': true,
} )
);
} );

Expand Down Expand Up @@ -1115,21 +1105,13 @@ describe( 'canUser', () => {
'blocks'
)( { dispatch, registry, resolveSelect } );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/blocks',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'read/blocks',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'update/blocks',
false
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'delete/blocks',
false
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( {
'create/blocks': true,
'read/blocks': true,
'update/blocks': false,
'delete/blocks': false,
} )
);
} );

Expand Down Expand Up @@ -1168,21 +1150,13 @@ describe( 'canUser', () => {

expect( triggerFetch ).toHaveBeenCalledTimes( 1 );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/blocks/123',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'read/blocks/123',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'update/blocks/123',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'delete/blocks/123',
true
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( {
'create/blocks/123': true,
'read/blocks/123': true,
'update/blocks/123': true,
'delete/blocks/123': true,
} )
);
} );

Expand Down Expand Up @@ -1221,21 +1195,13 @@ describe( 'canUser', () => {

expect( triggerFetch ).toHaveBeenCalledTimes( 1 );

expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'create/postType/wp_block/123',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'read/postType/wp_block/123',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'update/postType/wp_block/123',
true
);
expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith(
'delete/postType/wp_block/123',
true
expect( dispatch.receiveUserPermissions ).toHaveBeenCalledWith(
expect.objectContaining( {
'create/postType/wp_block/123': true,
'read/postType/wp_block/123': true,
'update/postType/wp_block/123': true,
'delete/postType/wp_block/123': true,
} )
);
} );
} );
Expand Down
Loading