Multisite: Initialize a user's roles correctly when setting them up for a different site.

While it has always been possible to initialize a user's roles and capabilities for another site than the current one in a multisite, the actual roles available were not switched prior to this change, possibly causing invalid roles to show up or actually valid capabilities not being available.

In order to fix this bug in a clean way, relevant parts of the `WP_User` class have been refactored. The ID of the site for which capabilities are currently initialized are now stored in a private property `WP_User::$site_id`. The `WP_User::for_blog( $blog_id )` and `WP_User::_init_caps( $cap_key )` methods have been deprecated in favor of `WP_User::for_site( $site_id )`. In addition, a new method `WP_User::get_site_id()` has been introduced to retrieve the site ID for which the user's capabilities are currently initialized.

Props ryanduff, jeremyfelt, flixos90.
Fixes #36961.


git-svn-id: https://develop.svn.wordpress.org/trunk@41624 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz
2017-09-27 21:09:11 +00:00
parent 0ef31b8de5
commit 8be3b4f729
4 changed files with 201 additions and 39 deletions
+1 -1
View File
@@ -180,7 +180,7 @@ class Tests_User extends WP_UnitTestCase {
$this->assertEquals( 'foo', $user->$key );
$this->assertEquals( 'foo', $user->data->$key ); // This will fail with WP < 3.3
foreach ( (array) $user as $key => $value ) {
foreach ( get_object_vars( $user ) as $key => $value ) {
$this->assertEquals( $value, $user->$key );
}
}
+91
View File
@@ -1848,4 +1848,95 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$this->assertFalse( user_can( self::$users['contributor']->ID, 'remove_user', self::$users['contributor']->ID ) );
$this->assertFalse( user_can( self::$users['subscriber']->ID, 'remove_user', self::$users['subscriber']->ID ) );
}
/**
* @ticket 36961
* @group ms-required
*/
function test_init_user_caps_for_different_site() {
global $wpdb;
$site_id = self::factory()->blog->create( array( 'user_id' => self::$users['administrator']->ID ) );
switch_to_blog( $site_id );
$role_name = 'uploader';
add_role( $role_name, 'Uploader', array(
'read' => true,
'upload_files' => true,
) );
add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name );
restore_current_blog();
$user = new WP_User( self::$users['subscriber']->ID, '', $site_id );
$this->assertTrue( $user->has_cap( 'upload_files' ) );
}
/**
* @ticket 36961
* @group ms-required
*/
function test_init_user_caps_for_different_site_by_user_switch() {
global $wpdb;
$user = new WP_User( self::$users['subscriber']->ID );
$site_id = self::factory()->blog->create( array( 'user_id' => self::$users['administrator']->ID ) );
switch_to_blog( $site_id );
$role_name = 'uploader';
add_role( $role_name, 'Uploader', array(
'read' => true,
'upload_files' => true,
) );
add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name );
restore_current_blog();
$user->for_site( $site_id );
$this->assertTrue( $user->has_cap( 'upload_files' ) );
}
/**
* @ticket 36961
*/
function test_get_caps_data() {
global $wpdb;
$custom_caps = array(
'do_foo' => true,
'do_bar' => false,
);
// Test `WP_User::get_caps_data()` by manually setting capabilities metadata.
update_user_meta( self::$users['subscriber']->ID, $wpdb->get_blog_prefix( get_current_blog_id() ) . 'capabilities', $custom_caps );
$user = new WP_User( self::$users['subscriber']->ID );
$this->assertSame( $custom_caps, $user->caps );
}
/**
* @ticket 36961
*/
function test_user_get_site_id_default() {
$user = new WP_User( self::$users['subscriber']->ID );
$this->assertSame( get_current_blog_id(), $user->get_site_id() );
}
/**
* @ticket 36961
*/
function test_user_get_site_id() {
global $wpdb;
// Suppressing errors here allows to get around creating an actual site,
// which is unnecessary for this test.
$suppress = $wpdb->suppress_errors();
$user = new WP_User( self::$users['subscriber']->ID, '', 333 );
$wpdb->suppress_errors( $suppress );
$this->assertSame( 333, $user->get_site_id() );
}
}