Make WordPress Core

Changeset 32174


Ignore:
Timestamp:
04/20/2015 07:29:01 AM (11 years ago)
Author:
pento
Message:

In Multisite, prevent plugins from unintentionally switching sites. Merge of [32173] to the 4.1 branch.

Props mdawaffe.

Location:
branches/4.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1/src/wp-includes/capabilities.php

    r30681 r32174  
    13751375 */
    13761376function current_user_can_for_blog( $blog_id, $capability ) {
    1377     if ( is_multisite() )
    1378         switch_to_blog( $blog_id );
     1377    $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
    13791378
    13801379    $current_user = wp_get_current_user();
    13811380
    1382     if ( empty( $current_user ) )
     1381    if ( empty( $current_user ) ) {
     1382        if ( $switched ) {
     1383            restore_current_blog();
     1384        }
    13831385        return false;
     1386    }
    13841387
    13851388    $args = array_slice( func_get_args(), 2 );
     
    13881391    $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
    13891392
    1390     if ( is_multisite() )
     1393    if ( $switched ) {
    13911394        restore_current_blog();
     1395    }
    13921396
    13931397    return $can;
  • branches/4.1/tests/phpunit/tests/user/capabilities.php

    r27390 r32174  
    661661        $author->remove_cap( 'foo' );
    662662        $this->assertFalse ( isset( $author->caps['foo'] ) );
     663    }
     664
     665    function test_borked_current_user_can_for_blog() {
     666        if ( ! is_multisite() ) {
     667            $this->markTestSkipped( 'Test only runs in multisite' );
     668            return;
     669        }
     670
     671        $orig_blog_id = get_current_blog_id();
     672        $blog_id = $this->factory->blog->create();
     673
     674        $nullify_current_user = function() {
     675            // Prevents fatal errors in ::tearDown()'s and other uses of restore_current_blog()
     676            $function_stack = wp_debug_backtrace_summary( null, 0, false );
     677            if ( in_array( 'restore_current_blog', $function_stack ) ) {
     678                return;
     679            }
     680            $GLOBALS['current_user'] = null;
     681        };
     682
     683        $nullify_current_user_and_keep_nullifying_user = function() use ( $nullify_current_user ) {
     684            $nullify_current_user();
     685
     686            add_action( 'set_current_user', $nullify_current_user );
     687        };
     688
     689        $nullify_current_user();
     690
     691        add_action( 'switch_blog', $nullify_current_user_and_keep_nullifying_user );
     692
     693        current_user_can_for_blog( $blog_id, 'edit_posts' );
     694
     695        $this->assertEquals( $orig_blog_id, get_current_blog_id() );
    663696    }
    664697
Note: See TracChangeset for help on using the changeset viewer.