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

Template: Only show post template actions to users with correct capabilities #33392

Merged
merged 5 commits into from
Aug 9, 2021

Conversation

walbo
Copy link
Member

@walbo walbo commented Jul 13, 2021

Description

Post template actions shows on user that doesn't have correct capabilities. Ex editors has a New button but when they try to create a new template nothing happens. (See screenshot)

How has this been tested?

Locally with a editor user and with a adminitrator user.

Screenshots

Before

template-new

After

image

Types of changes

Bug fix. Only show post template actions for users with correct capabilities

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@walbo walbo changed the title Template: Only show post template actions for users with correct capabilities Template: Only show post template actions to users with correct capabilities Jul 13, 2021
@Mamaduka Mamaduka added [Feature] Template Editing Mode Related to the template editor available in the Block Editor [Type] Bug An existing feature does not function as intended labels Jul 14, 2021
Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work here @walbo ! This is something we should fix indeed.

Currently for handling of templates we use the edit_theme_options capability. I think we should use the same permission check here. In order to do that I guess we should add an __experimentalUserCanEditThemeOptions or something like that here with post-editor context, set it editor-settings and in use-block-editor-settings.js and finally use this value to conditionally show/hide the controls.

I'd love some thoughts about this from @gziolo .

@gziolo
Copy link
Member

gziolo commented Jul 20, 2021

I'm not sure if there are any permissions tied to a related REST resource here. In that case, we could use canUser:

export function canUser( state, action, resource, id ) {
const key = compact( [ action, resource, id ] ).join( '/' );
return get( state, [ 'userPermissions', key ] );
}

In general, it would be great to have a higher level selector that lets us check permission through REST API, rather than overload settings for that purpose.

@walbo
Copy link
Member Author

walbo commented Jul 20, 2021

It's tied to the templates REST api.

Updated the permission check to use canUser( 'create', 'templates' ). Retested the code after the change and seems to work as expected.

Can you confirm this is correct usage @gziolo ?

@ntsekouras
Copy link
Contributor

I'm not sure this is 100% correct but it requires folks with better caps knowledge than me to chime in :) .

If you try this:

canUserCreate: canUser( 'create', 'templates' ),
createOne: canUser( 'create', 'template' ),
updateMany: canUser( 'update', 'templates' ),
updateOne: canUser( 'update', 'template' ),

In admin and author roles are all false, with the exception in admin and canUserCreate: canUser( 'create', 'templates' ), which is true. 🤔

@gziolo gziolo requested a review from a team July 21, 2021 08:49
@gziolo
Copy link
Member

gziolo commented Jul 21, 2021

I'm not sure if that is correct. Let's wait for feedback from other folks.

@Mamaduka
Copy link
Member

Mamaduka commented Aug 9, 2021

I think usage here is correct. The resource name is templates since the endpoint is /wp/v2/templates. Checking canUser( 'create', 'template' ) should return false to any user.

I checked the response headers templates resource, and it only supports read and create actions for Admin users.

Editor Allow Headers Admin Allow Headers
user-can-templates-editor user-can-templates-admin

P.S. I think the only resource that supports all four actions is settings.

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚢

@Mamaduka Mamaduka merged commit 8439bd8 into WordPress:trunk Aug 9, 2021
@github-actions github-actions bot added this to the Gutenberg 11.3 milestone Aug 9, 2021
@walbo walbo deleted the fix/post-template-actions branch August 9, 2021 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Template Editing Mode Related to the template editor available in the Block Editor [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants