WordPress MU

Changeset 1918


Ignore:
Timestamp:
08/05/2009 02:46:08 PM (17 years ago)
Author:
donncha
Message:

Sanitize the current user's blogs and primary key.
Redirect them to the correct blog if they visit a blog dashboard they don't have permission for. See #1076

Location:
branches/2.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2.8/wp-admin/includes/mu.php

    r1914 r1918  
    497497function redirect_user_to_blog() {
    498498        global $current_user, $current_site;
    499         $details = get_active_blog_for_user( $current_user->ID );
    500499        $c = 0;
    501500        if ( isset( $_GET[ 'c' ] ) )
    … …  
    507506        $c ++;
    508507
    509         if( $details == "username only" ) {
    510                 if ( $dashboard_blog = get_site_option( 'dashboard_blog' ) ) {
    511                         add_user_to_blog( $dashboard_blog, $current_user->ID, 'subscriber' ); // Add subscriber permission for dashboard blog
    512                 } else {
    513                         add_user_to_blog( get_blog_id_from_url( $current_site->domain, $current_site->path ), $current_user->ID, 'subscriber' ); // Add subscriber permission for first blog.
    514                 }
     508        $blog = get_active_blog_for_user( $current_user->ID );
     509        $dashboard_blog = get_dashboard_blog();
     510        if( is_object( $blog ) ) {
     511                $protocol = ( is_ssl() ? 'https://' : 'http://' );
     512                wp_redirect( $protocol . $blog->domain . $blog->path . 'wp-admin/?c=' . $c ); // redirect and count to 5, "just in case"
     513                exit;
    515514        }
    516515
    … …  
    521520        $blogs = get_blogs_of_user( $current_user->ID );
    522521
    523         if ( sizeof( $blogs ) == 1 ) {
     522        if ( !empty( $blogs ) ) {
    524523                foreach( $blogs as $blogid => $blog ) {
    525                         if ( $blogid != 1 && get_usermeta( $current_user->ID , 'primary_blog' ) == 1 ) {
     524                        if ( $blogid != $dashboard_blog->blog_id && get_usermeta( $current_user->ID , 'primary_blog' ) == $dashboard_blog->blog_id ) {
    526525                                update_usermeta( $current_user->ID, 'primary_blog', $blogid );
    527                                 wp_redirect( "http://" . $blog->domain . $blog->path . 'wp-admin/?c=' . $c ); // redirect and count to 5, "just in case"
    528                                 exit;
     526                                continue;
    529527                        }
    530528                }
     529                $blog = get_blog_details( get_usermeta( $current_user->ID , 'primary_blog' ) );
     530                $protocol = ( is_ssl() ? 'https://' : 'http://' );
     531                wp_redirect( $protocol . $blog->domain . $blog->path . 'wp-admin/?c=' . $c ); // redirect and count to 5, "just in case"
     532                exit;
    531533        }
    532534        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
  • branches/2.8/wp-includes/wpmu-functions.php

    r1917 r1918  
    424424function get_active_blog_for_user( $user_id ) { // get an active blog for user - either primary blog or from blogs list
    425425        global $wpdb;
     426        $blogs = get_blogs_of_user( $user_id );
     427        if ( empty( $blogs ) ) {
     428                $details = get_dashboard_blog();
     429                add_user_to_blog( $details->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
     430                update_usermeta($user_id, 'primary_blog', $details->blog_id);
     431                wp_cache_delete( $user_id, 'users' );
     432                return $details;
     433        }
     434
    426435        $primary_blog = get_usermeta( $user_id, "primary_blog" );
    427         if( $primary_blog == false ) {
    428                 $details = false;
     436        if ( $primary_blog ) {
     437                $blogs = get_blogs_of_user( $user_id );
     438                if ( isset( $blogs[ $primary_blog ] ) == false ) {
     439                        $details = get_dashboard_blog();
     440                        add_user_to_blog( $details->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
     441                        update_usermeta( $user_id, 'primary_blog', $details->blog_id );
     442                        wp_cache_delete( $user_id, 'users' );
     443                } else {
     444                        $details = get_blog_details( $primary_blog );
     445                }
    429446        } else {
    430                 $details = get_blog_details( $primary_blog );
    431         }
    432 
    433         if( ( is_object( $details ) == false ) || ( is_object( $details ) && $details->archived == 1 || $details->spam == 1 || $details->deleted == 1 ) ) {
     447                $details = get_dashboard_blog();
     448                add_user_to_blog( $details->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
     449                update_usermeta( $user_id, 'primary_blog', $details->blog_id );
     450        }
     451
     452        if ( ( is_object( $details ) == false ) || ( is_object( $details ) && $details->archived == 1 || $details->spam == 1 || $details->deleted == 1 ) ) {
    434453                $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
    435454                $ret = false;
    436                 if( is_array( $blogs ) && count( $blogs ) > 0 ) {
     455                if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
    437456                        foreach( (array) $blogs as $blog_id => $blog ) {
    438457                                if ( $blog->site_id != $wpdb->siteid )
    439458                                        continue;
    440459                                $details = get_blog_details( $blog_id );
    441                                 if( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
     460                                if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
    442461                                        $ret = $blog;
    443462                                        $changed = false;
    444                                         if( !get_usermeta($user_id , 'primary_blog') ) {
    445                                                 update_usermeta($user_id, 'primary_blog', $blog->userblog_id);
     463                                        if ( get_usermeta( $user_id , 'primary_blog' ) != $blog_id ) {
     464                                                update_usermeta( $user_id, 'primary_blog', $blog_id );
    446465                                                $changed = true;
    447466                                        }
    448                                         if( !get_usermeta($user_id , 'source_domain') ) {
    449                                                 update_usermeta($user_id, 'source_domain', $blog->domain);
     467                                        if ( !get_usermeta($user_id , 'source_domain') ) {
     468                                                update_usermeta( $user_id, 'source_domain', $blog->domain );
    450469                                                $changed = true;
    451470                                        }
    452                                         if( $changed )
     471                                        if ( $changed )
    453472                                                wp_cache_delete( $user_id, 'users' );
    454473                                        break;
    … …  
    456475                        }
    457476                } else {
    458                         $ret = "username only"; // user has no blogs.
     477                        // Should never get here
     478                        $dashboard_blog = get_dashboard_blog();
     479                        add_user_to_blog( $dashboard_blog->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
     480                        update_usermeta( $user_id, 'primary_blog', $dashboard_blog->blog_id );
     481                        return $dashboard_blog;
    459482                }
    460483                return $ret;
Note: See TracChangeset for help on using the changeset viewer.