Slide list actions (hide, delete, reset, duplicate, go live, pause) now run via fetch and refresh only the changed regions, keeping scroll position and focus. Add/edit slide forms open in a dialog. Admin pages answer with a slim fragment layout when the X-Fragment header is sent. Replace postLink confirms (inline onclick, blocked by the CSP) with postButton and data-confirm handled in plenario.js. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
100 lines
3.0 KiB
PHP
100 lines
3.0 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);
|
|
// plenario.js asks for a fragment to update the open page without reloading it.
|
|
if ($this->request->getHeaderLine('X-Fragment') !== '') {
|
|
$this->viewBuilder()->setLayout('fragment');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
}
|