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.
This commit is contained in:
Rodrigo Cortés Cano authored and GitHub committed 2026-11-18 13:05:39 +00:00
1 parent fda66026ca
commit 748e3ea121
6 files changed
+93 -41

No files matched your search

+1
View File
@@ -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
+9 -19
View File
@@ -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'] =
'<button type="submit" name="reset_totp" value="1" class="btn btn-warning btn-sm">' .
htmlspecialchars(Config::lang('pUsersMenu_reset_totp'), ENT_QUOTES, 'UTF-8') .
'</button>';
} 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'];
}
}
+9 -19
View File
@@ -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'] =
'<button type="submit" name="reset_totp" value="1" class="btn btn-warning btn-sm">' .
htmlspecialchars(Config::lang('pUsersMenu_reset_totp'), ENT_QUOTES, 'UTF-8') .
'</button>';
} 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'];
}
}
+5
View File
@@ -0,0 +1,5 @@
{if $has_totp}
<button type="submit" name="reset_totp" value="1" class="btn btn-warning btn-sm">{$PALANG.pUsersMenu_reset_totp}</button>
{else}
{$PALANG.pUsersMenu_no_totp_set}
{/if}
+3 -1
View File
@@ -17,7 +17,9 @@
<label class="col-md-4" for="{$key}">{$field.label}</label>
<div class="col-md-6 col-sm-8">
{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}}
+66 -2
View File
@@ -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 => '<input type="hidden" name="token" value="fixture">');
$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'] = '<Reset & confirm>';
$PALANG['pUsersMenu_no_totp_set'] = '<Not configured & safe>';
$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('&lt;Reset &amp; confirm&gt;', $html);
} else {
$this->assertStringNotContainsString('name="reset_totp"', $html);
$this->assertStringContainsString('&lt;Not configured &amp; safe&gt;', $html);
}
$this->assertStringNotContainsString('<Reset', $html);
}
}
}
}