Skip to:
Content

BuddyPress.org

Changeset 12666


Ignore:
Timestamp:
06/16/2020 04:31:11 AM (11 months ago)
Author:
r-a-y
Message:

Members: When marking a user as a spammer, do not mark sites as spam if the site has more than one administrator.

Previously on a multisite install, we would mark all the spammer's
sites as spam. This is pretty aggressive and could unintentionally
mark legitmiate sites as spam as well.

To address this, we now only mark a site as spam if the spammer is the
sole administrator of the site.

Fixes #8316 (for 6.0 branch).

Location:
branches/6.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.0/src/bp-members/bp-members-functions.php

    r12605 r12666  
    693693    if ( $do_wp_cleanup ) {
    694694
    695         // Get the blogs for the user.
    696         $blogs = get_blogs_of_user( $user_id, true );
    697 
    698         foreach ( (array) array_values( $blogs ) as $details ) {
    699 
    700             // Do not mark the main or current root blog as spam.
    701             if ( 1 == $details->userblog_id || bp_get_root_blog_id() == $details->userblog_id ) {
    702                 continue;
     695        // Mark blogs as spam if the user is the sole admin of a site.
     696        if ( is_multisite() ) {
     697            /*
     698             * No native function to fetch a user's blogs by role, so do it manually.
     699             *
     700             * This logic is mostly copied from get_blogs_of_user().
     701             */
     702            $meta = get_user_meta( $user_id );
     703
     704            foreach ( $meta as $key => $val ) {
     705                if ( 'capabilities' !== substr( $key, -12 ) ) {
     706                    continue;
     707                }
     708                if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) {
     709                    continue;
     710                }
     711                $site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
     712                if ( ! is_numeric( $site_id ) ) {
     713                    continue;
     714                }
     715
     716                $site_id = (int) $site_id;
     717
     718                // Do not mark the main or current root blog as spam.
     719                if ( 1 === $site_id || bp_get_root_blog_id() === $site_id ) {
     720                    continue;
     721                }
     722
     723                // Now, do check for administrator role.
     724                $role = maybe_unserialize( $val );
     725                if ( empty( $role['administrator'] ) ) {
     726                    continue;
     727                }
     728
     729                // Check if the site has more than 1 admin. If so, bail.
     730                $counts = count_users( 'time', $site_id );
     731                if ( empty( $counts['avail_roles']['administrator'] ) || $counts['avail_roles']['administrator'] > 1 ) {
     732                    continue;
     733                }
     734
     735                // Now we can spam the blog.
     736                update_blog_status( $site_id, 'spam', $is_spam );
    703737            }
    704 
    705             // Update the blog status.
    706             update_blog_status( $details->userblog_id, 'spam', $is_spam );
    707738        }
    708739
  • branches/6.0/tests/phpunit/testcases/members/functions.php

    r12606 r12666  
    538538        $this->assertSame( 'bp_make_ham_user', $this->filter_fired );
    539539
     540    }
     541
     542    /**
     543     * @group bp_core_process_spammer_status
     544     * @ticket BP8316
     545     */
     546    public function test_bp_core_process_spammer_status_ms_should_only_spam_sites_with_one_admin() {
     547        if ( ! is_multisite() ) {
     548            $this->markTestSkipped();
     549        }
     550
     551        $u1 = self::factory()->user->create();
     552        $u2 = self::factory()->user->create();
     553
     554        $b1 = self::factory()->blog->create( array( 'user_id' => $u1 ) );
     555
     556        // Add user 2 to site as administrator.
     557        add_user_to_blog( $b1, $u2, 'administrator' );
     558
     559        // Mark user 2 as a spammer.
     560        bp_core_process_spammer_status( $u2, 'spam' );
     561
     562        // Ensure site isn't marked as spam because there is more than one admin.
     563        $site = get_site( $b1 );
     564        $this->assertEmpty( $site->spam );
    540565    }
    541566
Note: See TracChangeset for help on using the changeset viewer.