From 3e66526eedb5c13248e7b202947efb1c5800d0a6 Mon Sep 17 00:00:00 2001 From: Dominik Schilling Date: Wed, 6 Nov 2024 17:35:21 +0100 Subject: [PATCH] Support filtering providers per user --- class-two-factor-core.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/class-two-factor-core.php b/class-two-factor-core.php index 99033a70..6bc7c2e4 100644 --- a/class-two-factor-core.php +++ b/class-two-factor-core.php @@ -257,10 +257,12 @@ private static function get_providers_classes() { * Get all enabled two-factor providers. * * @since 0.1-dev + * @since 0.10.0 Added the $user_id parameter. * + * @param int|null $user_id Optional. User ID. * @return array */ - public static function get_providers() { + public static function get_providers( $user_id = null ) { $providers = self::get_providers_registered(); /** @@ -269,10 +271,11 @@ public static function get_providers() { * This lets third-parties either remove providers (such as Email), or * add their own providers (such as text message or Clef). * - * @param array $providers A key-value array where the key is the class name, and - * the value is the path to the file containing the class. + * @param array $providers A key-value array where the key is the class name, and + * the value is the path to the file containing the class. + * @param int|null $user_id The user ID. */ - $providers = apply_filters( 'two_factor_providers', $providers ); + $providers = apply_filters( 'two_factor_providers', $providers, $user_id ); // FIDO U2F is PHP 5.3+ only. if ( isset( $providers['Two_Factor_FIDO_U2F'] ) && version_compare( PHP_VERSION, '5.3.0', '<' ) ) { @@ -499,7 +502,7 @@ public static function get_enabled_providers_for_user( $user = null ) { return array(); } - $providers = self::get_providers(); + $providers = self::get_providers( $user->ID ); $enabled_providers = get_user_meta( $user->ID, self::ENABLED_PROVIDERS_USER_META_KEY, true ); if ( empty( $enabled_providers ) ) { $enabled_providers = array(); @@ -527,7 +530,7 @@ public static function get_available_providers_for_user( $user = null ) { return array(); } - $providers = self::get_providers(); + $providers = self::get_providers( $user->ID ); $enabled_providers = self::get_enabled_providers_for_user( $user ); $configured_providers = array(); @@ -590,7 +593,7 @@ public static function get_primary_provider_for_user( $user = null ) { return null; } - $providers = self::get_providers(); + $providers = self::get_providers( $user->ID); $available_providers = self::get_available_providers_for_user( $user ); // If there's only one available provider, force that to be the primary. @@ -1834,7 +1837,7 @@ public static function user_two_factor_options( $user ) { - $object ) : ?> + ID ) as $provider_key => $object ) : ?> /> /> @@ -1889,7 +1892,7 @@ public static function user_two_factor_options( $user ) { * @return bool True if the provider was enabled, false otherwise. */ public static function enable_provider_for_user( $user_id, $new_provider ) { - $available_providers = self::get_providers(); + $available_providers = self::get_providers( $user_id ); if ( ! array_key_exists( $new_provider, $available_providers ) ) { return false; @@ -1929,7 +1932,7 @@ public static function enable_provider_for_user( $user_id, $new_provider ) { * @return bool True if the provider was disabled, false otherwise. */ public static function disable_provider_for_user( $user_id, $provider_to_delete ) { - $is_registered = array_key_exists( $provider_to_delete, self::get_providers() ); + $is_registered = array_key_exists( $provider_to_delete, self::get_providers( $user_id ) ); if ( ! $is_registered ) { return false; @@ -1970,7 +1973,7 @@ public static function user_two_factor_options_update( $user_id ) { return; } - $providers = self::get_providers(); + $providers = self::get_providers( $user_id ); $enabled_providers = $_POST[ self::ENABLED_PROVIDERS_USER_META_KEY ]; $existing_providers = self::get_enabled_providers_for_user( $user_id );