Files
Plenario/src/Controller/AdminAppController.php
T
gnilebeinandClaude Opus 5.5 eb319f8c1e
CI / Deploy / test (push) Successful in 49s
CI / Deploy / deploy (push) Failing after 5s
Add initial Plenario live polling app
CakePHP 5.4 app for live polls: create via email without login, manage
via secret admin link, join via code. Includes slide types (choice,
wordcloud, open, scale, content), presenter mode, CSV export, captcha,
throttling, tests and Gitea CI/deploy workflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 13:24:29 +01:00

108 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller;
use App\Model\Entity\Poll;
use App\Model\Table\PollsTable;
use Cake\Event\EventInterface;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\Response;
use Cake\I18n\DateTime;
/**
* Base class for everything under /admin/{token}.
*
* "Authentication" = possession of the secret token. The poll is resolved via
* the sha256 hash of the token in beforeFilter(); unknown tokens yield a 404.
* Subclasses use $this->poll and $this->token.
*/
abstract class AdminAppController extends AppController
{
protected PollsTable $Polls;
protected Poll $poll;
protected string $token;
/**
* @return void
*/
public function initialize(): void
{
parent::initialize();
/** @var \App\Model\Table\PollsTable $polls */
$polls = $this->fetchTable('Polls');
$this->Polls = $polls;
}
/**
* @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event
* @return void
*/
public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);
$this->token = (string)$this->request->getParam('token');
/** @var \App\Model\Entity\Poll|null $poll */
$poll = $this->Polls->find('byAdminToken', token: $this->token)->first();
if ($poll === null) {
throw new NotFoundException(__('Diese Admin-URL ist ungültig.'));
}
$this->poll = $poll;
// Track usage at most every 10 minutes to avoid a write per request.
if ($poll->last_admin_access === null || $poll->last_admin_access->wasWithinLast('10 minutes') === false) {
$this->Polls->updateAll(['last_admin_access' => DateTime::now()], ['id' => $poll->id]);
}
$this->set('poll', $poll);
$this->set('adminToken', $this->token);
}
/**
* The token is part of the URL: never send it as referrer, never index it, never cache it.
*
* @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event
* @return void
*/
public function beforeRender(EventInterface $event): void
{
parent::beforeRender($event);
$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
*/
protected function json(array $data, int $status = 200): Response
{
return $this->protect(parent::json($data, $status));
}
/**
* URL array for a named admin route of the current poll.
*
* @param string $name Route name
* @param array<string, mixed> $params Extra params
* @return array<string, mixed>
*/
protected function adminUrl(string $name = 'admin', array $params = []): array
{
return ['_name' => $name, 'token' => $this->token] + $params;
}
}