TwoFactorController.php 2.7 KB

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