Skip to:
Content

BuddyPress.org

Changeset 14102


Ignore:
Timestamp:
03/20/2025 06:37:00 PM (3 months ago)
Author:
dcavins
Message:

Restrict bulk notification management to owner (14.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/14.0
Files:
3 edited

Legend:

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

    r13882 r14102  
    3939
    4040    // Delete, mark as read or unread depending on the user 'action'.
     41    $result = bp_notifications_bulk_manage_notifications( $action, $notifications );
     42
     43    // Set message depending on the user 'action'.
    4144    switch ( $action ) {
    4245        case 'delete':
    43             bp_notifications_delete_notifications_by_ids( $notifications );
    4446            bp_core_add_message( __( 'Notifications deleted.', 'buddypress' ) );
    4547            break;
    4648
    4749        case 'read':
    48             bp_notifications_mark_notifications_by_ids( $notifications, false );
    4950            bp_core_add_message( __( 'Notifications marked as read', 'buddypress' ) );
    5051            break;
    5152
    5253        case 'unread':
    53             bp_notifications_mark_notifications_by_ids( $notifications, true );
    5454            bp_core_add_message( __( 'Notifications marked as unread.', 'buddypress' ) );
    5555            break;
  • branches/14.0/src/bp-notifications/bp-notifications-functions.php

    r13499 r14102  
    669669
    670670/**
     671 * Mark a batch of notifications as read or unread or delete them.
     672 *
     673 * @since 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/14.0/tests/phpunit/testcases/notifications/functions.php

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