diff --git a/libraries/classes/SecondFactor.php b/libraries/classes/SecondFactor.php index cb24d31cc3..cb6d79b0de 100644 --- a/libraries/classes/SecondFactor.php +++ b/libraries/classes/SecondFactor.php @@ -215,4 +215,24 @@ class SecondFactor } return true; } + + /** + * Returns array with all available backends + * + * @return array + */ + public function getAllBackends() + { + $all = array_merge([''], $this->available); + $backends = []; + foreach ($all as $name) { + $cls = $this->getBackendClass($name); + $backends[] = [ + 'id' => $cls::$id, + 'name' => $cls::getName(), + 'description' => $cls::getDescription(), + ]; + } + return $backends; + } } diff --git a/prefs_second.php b/prefs_second.php index 27e626a387..d6ade2de04 100644 --- a/prefs_second.php +++ b/prefs_second.php @@ -40,18 +40,6 @@ if (isset($_POST['2fa_remove'])) { } } -$all = array_merge([''], $second_factor->available); -$backends = []; -foreach ($all as $name) { - $cls = $second_factor->getBackendClass($name); - $backends[] = [ - 'id' => $cls::$id, - 'name' => $cls::getName(), - 'description' => $cls::getDescription(), - ]; -} - - $backend = $second_factor->backend; echo Template::get('prefs_second')->render([ 'enabled' => $second_factor->writable, @@ -59,5 +47,5 @@ echo Template::get('prefs_second')->render([ 'backend_id' => $backend::$id, 'backend_name' => $backend::getName(), 'backend_description' => $backend::getDescription(), - 'backends' => $backends, + 'backends' => $second_factor->getAllBackends(), ]); diff --git a/test/classes/SecondFactorTest.php b/test/classes/SecondFactorTest.php index 2b5ac768f8..93bfcb1eac 100644 --- a/test/classes/SecondFactorTest.php +++ b/test/classes/SecondFactorTest.php @@ -133,4 +133,19 @@ class SecondFactorTest extends PmaTestCase $_POST['u2f_registration_response'] = 'invalid'; $this->assertFalse($object->configure('key')); } + + /** + * Test listing of available backends. + */ + public function testBackends() + { + $GLOBALS['cfg']['DBG']['simple2fa'] = true; + $object = new SecondFactor('user'); + $backends = $object->getAllBackends(); + $this->assertEquals( + count($object->available) + 1, + count($backends) + ); + $GLOBALS['cfg']['DBG']['simple2fa'] = false; + } }