Skip to:
Content

BuddyPress.org

Changeset 14116


Ignore:
Timestamp:
03/20/2025 08:24:38 PM (3 months ago)
Author:
dcavins
Message:

Restrict bulk notification management to owner (11.0 branch).

When attempting to manage notifications in bulk, ensure that the current user is either a site admin or owns all of the notifications specified.

Many thanks to Brian Mungah for responsibly reporting the problem.

Location:
branches/11.0
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/11.0/src/bp-notifications/actions/bulk-manage.php

    r13112 r14116  
    4141
    4242    // Delete, mark as read or unread depending on the user 'action'.
     43    $result = bp_notifications_bulk_manage_notifications( $action, $notifications );
     44
     45    // Set message depending on the user 'action'.
    4346    switch ( $action ) {
    4447        case 'delete':
    45             bp_notifications_delete_notifications_by_ids( $notifications );
    4648            bp_core_add_message( __( 'Notifications deleted.', 'buddypress' ) );
    4749            break;
    4850
    4951        case 'read':
    50             bp_notifications_mark_notifications_by_ids( $notifications, false );
    5152            bp_core_add_message( __( 'Notifications marked as read', 'buddypress' ) );
    5253            break;
    5354
    5455        case 'unread':
    55             bp_notifications_mark_notifications_by_ids( $notifications, true );
    5656            bp_core_add_message( __( 'Notifications marked as unread.', 'buddypress' ) );
    5757            break;
  • branches/11.0/src/bp-notifications/bp-notifications-functions.php

    r13112 r14116  
    669669
    670670/**
     671 * Mark a batch of notifications as read or unread or delete them.
     672 *
     673 * @since 11.4.4 (Backported from 14.3.4)
     674 *
     675 * @param string $user_id          Action to run on notifications.
     676 * @param array  $notification_ids IDs of the notifications to change.
     677 * @return bool True if the action run returned true.
     678 */
     679function bp_notifications_bulk_manage_notifications( $action, $notification_ids = array() ) {
     680    $notification_ids = wp_parse_id_list( $notification_ids );
     681    if ( empty( $notification_ids ) ) {
     682        return false;
     683    }
     684
     685    if ( ! current_user_can( 'bp_manage' ) ) {
     686        // Regular users can only manage their own notifications.
     687        $all_user_notifications     = BP_Notifications_Notification::get(
     688            array(
     689                'user_id'           => bp_loggedin_user_id(),
     690                'is_new'            => 'both', // Allow unread and read notices to be found.
     691                'update_meta_cache' => false,
     692            )
     693        );
     694        $all_user_notifications_ids = wp_list_pluck( $all_user_notifications, 'id' );
     695        $notification_ids           = array_intersect( $notification_ids, $all_user_notifications_ids );
     696        if ( empty( $notification_ids ) ) {
     697            return false;
     698        }
     699    }
     700
     701    // Delete, mark as read or unread depending on the 'action'.
     702    $result = false;
     703    switch ( $action ) {
     704        case 'delete':
     705            $result = bp_notifications_delete_notifications_by_ids( $notification_ids );
     706            break;
     707
     708        case 'read':
     709            $result = bp_notifications_mark_notifications_by_ids( $notification_ids, false );
     710            break;
     711
     712        case 'unread':
     713            $result = bp_notifications_mark_notifications_by_ids( $notification_ids, true );
     714            break;
     715    }
     716
     717    return ( bool ) $result;
     718}
     719
     720/**
    671721 * Check if a user has access to a specific notification.
    672722 *
  • branches/11.0/tests/phpunit/testcases/notifications/functions.php

    r13243 r14116  
    501501        $this->assertTrue( 1 === (int) $n_obj->is_new );
    502502    }
     503
     504    /**
     505     * @group bulk_manage_notifications
     506     */
     507    public function test_bp_notifications_bulk_manage_notifications_user_must_own_items() {
     508        $u1 = self::factory()->user->create();
     509        $u2 = self::factory()->user->create();
     510
     511        // Create notifications
     512        $n1 = self::factory()->notification->create( array(
     513            'component_name'    => 'messages',
     514            'component_action'  => 'new_message',
     515            'item_id'           => 99,
     516            'user_id'           => $u1,
     517        ) );
     518        $n2 = self::factory()->notification->create( array(
     519            'component_name'    => 'messages',
     520            'component_action'  => 'new_message',
     521            'item_id'           => 100,
     522            'user_id'           => $u1,
     523        ) );
     524        $n3 = self::factory()->notification->create( array(
     525            'component_name'    => 'messages',
     526            'component_action'  => 'new_message',
     527            'item_id'           => 101,
     528            'user_id'           => $u2,
     529        ) );
     530
     531        wp_set_current_user( $u2 );
     532        // Attempt to mark all as read.
     533        bp_notifications_bulk_manage_notifications( 'read', array( $n1, $n2, $n3 ) );
     534
     535        // Check status of $n2 (which shouldn't be affected).
     536        $n_get = BP_Notifications_Notification::get(
     537            array(
     538                'id'               => $n2,
     539                'component_name'   => 'messages',
     540                'component_action' => 'new_message',
     541                'is_new'           => 'both',
     542            )
     543        );
     544        $n_obj = reset( $n_get );
     545        $this->assertTrue( 1 === (int) $n_obj->is_new );
     546
     547        // Check status of $n3 (which should be affected).
     548        $n_get = BP_Notifications_Notification::get(
     549            array(
     550                'id'               => $n3,
     551                'component_name'   => 'messages',
     552                'component_action' => 'new_message',
     553                'is_new'           => 'both',
     554            )
     555        );
     556        $n_obj = reset( $n_get );
     557        $this->assertTrue( 0 === (int) $n_obj->is_new );
     558    }
     559
    503560}
Note: See TracChangeset for help on using the changeset viewer.