Skip to content

Commit

Permalink
Core data: Performance: fix receive user permissions (#64894)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Aug 30, 2024
1 parent 7ed54f4 commit 497f581
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
22 changes: 22 additions & 0 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,28 @@ export function receiveUserPermission( key, isAllowed ) {
};
}

/**
* Returns an action object used in signalling that the current user has
* permission to perform an action on a REST resource. Ignored from
* documentation as it's internal to the data store.
*
* @ignore
*
* @param {Object<string, boolean>} permissions An object where keys represent
* actions and REST resources, and
* values indicate whether the user
* is allowed to perform the
* action.
*
* @return {Object} Action object.
*/
export function receiveUserPermissions( permissions ) {
return {
type: 'RECEIVE_USER_PERMISSIONS',
permissions,
};
}

/**
* Returns an action object used in signalling that the autosaves for a
* post have been received.
Expand Down
5 changes: 5 additions & 0 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,11 @@ export function userPermissions( state = {}, action ) {
...state,
[ action.key ]: action.isAllowed,
};
case 'RECEIVE_USER_PERMISSIONS':
return {
...state,
...action.permissions,
};
}

return state;
Expand Down
11 changes: 7 additions & 4 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,24 +325,27 @@ export const getEntityRecords =
} ) );

const canUserResolutionsArgs = [];
const receiveUserPermissionArgs = {};
for ( const targetHint of targetHints ) {
for ( const action of ALLOWED_RESOURCE_ACTIONS ) {
canUserResolutionsArgs.push( [
action,
{ kind, name, id: targetHint.id },
] );

dispatch.receiveUserPermission(
receiveUserPermissionArgs[
getUserPermissionCacheKey( action, {
kind,
name,
id: targetHint.id,
} ),
targetHint.permissions[ action ]
);
} )
] = targetHint.permissions[ action ];
}
}

dispatch.receiveUserPermissions(
receiveUserPermissionArgs
);
dispatch.finishResolutions(
'getEntityRecord',
resolutionsArgs
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe( 'getEntityRecords', () => {
const finishResolutions = jest.fn();
const dispatch = Object.assign( jest.fn(), {
receiveEntityRecords: jest.fn(),
receiveUserPermission: jest.fn(),
receiveUserPermissions: jest.fn(),
__unstableAcquireStoreLock: jest.fn(),
__unstableReleaseStoreLock: jest.fn(),
finishResolutions,
Expand Down

0 comments on commit 497f581

Please sign in to comment.