Share and test code for listing backends

Signed-off-by: Michal Čihař <michal@cihar.com>
This commit is contained in:
Michal Čihař 2017-11-01 14:33:20 +01:00
parent dd3154b103
commit 0d14ba065c
3 changed files with 36 additions and 13 deletions

View File

@ -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;
}
}

View File

@ -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(),
]);

View File

@ -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;
}
}