Skip to content

Commit

Permalink
Add checks when resource is an entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 10, 2024
1 parent a74fd72 commit 19e01ea
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ _Parameters_

_Returns_

- `boolean | undefined`: Whether or not the user can perform the action, or `undefined` if the OPTIONS request is still being made.
- `boolean | undefined | null`: Whether or not the user can perform the action, or `undefined` if the OPTIONS request is still being made.

### canUserEditEntityRecord

Expand All @@ -42,7 +42,7 @@ _Parameters_

_Returns_

- `boolean | undefined`: Whether or not the user can edit, or `undefined` if the OPTIONS request is still being made.
- `boolean | undefined | null`: Whether or not the user can edit, or `undefined` if the OPTIONS request is still being made.

### getAuthors

Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ _Parameters_

_Returns_

- `boolean | undefined`: Whether or not the user can perform the action, or `undefined` if the OPTIONS request is still being made.
- `boolean | undefined | null`: Whether or not the user can perform the action, or `undefined` if the OPTIONS request is still being made.

### canUserEditEntityRecord

Expand All @@ -363,7 +363,7 @@ _Parameters_

_Returns_

- `boolean | undefined`: Whether or not the user can edit, or `undefined` if the OPTIONS request is still being made.
- `boolean | undefined | null`: Whether or not the user can edit, or `undefined` if the OPTIONS request is still being made.

### getAuthors

Expand Down
4 changes: 4 additions & 0 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ export const canUser =

let resourcePath = null;
if ( typeof resource === 'object' ) {
if ( ! resource.kind || ! resource.name ) {
return;
}

const configs = await dispatch(
getOrLoadEntitiesConfig( resource.kind, resource.name )
);
Expand Down
11 changes: 8 additions & 3 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1147,9 +1147,14 @@ export function canUser(
action: string,
resource: string | Record< string, any >,
id?: EntityRecordKey
): boolean | undefined {
): boolean | undefined | null {
const isEntity = typeof resource === 'object';
if ( isEntity && ( ! resource.kind || ! resource.name ) ) {
return null;
}

const key = (
typeof resource === 'object'
isEntity
? [ action, resource.kind, resource.name, resource.id ]
: [ action, resource, id ]
)
Expand Down Expand Up @@ -1179,7 +1184,7 @@ export function canUserEditEntityRecord(
kind: string,
name: string,
recordId: EntityRecordKey
): boolean | undefined {
): boolean | undefined | null {
return canUser( state, 'update', { kind, name, id: recordId } );
}

Expand Down
19 changes: 19 additions & 0 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,25 @@ describe( 'canUser', () => {
expect( dispatch.receiveUserPermission ).not.toHaveBeenCalled();
} );

it( 'does nothing when entity kind or name is missing', async () => {
triggerFetch.mockImplementation( () =>
Promise.reject( { status: 404 } )
);

await canUser( 'create', { kind: 'root', name: 'media' } )( {
dispatch,
registry,
} );
await canUser( 'create', { name: 'wp_block' } )( {
dispatch,
registry,
} );

expect( triggerFetch ).not.toHaveBeenCalledWith();

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

it( 'receives false when the user is not allowed to perform an action', async () => {
triggerFetch.mockImplementation( () => ( {
headers: new Map( [ [ 'allow', 'GET' ] ] ),
Expand Down
8 changes: 8 additions & 0 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,14 @@ describe( 'canUser', () => {
).toBe( undefined );
} );

it( 'returns null when entity kind or name is missing', () => {
const state = deepFreeze( {
userPermissions: {},
} );
expect( canUser( state, 'create', { name: 'media' } ) ).toBe( null );
expect( canUser( state, 'create', { kind: 'root' } ) ).toBe( null );
} );

it( 'returns whether an action can be performed', () => {
const state = deepFreeze( {
userPermissions: {
Expand Down

0 comments on commit 19e01ea

Please sign in to comment.