Commit bb25333e049 for woocommerce

commit bb25333e0495f796b44bc37c2dad09dae072c16f
Author: Jorge A. Torres <jorge.torres@automattic.com>
Date:   Tue Dec 1 09:45:40 2026 -0500

    Restrict the REST API key user search to users the current user can assign a key to (#70958)

diff --git a/plugins/woocommerce/changelog/enhancement-api-keys-user-list b/plugins/woocommerce/changelog/enhancement-api-keys-user-list
new file mode 100644
index 00000000000..077a4c21829
--- /dev/null
+++ b/plugins/woocommerce/changelog/enhancement-api-keys-user-list
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Hide users that can't be assigned a REST API key from the key owner search.
diff --git a/plugins/woocommerce/client/legacy/js/admin/wc-enhanced-select.js b/plugins/woocommerce/client/legacy/js/admin/wc-enhanced-select.js
index 396650eeb07..d4c6acf55bd 100644
--- a/plugins/woocommerce/client/legacy/js/admin/wc-enhanced-select.js
+++ b/plugins/woocommerce/client/legacy/js/admin/wc-enhanced-select.js
@@ -408,6 +408,7 @@ jQuery( function ( $ ) {
 										security:
 											wc_enhanced_select_params.search_customers_nonce,
 										exclude: $( this ).data( 'exclude' ),
+										context: $( this ).data( 'context' ),
 									};
 								},
 								processResults: function ( data ) {
diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-api-keys.php b/plugins/woocommerce/includes/admin/class-wc-admin-api-keys.php
index 51d1ea91399..56b60f2cbed 100644
--- a/plugins/woocommerce/includes/admin/class-wc-admin-api-keys.php
+++ b/plugins/woocommerce/includes/admin/class-wc-admin-api-keys.php
@@ -20,6 +20,29 @@ class WC_Admin_API_Keys {
 		add_action( 'admin_init', array( $this, 'actions' ) );
 		add_action( 'woocommerce_settings_page_init', array( $this, 'screen_option' ) );
 		add_filter( 'woocommerce_save_settings_advanced_keys', array( $this, 'allow_save_settings' ) );
+		add_filter( 'woocommerce_json_search_found_customers', array( $this, 'restrict_api_key_user_search' ) );
+	}
+
+	/**
+	 * Hide users the current user can't actually assign a key to from the API Keys "User" field search.
+	 *
+	 * @internal Public only because it is used as a hook callback.
+	 *
+	 * @since 11.5.0
+	 * @param  array $found_customers Map of user ID to display text.
+	 * @return array
+	 */
+	public function restrict_api_key_user_search( $found_customers ) {
+		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce already verified by the handler.
+		if ( ! is_array( $found_customers ) || ! doing_action( 'wp_ajax_woocommerce_json_search_customers' ) || ! isset( $_GET['context'] ) || 'admin_rest_api_keys' !== $_GET['context'] ) {
+			return $found_customers;
+		}
+
+		return array_filter(
+			$found_customers,
+			static fn( $user_id ) => current_user_can( 'edit_user', $user_id ),
+			ARRAY_FILTER_USE_KEY
+		);
 	}

 	/**
diff --git a/plugins/woocommerce/includes/admin/settings/views/html-keys-edit.php b/plugins/woocommerce/includes/admin/settings/views/html-keys-edit.php
index a893cdb17fd..4624e82eb9d 100644
--- a/plugins/woocommerce/includes/admin/settings/views/html-keys-edit.php
+++ b/plugins/woocommerce/includes/admin/settings/views/html-keys-edit.php
@@ -56,7 +56,7 @@ defined( 'ABSPATH' ) || exit;
 						$user->user_email
 					);
 					?>
-					<select class="wc-customer-search" id="key_user" data-placeholder="<?php esc_attr_e( 'Search for a user&hellip;', 'woocommerce' ); ?>" data-allow_clear="true">
+					<select class="wc-customer-search" id="key_user" data-context="admin_rest_api_keys" data-placeholder="<?php esc_attr_e( 'Search for a user&hellip;', 'woocommerce' ); ?>" data-allow_clear="true">
 						<option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( wp_kses_post( $user_string ) ); // htmlspecialchars to prevent XSS when rendered by selectWoo. ?></option>
 					</select>
 				</td>
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-ajax-test.php b/plugins/woocommerce/tests/php/includes/class-wc-ajax-test.php
index d7a3a5183bc..718d3d1d160 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-ajax-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-ajax-test.php
@@ -2045,6 +2045,53 @@ class WC_AJAX_Test extends \WP_Ajax_UnitTestCase {
 		$legacy_proxy->reset();
 	}

+	/**
+	 * @testdox Should only hide users from the REST API key owner search for a shop manager tagging the request with the API Keys context.
+	 * @param string $current_role Role of the user performing the search.
+	 * @param bool   $with_context Whether the request is tagged with the API Keys search context.
+	 * @param string $target_role Role of the user being searched for, or "self" to search for the current user.
+	 * @param bool   $expect_visible Whether the target user should appear in the results.
+	 * @testWith ["administrator", true, "shop_manager", true]
+	 *           ["administrator", false, "shop_manager", true]
+	 *           ["shop_manager", true, "customer", true]
+	 *           ["shop_manager", true, "shop_manager", false]
+	 *           ["shop_manager", false, "shop_manager", true]
+	 *           ["shop_manager", true, "administrator", false]
+	 *           ["shop_manager", true, "subscriber", false]
+	 *           ["shop_manager", true, "self", true]
+	 */
+	public function test_json_search_api_key_users_filters_by_role_and_context( string $current_role, bool $with_context, string $target_role, bool $expect_visible ): void {
+		$current_user_id = self::factory()->user->create( array( 'role' => $current_role ) );
+		$target_id       = 'customer' === $target_role
+			? WC_Helper_Customer::create_customer( 'api-key-target', 'pass1', 'api-key-target@example.com' )->get_id()
+			: ( 'self' === $target_role ? $current_user_id : self::factory()->user->create( array( 'role' => $target_role ) ) );
+
+		try {
+			wp_set_current_user( $current_user_id );
+			$_GET['security'] = wp_create_nonce( 'search-customers' );
+			$_GET['term']     = (string) $target_id;
+			if ( $with_context ) {
+				$_GET['context'] = 'admin_rest_api_keys';
+			}
+
+			// WC_Admin_API_Keys normally loads via WC_Admin::includes() on 'init', which only runs on
+			// a real admin request. The ajax test harness never flips is_admin() to true until after
+			// 'init' has already fired, so nothing hooks its search filter unless we load it here.
+			new WC_Admin_API_Keys();
+
+			$response = $this->do_ajax( 'woocommerce_json_search_customers' );
+
+			if ( $expect_visible ) {
+				$this->assertArrayHasKey( $target_id, $response );
+			} else {
+				$this->assertArrayNotHasKey( $target_id, $response );
+			}
+		} finally {
+			unset( $_GET['security'], $_GET['term'], $_GET['context'] );
+			wp_set_current_user( 0 );
+		}
+	}
+
 	/**
 	 * Describes the behavior of the `get_customer_details` ajax endpoint, particularly in relation to
 	 * permissions of the requesting user.