Share repeated controller and service code
CI / Deploy / test (push) Successful in 53s
CI / Deploy / deploy (push) Skipped

- AppController: joinUrl(), adminUrlFor(), setPollInterval() and protect()
  instead of copies in Admin-, Home-, Participate- and EmbedController.
- PollMailer::sendSafely() replaces two try/catch blocks around mail sending.
- Slide::optionIds() replaces five identical array_map() calls.
- PollStateService: one pollPayload() for presenter and embed; the vote
  action passes the already loaded slide instead of loading it again.
- ResultsService::forSlide() takes the answer/comment limit and the rating
  flag, so ReportService no longer queries answers, comments and the scale
  distribution twice and the export loads the slides only once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-08 22:50:56 +01:00
1 parent ad537b9b34
commit 3e44ded107
14 files changed
+189 -137

No files matched your search

-12
View File
@@ -73,18 +73,6 @@ abstract class AdminAppController extends AppController
$this->response = $this->protect($this->response);
}
/**
* @param \Cake\Http\Response $response Response
* @return \Cake\Http\Response
*/
protected function protect(Response $response): Response
{
return $response
->withHeader('Referrer-Policy', 'no-referrer')
->withHeader('X-Robots-Tag', 'noindex, nofollow')
->withHeader('Cache-Control', 'no-store, private');
}
/**
* @inheritDoc
*/
+10 -16
View File
@@ -13,16 +13,13 @@ use App\Service\ReportService;
use App\Service\ResetService;
use App\Service\SlideControlService;
use App\View\PdfView;
use Cake\Core\Configure;
use Cake\Http\Exception\BadRequestException;
use Cake\Http\Response;
use Cake\I18n\DateTime;
use Cake\Log\Log;
use Cake\Routing\Router;
use chillerlan\QRCode\Output\QRMarkupSVG;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use Throwable;
/**
* Poll level admin actions: dashboard, settings, presenter mode, export.
@@ -58,7 +55,7 @@ class AdminController extends AdminAppController
$responseCounts = $this->Responses->countParticipantsPerSlide($this->poll->id);
$typeLabels = Slide::typeLabels();
$typeDescriptions = Slide::typeDescriptions();
$joinUrl = Router::url(['_name' => 'participate', 'code' => $this->poll->join_code], true);
$joinUrl = $this->joinUrl($this->poll);
// Created on first visit, so every slide has its embed URL ready to copy.
$embedKey = $this->Polls->ensureEmbedKey($this->poll);
@@ -95,13 +92,10 @@ class AdminController extends AdminAppController
$this->Polls->saveOrFail($this->poll);
$this->token = $token;
$adminUrl = Router::url($this->adminUrl(), true);
$joinUrl = Router::url(['_name' => 'participate', 'code' => $this->poll->join_code], true);
try {
(new PollMailer())->send('adminLink', [$this->poll, $adminUrl, $joinUrl]);
$sent = (new PollMailer())->sendSafely('adminLink', [$this->poll, $this->adminUrlFor($token), $this->joinUrl($this->poll)]);
if ($sent) {
$this->Flash->success(__('Neuer Admin-Link erstellt und per E-Mail verschickt. Der alte Link ist ab sofort ungültig – speichere dir diese Seite als Lesezeichen.'));
} catch (Throwable $e) {
Log::error('Mail delivery failed: ' . $e->getMessage());
} else {
$this->Flash->warning(__('Neuer Admin-Link erstellt, die E-Mail konnte aber nicht verschickt werden. Speichere dir diese Seite unbedingt als Lesezeichen!'));
}
@@ -177,9 +171,9 @@ class AdminController extends AdminAppController
}
$this->viewBuilder()->setLayout('present');
$this->set('joinUrl', Router::url(['_name' => 'participate', 'code' => $this->poll->join_code], true));
$this->set('joinUrl', $this->joinUrl($this->poll));
$this->set('joinBaseUrl', Router::url(['_name' => 'join:short'], true));
$this->set('pollIntervalMs', (int)Configure::read('Plenario.pollIntervalMs', 2000));
$this->setPollInterval();
}
/**
@@ -275,7 +269,7 @@ class AdminController extends AdminAppController
*/
public function qr(): Response
{
$url = Router::url(['_name' => 'participate', 'code' => $this->poll->join_code], true);
$url = $this->joinUrl($this->poll);
$options = new QROptions([
'outputInterface' => QRMarkupSVG::class,
'outputBase64' => false,
@@ -301,13 +295,13 @@ class AdminController extends AdminAppController
*/
public function export(): Response
{
$report = new ReportService();
$hasInternal = $report->hasInternalContent($this->poll);
$sections = (new ReportService())->build($this->poll, $this->request->getQuery('internal') === '1');
$hasInternal = ReportService::hasInternal($sections);
$includeInternal = $hasInternal && $this->request->getQuery('internal') === '1';
$this->viewBuilder()->setClassName(PdfView::class);
$this->set([
'sections' => $report->build($this->poll, $includeInternal),
'sections' => $sections,
'includeInternal' => $includeInternal,
'hasInternal' => $hasInternal,
'exportedAt' => DateTime::now(),
+49
View File
@@ -3,10 +3,12 @@ declare(strict_types=1);
namespace App\Controller;
use App\Model\Entity\Poll;
use App\Service\ThrottleService;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Http\Response;
use Cake\Routing\Router;
/**
* Base controller of PlenarIO. Security headers (CSP, framing, nosniff) are set for every
@@ -24,6 +26,53 @@ class AppController extends Controller
$this->loadComponent('Flash');
}
/**
* Pages whose URL contains a secret (admin token, embed key): never send it as referrer,
* never index them, never cache them.
*
* @param \Cake\Http\Response $response Response
* @return \Cake\Http\Response
*/
protected function protect(Response $response): Response
{
return $response
->withHeader('Referrer-Policy', 'no-referrer')
->withHeader('X-Robots-Tag', 'noindex, nofollow')
->withHeader('Cache-Control', 'no-store, private');
}
/**
* Absolute participant URL of a poll (QR code, mails, presenter).
*
* @param \App\Model\Entity\Poll $poll Poll
* @return string
*/
protected function joinUrl(Poll $poll): string
{
return Router::url(['_name' => 'participate', 'code' => $poll->join_code], true);
}
/**
* Absolute admin URL for a plain token (only for mails and the confirmation page).
*
* @param string $token Plain admin token
* @return string
*/
protected function adminUrlFor(string $token): string
{
return Router::url(['_name' => 'admin', 'token' => $token], true);
}
/**
* Passes the polling interval of live pages to the view (`pollIntervalMs`).
*
* @return void
*/
protected function setPollInterval(): void
{
$this->set('pollIntervalMs', (int)Configure::read('Plenario.pollIntervalMs', 2000));
}
/**
* Registers a hit of the client IP in a throttle bucket (see ThrottleService).
*
+1 -13
View File
@@ -101,7 +101,7 @@ class EmbedController extends AppController
$this->set('slide', $this->slide);
$this->set('urls', $urls);
$this->set('joinBaseUrl', Router::url(['_name' => 'join:short'], true));
$this->set('pollIntervalMs', (int)Configure::read('Plenario.pollIntervalMs', 2000));
$this->setPollInterval();
$this->set('heartbeatMs', max(1, (int)Configure::read('Plenario.embed.heartbeatSeconds', 5)) * 1000);
}
@@ -188,18 +188,6 @@ class EmbedController extends AppController
return $this->protect($this->json((new PollStateService())->forEmbed($this->poll, $this->slide)));
}
/**
* @param \Cake\Http\Response $response Response
* @return \Cake\Http\Response
*/
protected function protect(Response $response): Response
{
return $response
->withHeader('Referrer-Policy', 'no-referrer')
->withHeader('X-Robots-Tag', 'noindex, nofollow')
->withHeader('Cache-Control', 'no-store, private');
}
/**
* @param string $name Route name
* @return array<string, mixed>
+4 -27
View File
@@ -9,9 +9,6 @@ use App\Model\Entity\Slide;
use App\Model\Table\PollsTable;
use Cake\Core\Configure;
use Cake\Http\Response;
use Cake\Log\Log;
use Cake\Routing\Router;
use Throwable;
/**
* Public pages: start page, poll creation, joining by code, link recovery.
@@ -82,9 +79,8 @@ class HomeController extends AppController
} elseif (!$poll->hasErrors()) {
[$poll, $token] = $this->Polls->insertPoll($poll);
if ($token !== null) {
$adminUrl = Router::url(['_name' => 'admin', 'token' => $token], true);
$joinUrl = Router::url(['_name' => 'participate', 'code' => $poll->join_code], true);
$sent = $this->sendMail(fn(PollMailer $mailer) => $mailer->send('adminLink', [$poll, $adminUrl, $joinUrl]));
$adminUrl = $this->adminUrlFor($token);
$sent = (new PollMailer())->sendSafely('adminLink', [$poll, $adminUrl, $this->joinUrl($poll)]);
$session = $this->request->getSession();
$session->write('Plenario.created', [
@@ -218,31 +214,12 @@ class HomeController extends AppController
}
$links[] = [
'title' => $poll->title,
'url' => Router::url(['_name' => 'admin', 'token' => $token], true),
'url' => $this->adminUrlFor($token),
'created' => $poll->created->i18nFormat('dd.MM.yyyy HH:mm'),
];
}
if ($links !== []) {
$this->sendMail(fn(PollMailer $mailer) => $mailer->send('recovery', [$email, $links]));
}
}
/**
* Sends a mail and logs (instead of throwing) transport errors.
*
* @param callable(\App\Mailer\PollMailer): mixed $send Callback
* @return bool
*/
protected function sendMail(callable $send): bool
{
try {
$send(new PollMailer());
return true;
} catch (Throwable $e) {
Log::error('Mail delivery failed: ' . $e->getMessage());
return false;
(new PollMailer())->sendSafely('recovery', [$email, $links]);
}
}
}
+3 -4
View File
@@ -8,7 +8,6 @@ use App\Model\Table\PollsTable;
use App\Service\PollStateService;
use App\Service\VoteException;
use App\Service\VoteService;
use Cake\Core\Configure;
use Cake\Event\EventInterface;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Exception\NotFoundException;
@@ -86,10 +85,10 @@ class ParticipateController extends AppController
$stateService = new PollStateService();
$slide = $poll->is_active ? $stateService->currentSlide($poll) : null;
$participantHash = $this->participantHash($poll);
$state = $stateService->forParticipant($poll, $participantHash);
$state = $stateService->forParticipant($poll, $participantHash, $slide);
$this->set(compact('poll', 'slide', 'state', 'participantHash'));
$this->set('pollIntervalMs', (int)Configure::read('Plenario.pollIntervalMs', 2000));
$this->setPollInterval();
}
/**
@@ -146,7 +145,7 @@ class ParticipateController extends AppController
return $this->json([
'ok' => $ok,
'message' => $message,
'state' => $stateService->forParticipant($poll, $this->participantHash($poll)),
'state' => $stateService->forParticipant($poll, $this->participantHash($poll), $slide),
], $status);
}
$ok ? $this->Flash->success($message) : $this->Flash->error($message);