From 3d6ade668f584f47f22fb76a19ae967a89892652 Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Mon, 16 Nov 2026 13:59:38 +0100 Subject: [PATCH] Add --json to polls list and polls show Machine-readable output for scripts; never contains the admin token unless --link is given. Co-Authored-By: Claude Sonnet 5.5 --- docs/deployment.md | 8 +++- docs/operations.md | 2 + src/Command/PollsCommand.php | 38 +++++++++++++++++++ src/Command/PollsListCommand.php | 10 +++++ src/Command/PollsShowCommand.php | 19 ++++++++++ tests/TestCase/Command/PollsCommandsTest.php | 40 ++++++++++++++++++++ 6 files changed, 115 insertions(+), 2 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index 2fa4812..7025085 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -125,8 +125,8 @@ und fragen nach, jede Eingabe hat eine sichere Vorgabe (Zurück/Nein). Für Skri | Befehl | Zweck | |---|---| | `bin/cake polls` | Menü (interaktiv) | -| `bin/cake polls list [--expired] [--email=text] [--sort=created\|activity\|title]` | Umfragen mit E-Mail, Ablaufdatum und Erinnerung | -| `bin/cake polls show [--link]` | Details; mit `--link` auch der geheime Admin-Link (nur auf Wunsch) | +| `bin/cake polls list [--expired] [--email=text] [--sort=created\|activity\|title] [--json]` | Umfragen mit E-Mail, Ablaufdatum und Erinnerung | +| `bin/cake polls show [--link] [--json]` | Details; mit `--link` auch der geheime Admin-Link (nur auf Wunsch) | | `bin/cake polls delete [--yes]` | Eine Umfrage samt Folien, Antworten und Bildern löschen (fragt nach) | | `bin/cake polls remind [--dry-run]` | Erinnerungsmails für bald ablaufende Umfragen (je Umfrage einmal) | | `bin/cake polls cleanup [--dry-run]` | Der Cronjob: erst erinnern, dann abgelaufene Umfragen löschen, danach verwaiste Bilddateien | @@ -137,6 +137,10 @@ einmalig eine Mail mit Ablaufdatum und Admin-Link, um die Ergebnisse als PDF zu der Anlege-Mail und im Dashboard (Karte „Sicherheit“). Es wird berechnet, eine Änderung der Einstellung gilt also sofort für alle Umfragen. +Mit `--json` geben `polls list` und `polls show` statt der Tabelle bzw. des Texts reines JSON aus (Datumswerte als +ISO 8601, ohne weitere Ausgabe), z. B. für `jq`: `bin/cake polls list --json --expired | jq '.[].email'`. Der Admin-Link +steht auch dort nur mit `--link` (`adminUrl`, sonst `null`). + ### Cronjob einrichten Ein einziger täglicher Cronjob erledigt Erinnern, Löschen und Bild-Aufräumen (`polls cleanup`). Ohne ihn läuft keine diff --git a/docs/operations.md b/docs/operations.md index 2e8de48..0cccbbb 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -15,6 +15,8 @@ Aufräumen und Erinnerungen, CI/CD, Release-Pakete, Container. „Cronjob einrichten“, im Container läuft kein cron). `last_admin_access` fließt nicht mehr ein. `polls show --link` ist die einzige Stelle, die den Admin-Link ausgibt (nie in `list`). Das Menü `bin/cake polls` (`PollsMenuCommand`) ruft nur diese Befehle per `executeCommand()` auf; jede Abfrage hat eine Vorgabe (`0`/leer/`n`), damit geschlossene Eingabe es beendet statt zu schleifen; Löschen/Aufräumen erst nach Vorschau (`--dry-run`) und Rückfrage. + `polls list|show --json` gibt nur JSON aus (`PollsCommand::pollData()`/`printJson()`, Ausgabemodus RAW, sonst würden `<…>` in Titeln als + Markup verschluckt; nie das Token, `adminUrl` nur mit `--link`). Tests: `PollsCommandsTest.php` (leert vorher alle Umfragen). ## Deployment diff --git a/src/Command/PollsCommand.php b/src/Command/PollsCommand.php index 6814da2..2c89302 100644 --- a/src/Command/PollsCommand.php +++ b/src/Command/PollsCommand.php @@ -7,6 +7,7 @@ use App\Model\Entity\Poll; use App\Model\Table\PollsTable; use Cake\Command\Command; use Cake\Console\ConsoleIo; +use Cake\Console\ConsoleOutput; use Cake\I18n\DateTime; use Cake\Routing\Router; @@ -26,6 +27,43 @@ abstract class PollsCommand extends Command return $polls; } + /** + * Machine-readable form of a poll (for `--json`). Never contains the admin token. + * + * @param \App\Model\Entity\Poll $poll Poll + * @return array Dates as ISO 8601 (server time zone), null where empty + */ + protected function pollData(Poll $poll): array + { + $date = fn(?DateTime $d): ?string => $d?->format(DATE_ATOM); + + return [ + 'id' => $poll->id, + 'title' => $poll->title, + 'email' => $poll->email, + 'joinCode' => $poll->join_code, + 'joinUrl' => $this->joinLink($poll), + 'created' => $date($poll->created), + 'lastAdminAccess' => $date($poll->last_admin_access), + 'expiresAt' => $date($poll->expiresAt()), + 'expired' => $poll->isExpired(), + 'reminderSentAt' => $date($poll->expiry_reminded_at), + ]; + } + + /** + * Prints data as JSON only: nothing else on stdout, no markup interpretation (titles may contain "<"). + * + * @param \Cake\Console\ConsoleIo $io Console + * @param mixed $data JSON serialisable data + * @return void + */ + protected function printJson(ConsoleIo $io, mixed $data): void + { + $io->setOutputAs(ConsoleOutput::RAW); + $io->out((string)json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); + } + /** * Finds a poll by numeric id or join code ("K7P2QX", "k7p-2qx"). * diff --git a/src/Command/PollsListCommand.php b/src/Command/PollsListCommand.php index 4878d32..59cc4ac 100644 --- a/src/Command/PollsListCommand.php +++ b/src/Command/PollsListCommand.php @@ -37,6 +37,10 @@ class PollsListCommand extends PollsCommand ->addOption('email', [ 'help' => __('Nur Umfragen, deren E-Mail-Adresse diesen Text enthält.'), ]) + ->addOption('json', [ + 'help' => __('Ausgabe als JSON statt als Tabelle (für Skripte).'), + 'boolean' => true, + ]) ->addOption('sort', [ 'help' => __('Sortierung (aufsteigend).'), 'default' => 'created', @@ -75,6 +79,12 @@ class PollsListCommand extends PollsCommand }); } + if ($args->getOption('json')) { + $this->printJson($io, array_map($this->pollData(...), $polls)); + + return static::CODE_SUCCESS; + } + if ($polls === []) { $io->out(__('Keine Umfragen gefunden.')); diff --git a/src/Command/PollsShowCommand.php b/src/Command/PollsShowCommand.php index 00cec45..8154769 100644 --- a/src/Command/PollsShowCommand.php +++ b/src/Command/PollsShowCommand.php @@ -34,6 +34,10 @@ class PollsShowCommand extends PollsCommand 'help' => __('ID oder Beitrittscode der Umfrage.'), 'required' => true, ]) + ->addOption('json', [ + 'help' => __('Ausgabe als JSON statt als Text (für Skripte).'), + 'boolean' => true, + ]) ->addOption('link', [ 'help' => __('Auch den Admin-Link ausgeben (enthält das geheime Token!).'), 'boolean' => true, @@ -55,6 +59,21 @@ class PollsShowCommand extends PollsCommand } $slides = $this->fetchTable('Slides')->find()->where(['poll_id' => $poll->id])->count(); + if ($args->getOption('json')) { + $data = $this->pollData($poll) + ['slides' => $slides, 'adminUrl' => null]; + if ($args->getOption('link')) { + $data['adminUrl'] = $this->adminLink($poll); + if ($data['adminUrl'] === null) { + $io->err(__('Der Admin-Link lässt sich nicht entschlüsseln (SECURITY_SALT geändert?).')); + + return static::CODE_ERROR; + } + } + $this->printJson($io, $data); + + return static::CODE_SUCCESS; + } + $io->out([ __('ID: {0}', $poll->id), __('Titel: {0}', $poll->title), diff --git a/tests/TestCase/Command/PollsCommandsTest.php b/tests/TestCase/Command/PollsCommandsTest.php index ced826d..fff5c77 100644 --- a/tests/TestCase/Command/PollsCommandsTest.php +++ b/tests/TestCase/Command/PollsCommandsTest.php @@ -103,6 +103,46 @@ class PollsCommandsTest extends TestCase $this->assertExitError(); } + public function testListAndShowAsJson(): void + { + [$old, $token] = $this->createPoll('Alt &', 'alt@example.org'); + $this->createPoll('Neu', 'neu@example.org'); + $this->age($old->id, 120); + + $this->exec('polls list --json'); + $this->assertExitSuccess(); + $rows = json_decode(implode(" +", $this->_out->messages()), true, flags: JSON_THROW_ON_ERROR); + $this->assertCount(2, $rows); + $this->assertSame('Alt &', $rows[0]['title']); + $this->assertSame('alt@example.org', $rows[0]['email']); + $this->assertTrue($rows[0]['expired']); + $this->assertFalse($rows[1]['expired']); + $this->assertNull($rows[1]['reminderSentAt']); + $this->assertArrayNotHasKey('adminUrl', $rows[0]); + $this->assertStringNotContainsString($token, implode('', $this->_out->messages())); + + $this->exec('polls list --json --expired'); + $this->assertCount(1, json_decode(implode(" +", $this->_out->messages()), true)); + + $this->exec('polls list --json --email=gibtsnicht'); + $this->assertSame([], json_decode(implode(" +", $this->_out->messages()), true)); + + $this->exec('polls show ' . $old->id . ' --json'); + $data = json_decode(implode(" +", $this->_out->messages()), true, flags: JSON_THROW_ON_ERROR); + $this->assertSame($old->id, $data['id']); + $this->assertNull($data['adminUrl']); + $this->assertStringNotContainsString($token, implode('', $this->_out->messages())); + + $this->exec('polls show ' . $old->id . ' --json --link'); + $data = json_decode(implode(" +", $this->_out->messages()), true, flags: JSON_THROW_ON_ERROR); + $this->assertSame('https://plenario.test/admin/' . $token, $data['adminUrl']); + } + public function testDeleteRemovesPollWithSlides(): void { [$poll] = $this->createPoll();