TwoFactorController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Preferences;
  4. use PhpMyAdmin\Controllers\AbstractController;
  5. use PhpMyAdmin\Message;
  6. use PhpMyAdmin\Relation;
  7. use PhpMyAdmin\Response;
  8. use PhpMyAdmin\Template;
  9. use PhpMyAdmin\TwoFactor;
  10. use function count;
  11. class TwoFactorController extends AbstractController
  12. {
  13. /** @var Relation */
  14. private $relation;
  15. /**
  16. * @param Response $response
  17. */
  18. public function __construct($response, Template $template, Relation $relation)
  19. {
  20. parent::__construct($response, $template);
  21. $this->relation = $relation;
  22. }
  23. public function index(): void
  24. {
  25. global $cfg, $route;
  26. $cfgRelation = $this->relation->getRelationsParam();
  27. echo $this->template->render('preferences/header', [
  28. 'route' => $route,
  29. 'is_saved' => ! empty($_GET['saved']),
  30. 'has_config_storage' => $cfgRelation['userconfigwork'],
  31. ]);
  32. $twoFactor = new TwoFactor($cfg['Server']['user']);
  33. if (isset($_POST['2fa_remove'])) {
  34. if (! $twoFactor->check(true)) {
  35. echo $this->template->render('preferences/two_factor/confirm', [
  36. 'form' => $twoFactor->render(),
  37. ]);
  38. return;
  39. }
  40. $twoFactor->configure('');
  41. echo Message::rawNotice(__('Two-factor authentication has been removed.'))->getDisplay();
  42. } elseif (isset($_POST['2fa_configure'])) {
  43. if (! $twoFactor->configure($_POST['2fa_configure'])) {
  44. echo $this->template->render('preferences/two_factor/configure', [
  45. 'form' => $twoFactor->setup(),
  46. 'configure' => $_POST['2fa_configure'],
  47. ]);
  48. return;
  49. }
  50. echo Message::rawNotice(__('Two-factor authentication has been configured.'))->getDisplay();
  51. }
  52. $backend = $twoFactor->getBackend();
  53. echo $this->template->render('preferences/two_factor/main', [
  54. 'enabled' => $twoFactor->isWritable(),
  55. 'num_backends' => count($twoFactor->getAvailable()),
  56. 'backend_id' => $backend::$id,
  57. 'backend_name' => $backend::getName(),
  58. 'backend_description' => $backend::getDescription(),
  59. 'backends' => $twoFactor->getAllBackends(),
  60. 'missing' => $twoFactor->getMissingDeps(),
  61. ]);
  62. }
  63. }