Make WordPress Core

Opened 6 years ago

Last modified 2 weeks ago

#45197 reviewing enhancement

Introduce `user_can_for_blog()`

Reported by: johnbillion's profile johnbillion Owned by: johnbillion's profile johnbillion
Milestone: 6.7 Priority: normal
Severity: normal Version:
Component: Role/Capability Keywords: has-patch has-unit-tests needs-testing has-testing-info
Focuses: multisite Cc:

Description

The available user capability checking functions include:

  • current_user_can()
  • user_can()
  • current_user_can_for_blog()

What's missing is user_can_for_blog() so that both a user ID and a site ID can be passed in order to check a given user's capabilities on a given site.

Attachments (1)

45197.diff (1.1 KB) - added by tmanoilov 6 years ago.

Download all attachments as: .zip

Change History (12)

#1 @johnjamesjacoby
6 years ago

+1 to this.

I have coded some version of this logic at least a few times (into private methods in plugins.)

I can adapt something into a patch, but it’s pretty straight forward if anyone wants to try.

Last edited 6 years ago by johnjamesjacoby (previous) (diff)

@tmanoilov
6 years ago

#2 @tmanoilov
6 years ago

  • Keywords has-patch added; needs-patch removed

Hey guys, I'm at WordCamp Sofia 2018 Contributors day and this is the first patch I'm submitting. I'm not able to fully test it so I'm looking forward to a review. Thanks in advance.

#3 @johnbillion
5 months ago

  • Milestone changed from Future Release to 6.6
  • Owner set to johnbillion
  • Status changed from new to reviewing

This ticket was mentioned in PR #6418 on WordPress/wordpress-develop by @rajinsharwar.


3 months ago
#4

  • Keywords has-unit-tests added; needs-unit-tests removed

Adding a new function named user_can_for_blog(), and unit tests for it.

Trac ticket: https://core.trac.wordpress.org/ticket/45197

#5 @rajinsharwar
3 months ago

Added the new function, along with its unit tests.

@johnbillion commented on PR #6418:


2 months ago
#6

@Rajinsharwar @JJJ Any thoughts on what's up here? The test_current_user_can_for_blog() test is failing when it checks the edit_posts capability of a user on a site that the user doesn't belong to. The result is incorrectly true instead of false.

@rajinsharwar commented on PR #6418:


2 months ago
#7

Fixed @johnbillion, used the user_can() instead in the function, the previous method didn't check for the current_blog I bielive.

#8 @rajinsharwar
2 months ago

Hi @johnbillion, wondering if we need separate manual testing for this, or if we can mark this for commit. Maybe we can get this committed as early as possible so that it can be tested out in beta.

This ticket was mentioned in Slack in #core by oglekler. View the logs.


8 weeks ago

#10 @oglekler
7 weeks ago

  • Keywords needs-testing needs-testing-info added
  • Milestone changed from 6.6 to 6.7

We have 2 days before Beta 1 and no time to test, so I am moving it into the next milestone. 

@rajinsharwar I wonder if it is worth to provide code example for testing, how actually this new functions can be used because it already covered in unit tests, but still it will be easier to use (but can limit tester's creativity and the same time).

#11 @rajinsharwar
2 weeks ago

  • Keywords has-testing-info added; needs-testing-info removed

Yeah, maybe some code testing info might be helpful!

So this is a new function, especially useful for multisites. Used to check if a current user can a particular capability in a particular site in the network.

Testing instructions:

  1. Make sure you are in a multisite environment and have more than one subsite. Let's say, you have a editor user with an ID of 2 in your subsite 1. And another subscriber user with an ID of 3, in your subsite 2.
  2. In the functions.php of your active theme on your main site, add this code:
    
    error_log('User Capability for Editor User ID 2, in subsite ID 1: ' . user_can_for_blog( 2, 1, 'delete_pages' ) );
    error_log('User Capability for Subscriber User ID 3, in subsite ID 1: ' . user_can_for_blog( 3, 1, 'delete_pages' ) );
    
    error_log('User Capability for False cap User ID 2, in subsite ID 1: ' . user_can_for_blog( 2, 1, 'foo_bar' ) );
    error_log('Read User Capability for Subscriber User ID 3, in subsite ID 1: ' . user_can_for_blog( 3, 1, 'read' ) );
    
    

Here the:
"User Capability for Editor User ID 2, in subsite ID 1" should be true.
"User Capability for Subscriber User ID 3, in subsite ID 1" should be false.
"User Capability for False cap User ID 2, in subsite ID 1" should be false.
"Read User Capability for Subscriber User ID 3" should be true.

Feel free to add/edit this @oglekler or anyone working on this ticket if you feel. 🙂

Note: See TracTickets for help on using tickets.