WordPress / two-factor

Two-Factor Authentication for WordPress.
https://wordpress.org/plugins/two-factor/
GNU General Public License v2.0
724 stars 151 forks source link

Standardise on `int|WP_User` input to the "for user" functions #535

Closed dd32 closed 1 year ago

dd32 commented 1 year ago

Currently the various API-level functions accept two main inputs: ID or WP_User, and assumes that if not provided the function should operate on the current user.

Accepts WP_User:

Accepts ID:

When a WP_User is passed in place of an ID, or an ID in place of a WP_User, the functions operate on the current user, which could lead to unexpected responses.

This PR standardises the above 4 functions to:

For example, currently:

wp_set_current_user( 1 );
$incorrect = Two_Factor_Core::get_enabled_providers_for_user( 2 );
$correct = Two_Factor_Core::get_enabled_providers_for_user( get_user_by( 'id', 2 ) );

// $incorrect is for user_id 1, despite 2 being passed in.
// $correct is correct, because a WP_User was passed

After this PR, both $incorrect and $correct in the above would contain the details for user 2.

I'll note that is_user_api_login_enabled() has not been touched, as the $user_id is not used for any logic within the function, and the onus is on the filtering functions to check the value of $user_id passed.