Skip to content

Commit

Permalink
cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Sep 1, 2023
1 parent 08c9ede commit 0ec9c1e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 83 deletions.
56 changes: 2 additions & 54 deletions class-two-factor-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -1845,63 +1845,11 @@ public static function disable_provider_for_user( $user_id, $provider_to_delete
return true;
}

$primary_provider = self::get_primary_provider_for_user( $user_id );
$new_enabled_providers = array_diff( $old_enabled_providers, array( $provider_to_delete ) );
$was_disabled = update_user_meta( $user_id, self::ENABLED_PROVIDERS_USER_META_KEY, $new_enabled_providers );
// $disabled_primary_provider = is_object( $primary_provider ) && $primary_provider->get_key() === $provider_to_delete;

// if ( $disabled_primary_provider ) {
// if ( $new_enabled_providers ) {
// // call the prim func to set it?
// $new_primary_provider = array_keys( $new_enabled_providers )[0]; // need to use key?
// $has_primary = update_user_meta( $user_id, self::PROVIDER_USER_META_KEY, $new_primary_provider );
// } else {
// $has_primary = delete_user_meta( $user_id, self::PROVIDER_USER_META_KEY );
// // failing to delete b/c it doesn't exist i guess, maybe tests need to manually set which is primary
// }
// } else {
// $has_primary = true;
// }

return (bool) $was_disabled; //&& $has_primary;
}

// overcomplicated
// /**
// * Get the most secure provider among the available to the user.
// *
// * @param int $user_id The ID of the user.
// */
// public static function get_most_secure_available_provider_for_user( $user_id ) {
// /**
// * Filter which provider is selected as the most secure for the user. The first provider in the array is
// * considered the most secure, and the last provider is considered the least secure.
// *
// * @param string $selected_provider The most secure provider available to the user.
// * @param int $user_id The ID of the user.
// */
// $most_secure_to_least = apply_filters(
// 'two_factor_provider_security_rank',
// array(
// 'Two_Factor_FIDO_U2F',
// 'Two_Factor_Totp',
// 'Two_Factor_Email',
// 'Two_Factor_Backup_Codes',
// 'Two_Factor_Dummy',
// ),
// $user_id
// );

// $user_available_providers = array_keys( self::get_available_providers_for_user( $user_id ) );

// foreach ( $most_secure_to_least as $provider ) {
// if ( in_array( $provider, $user_available_providers, true ) ) {
// return $provider;
// }
// }

// return false;
// }
return (bool) $was_disabled;
}

/**
* Update the user meta value.
Expand Down
29 changes: 0 additions & 29 deletions tests/class-two-factor-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -866,35 +866,6 @@ public function test_enable_disable_provider_for_user() {
$this->assertSame( 'Two_Factor_Dummy', Two_Factor_Core::get_primary_provider_for_user( $user->ID )->get_key() );
}

/**
* @covers Two_Factor_Core::get_most_secure_available_provider_for_user()
*/
// public function test_get_most_secure_available_provider_for_user() {
// $user = self::factory()->user->create_and_get();

// $this->assertFalse( Two_Factor_Core::get_most_secure_available_provider_for_user( $user->ID ) );

// // Enable TOTP and Email. TOTP should be most secure.
// Two_Factor_Core::enable_provider_for_user( $user->ID, 'Two_Factor_Totp' );
// Two_Factor_Core::enable_provider_for_user( $user->ID, 'Two_Factor_Email' );

// $totp = Two_Factor_Totp::get_instance();
// $totp->set_user_totp_key( $user->ID, 'foo' );
// $available_providers = array_keys( Two_Factor_Core::get_available_providers_for_user( $user->ID ) );

// $this->assertSame( array( 'Two_Factor_Email', 'Two_Factor_Totp' ), $available_providers );
// $this->assertSame( 'Two_Factor_Totp', Two_Factor_Core::get_most_secure_available_provider_for_user( $user->ID ) );


// // Enable Dummy and disable TOTP. Email should be more secure.
// Two_Factor_Core::enable_provider_for_user( $user->ID, 'Two_Factor_Dummy' );
// Two_Factor_Core::disable_provider_for_user( $user->ID, 'Two_Factor_Totp' );
// $available_providers = array_keys( Two_Factor_Core::get_available_providers_for_user( $user->ID ) );

// $this->assertSame( array( 'Two_Factor_Email', 'Two_Factor_Dummy'), $available_providers );
// $this->assertSame( 'Two_Factor_Email', Two_Factor_Core::get_most_secure_available_provider_for_user( $user->ID ) );
// }

/**
* Ensure that when a user enables two factor, that they are able to continue to change settings.
*
Expand Down

0 comments on commit 0ec9c1e

Please sign in to comment.