From 748e3ea121ed007e308c5df4c7866a49ffef3341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Cort=C3=A9s=20Cano?= <38475681+TrapoSAMA@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:05:39 -0300 Subject: [PATCH] Refactor account TOTP action rendering and status queries (#1198) As suggested by @cboltz in #1188 (comment) and #1188 (comment), move TOTP action HTML out of both AdminHandler and MailboxHandler into a shared Smarty partial. --- DOCUMENTS/HANDLER_CLASSES.md | 1 + model/AdminHandler.php | 28 +++++---------- model/MailboxHandler.php | 28 +++++---------- templates/_totp-action.tpl | 5 +++ templates/editform.tpl | 4 ++- tests/TotpActionTest.php | 68 ++++++++++++++++++++++++++++++++++-- 6 files changed, 93 insertions(+), 41 deletions(-) create mode 100644 templates/_totp-action.tpl diff --git a/DOCUMENTS/HANDLER_CLASSES.md b/DOCUMENTS/HANDLER_CLASSES.md index a0dfa4b8..3898abc2 100644 --- a/DOCUMENTS/HANDLER_CLASSES.md +++ b/DOCUMENTS/HANDLER_CLASSES.md @@ -122,6 +122,7 @@ protected function initStruct() | `vnum` | Virtual numeric (not stored in DB) | | `vtxt` | Virtual text (not stored in DB) | | `quot` | Quota display | +| `totp` | Read-only TOTP action in account edit forms; the handler returns a boolean and the template renders it | This list covers the most common types. Additional types may exist in `PFAHandler` and individual Handler classes — check the source for the diff --git a/model/AdminHandler.php b/model/AdminHandler.php index 19792642..b61c28cc 100644 --- a/model/AdminHandler.php +++ b/model/AdminHandler.php @@ -91,13 +91,16 @@ class AdminHandler extends PFAHandler 'created' => self::pacol(0, 0, 0, 'ts', 'created' , ''), 'modified' => self::pacol(0, 0, 1, 'ts', 'last_modified' , ''), ); - # Admin-side TOTP management + # Compute status in the account query without exposing the secret or querying each row. if (Config::bool('totp') && !$this->new) { $this->struct['totp_action'] = self::pacol( - 0, 1, 0, 'html', 'pUsersMenu_totp', '', '', + 0, 1, 0, 'totp', 'pUsersMenu_totp', '', false, array(), - /*not_in_db*/ 1, - /*dont_write_to_db*/ 1 + /*not_in_db*/ 0, + /*dont_write_to_db*/ 1, + /*select*/ "CASE WHEN active = '" . db_get_boolean(true) . "' AND totp_secret IS NOT NULL " . + "AND LENGTH(totp_secret) > 0 AND NOT (LENGTH(totp_secret) = 1 AND totp_secret = '0') " . + "THEN 1 ELSE 0 END AS totp_action" ); } } @@ -196,21 +199,8 @@ class AdminHandler extends PFAHandler if ($row['superadmin']) { $db_result[$key]['domain_count'] = Config::lang('super_admin'); } - # Render TOTP action - if (isset($this->struct['totp_action']) && is_string($row['username'])) { - - $totp = new TotpPf('admin', new Login('admin')); - $has_totp = $totp->usesTOTP($row['username']); - - if ($has_totp) { - $db_result[$key]['totp_action'] = - ''; - } else { - $db_result[$key]['totp_action'] = - htmlspecialchars(Config::lang('pUsersMenu_no_totp_set'), ENT_QUOTES, 'UTF-8'); - } + if (isset($row['totp_action'])) { + $db_result[$key]['totp_action'] = (bool)$row['totp_action']; } } diff --git a/model/MailboxHandler.php b/model/MailboxHandler.php index 992a20ae..f2ae83df 100644 --- a/model/MailboxHandler.php +++ b/model/MailboxHandler.php @@ -66,13 +66,16 @@ class MailboxHandler extends PFAHandler # TODO: add virtual 'notified' column and allow to display who received a vacation response? ); - # Admin-side TOTP management + # Compute status in the account query without exposing the secret or querying each row. if (Config::bool('totp') && !$this->new) { $this->struct['totp_action'] = self::pacol( - 0, 1, 0, 'html', 'pUsersMenu_totp', '', '', + 0, 1, 0, 'totp', 'pUsersMenu_totp', '', false, array(), - /*not_in_db*/ 1, - /*dont_write_to_db*/ 1 + /*not_in_db*/ 0, + /*dont_write_to_db*/ 1, + /*select*/ "CASE WHEN active = '" . db_get_boolean(true) . "' AND totp_secret IS NOT NULL " . + "AND LENGTH(totp_secret) > 0 AND NOT (LENGTH(totp_secret) = 1 AND totp_secret = '0') " . + "THEN 1 ELSE 0 END AS totp_action" ); } @@ -260,21 +263,8 @@ class MailboxHandler extends PFAHandler $db_result[$key]['password_expiry'] = $password_expiration_never_label; } } - # Render TOTP action - if (isset($this->struct['totp_action']) && is_string($row['username'])) { - - $totp = new TotpPf('mailbox', new Login('mailbox')); - $has_totp = $totp->usesTOTP($row['username']); - - if ($has_totp) { - $db_result[$key]['totp_action'] = - ''; - } else { - $db_result[$key]['totp_action'] = - htmlspecialchars(Config::lang('pUsersMenu_no_totp_set'), ENT_QUOTES, 'UTF-8'); - } + if (isset($row['totp_action'])) { + $db_result[$key]['totp_action'] = (bool)$row['totp_action']; } } diff --git a/templates/_totp-action.tpl b/templates/_totp-action.tpl new file mode 100644 index 00000000..b2e820ce --- /dev/null +++ b/templates/_totp-action.tpl @@ -0,0 +1,5 @@ +{if $has_totp} + +{else} + {$PALANG.pUsersMenu_no_totp_set} +{/if} diff --git a/templates/editform.tpl b/templates/editform.tpl index 27577a8b..3bfa24be 100644 --- a/templates/editform.tpl +++ b/templates/editform.tpl @@ -17,7 +17,9 @@