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

Core Data selector canUser does not handle entity records #43751

Closed
TimothyBJacobs opened this issue Aug 31, 2022 · 5 comments · Fixed by #63322
Closed

Core Data selector canUser does not handle entity records #43751

TimothyBJacobs opened this issue Aug 31, 2022 · 5 comments · Fixed by #63322
Assignees
Labels
[Package] Core data /packages/core-data REST API Interaction Related to REST API [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.

Comments

@TimothyBJacobs
Copy link
Member

Description

The @wordpress/core-data module provides a selector canUser( action, resource, id ) that can interrogate whether a user has permission to perform the given CRUD action for the given resource and optionally a specific record.

For example, to check whether the user can update a page with the id of 5, you can perform the following check.

select( 'core' ).canUser( 'update', 'pages', 5 )

Unfortuantely, this method only supports resources that are in the wp/v2 namespace. Additionally, it requires you to know the final REST API path. Typically, however, only an entity kind and name are known.

There currently exists a canUserEntityRecord selector, but it is only a wrapper for canUser and does not Post Type entity records. Additionally, it only supports Post Types that have the wp/v2 namespace which is not a requirement since WP 5.9.

export function canUserEditEntityRecord(
state: State,
kind: string,
name: string,
recordId: EntityRecordKey
): boolean | undefined {
const entityConfig = getEntityConfig( state, kind, name );
if ( ! entityConfig ) {
return false;
}
const resource = entityConfig.__unstable_rest_base;
return canUser( state, 'update', resource, recordId );
}

I think canUserEntityRecord should be adapted to actually perform the permission handling logic utilizing the baseURL property of the entity config. Then canUser would be deprecated.

Step-by-step reproduction instructions

  1. Register a custom post type with a custom namespace.
register_post_type( 'custom-ns', [
	'public'         => true,
	'show_in_rest'   => true,
	'rest_namespace' => 'my/namespace',
	'supports'       => [ 'editor', 'title', 'custom-fields' ],
] );
  1. Call the canUser selector via the browser console.
wp.data.select('core').canUser('create', 'custom-ns');

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@Mamaduka
Copy link
Member

Mamaduka commented Sep 1, 2022

Thanks for creating the issue.

I think we might want to keep canUser around for a while. The new recommended selector will require resources to be registered as custom entities; currently, we don't have an easy way of doing that. See #27859.

The canUserEditEntityRecord selector implies the action. We might need a better name here. Maybe hasPermssionsTo? Matches the new hook @adamziel stabilized recently - #43268.

@TimothyBJacobs
Copy link
Member Author

I think we might want to keep canUser around for a while. The new recommended selector will require resources to be registered as custom entities; currently, we don't have an easy way of doing that. See #27859.

I think most custom entities would be using a non wp/v2 namespace, so they wouldn't be able to leverage canUser anyways. But keeping it around seems fine too.

The canUserEditEntityRecord selector implies the action. We might need a better name here. Maybe hasPermssionsTo? Matches the new hook @adamziel stabilized recently - #43268.

I like that a lot too.

@adamziel
Copy link
Contributor

adamziel commented Sep 2, 2022

@Mamaduka we can't rename it, as it is a part of the public API, but we can create a new one and deprecate the old one. How about hasEntityRecordPermissions?

@Mamaduka
Copy link
Member

Mamaduka commented Sep 2, 2022

@adamziel, right. We should deprecate the canUserEditEntityRecord selector and introduce the new one.

@Mamaduka
Copy link
Member

I just want to cross-link the "Short-circuit HEAD methods in Core controllers" core ticket. When it's available in core, I think it would be a nice addition to the new selector/resolver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Core data /packages/core-data REST API Interaction Related to REST API [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.
Projects
None yet
4 participants