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 @@
{if $field.editable == 0} - {if $field.type == 'enma'} + {if $field.type == 'totp'} + {include file="_totp-action.tpl" has_totp=$value_{$key}} + {elseif $field.type == 'enma'} {$struct.{$key}.options.{$value_{$key}}} {else} {$value_{$key}} diff --git a/tests/TotpActionTest.php b/tests/TotpActionTest.php index 4ac8074a..b17be84a 100644 --- a/tests/TotpActionTest.php +++ b/tests/TotpActionTest.php @@ -68,7 +68,71 @@ class TotpActionTest extends TestCase $enabled = $rows['enabled@' . self::DOMAIN]; $plain = $rows['plain@' . self::DOMAIN]; $this->assertArrayHasKey('totp_action', $enabled); - $this->assertStringContainsString('name="reset_totp"', $enabled['totp_action']); - $this->assertSame(htmlspecialchars(Config::lang('pUsersMenu_no_totp_set'), ENT_QUOTES, 'UTF-8'), $plain['totp_action']); + $this->assertTrue($enabled['totp_action']); + $this->assertArrayNotHasKey('totp_secret', $enabled); + $this->assertFalse($plain['totp_action']); } + #[DataProvider('handlers')] + public function testInactiveAndEmptySecretsDoNotOfferReset(string $handlerClass): void + { + $table = $handlerClass === AdminHandler::class ? 'admin' : 'mailbox'; + $handler = new $handlerClass(); + $username = 'enabled@' . self::DOMAIN; + foreach ([null, '', '0'] as $secret) { + db_update($table, 'username', $username, ['totp_secret' => $secret]); + $this->assertTrue($handler->getList(['username' => $username])); + $this->assertFalse($handler->result()[$username]['totp_action']); + } + db_update($table, 'username', $username, ['totp_secret' => 'JBSWY3DPEHPK3PXP', 'active' => false]); + $this->assertTrue($handler->getList(['username' => $username])); + $this->assertFalse($handler->result()[$username]['totp_action']); + } + + #[DataProvider('handlers')] + public function testDisabledTotpOmitsAction(string $handlerClass): void + { + global $CONF; + Config::write('totp', 'NO'); + $CONF['totp'] = 'NO'; + $handler = new $handlerClass(); + $username = 'enabled@' . self::DOMAIN; + $this->assertTrue($handler->getList(['username' => $username])); + $this->assertArrayNotHasKey('totp_action', $handler->result()[$username]); + } + + #[DataProvider('handlers')] + public function testEditTemplateRendersBooleanAction(string $handlerClass): void + { + $smarty = new \Smarty\Smarty(); + $smarty->setCompileDir(sys_get_temp_dir()); + $smarty->setTemplateDir(__DIR__ . '/../templates'); + $smarty->registerPlugin('function', 'CSRF_Token', static fn (): string => ''); + $smarty->assign([ + 'table' => $handlerClass === AdminHandler::class ? 'admin' : 'mailbox', + 'formtitle' => 'Fixture', 'submitbutton' => 'Save', 'cancel_url' => 'list.php', + 'fielderror' => ['totp_action' => ''], + 'struct' => ['totp_action' => PFAHandler::pacol(0, 1, 0, 'totp', '', '')], + ]); + foreach (['en', 'es'] as $language) { + $PALANG = []; + require __DIR__ . '/../languages/' . $language . '.lang'; + $PALANG['pUsersMenu_reset_totp'] = ''; + $PALANG['pUsersMenu_no_totp_set'] = ''; + $smarty->assign('PALANG', PFASmarty::getInstance()->sanitise($PALANG)); + foreach ([true, false] as $hasTotp) { + $smarty->assign('value_totp_action', $hasTotp); + $html = $smarty->fetch('editform.tpl'); + $this->assertStringContainsString('name="token"', $html); + if ($hasTotp) { + $this->assertStringContainsString('name="reset_totp" value="1"', $html); + $this->assertStringContainsString('<Reset & confirm>', $html); + } else { + $this->assertStringNotContainsString('name="reset_totp"', $html); + $this->assertStringContainsString('<Not configured & safe>', $html); + } + $this->assertStringNotContainsString('