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>
This commit is contained in:
commit
eb319f8c1e
120 files changed
+15572
No files matched your search
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model\Table;
|
||||
|
||||
use App\Model\Entity\Poll;
|
||||
use Cake\ORM\Query\SelectQuery;
|
||||
use Cake\ORM\RulesChecker;
|
||||
use Cake\ORM\Table;
|
||||
use Cake\Utility\Security;
|
||||
use Cake\Validation\Validator;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Polls ("presentations"). Access control works exclusively via the secret
|
||||
* admin token - there are no user accounts.
|
||||
*
|
||||
* @property \App\Model\Table\SlidesTable&\Cake\ORM\Association\HasMany $Slides
|
||||
* @method \App\Model\Entity\Poll newEmptyEntity()
|
||||
* @method \App\Model\Entity\Poll get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
|
||||
* @method \App\Model\Entity\Poll saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
|
||||
*/
|
||||
class PollsTable extends Table
|
||||
{
|
||||
/**
|
||||
* Characters used for join codes (no 0/O, 1/I/L to avoid confusion).
|
||||
*/
|
||||
public const JOIN_CODE_ALPHABET = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789';
|
||||
|
||||
public const JOIN_CODE_LENGTH = 6;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $config The configuration for the Table.
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(array $config): void
|
||||
{
|
||||
parent::initialize($config);
|
||||
|
||||
$this->setTable('polls');
|
||||
$this->setDisplayField('title');
|
||||
$this->setPrimaryKey('id');
|
||||
|
||||
$this->addBehavior('Timestamp');
|
||||
|
||||
$this->hasMany('Slides', [
|
||||
'foreignKey' => 'poll_id',
|
||||
'sort' => ['Slides.position' => 'ASC', 'Slides.id' => 'ASC'],
|
||||
'dependent' => true,
|
||||
'cascadeCallbacks' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Validation\Validator $validator Validator instance.
|
||||
* @return \Cake\Validation\Validator
|
||||
*/
|
||||
public function validationDefault(Validator $validator): Validator
|
||||
{
|
||||
$validator
|
||||
->scalar('title')
|
||||
->maxLength('title', 200, __('Der Titel darf maximal 200 Zeichen lang sein.'))
|
||||
->requirePresence('title', 'create')
|
||||
->notEmptyString('title', __('Bitte gib einen Titel ein.'));
|
||||
|
||||
$validator
|
||||
->email('email', false, __('Bitte gib eine gültige E-Mail-Adresse ein.'))
|
||||
->maxLength('email', 255)
|
||||
->requirePresence('email', 'create')
|
||||
->notEmptyString('email', __('Bitte gib deine E-Mail-Adresse ein.'));
|
||||
|
||||
$validator->boolean('is_active');
|
||||
$validator->boolean('show_results_to_participants');
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
|
||||
* @return \Cake\ORM\RulesChecker
|
||||
*/
|
||||
public function buildRules(RulesChecker $rules): RulesChecker
|
||||
{
|
||||
$rules->add($rules->isUnique(['join_code']), ['errorField' => 'join_code']);
|
||||
$rules->add($rules->isUnique(['admin_token_hash']), ['errorField' => 'admin_token_hash']);
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new admin token: URL safe, 256 bit entropy.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function generateToken(): string
|
||||
{
|
||||
return rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $token Plain admin token
|
||||
* @return string sha256 hex digest
|
||||
*/
|
||||
public static function hashToken(string $token): string
|
||||
{
|
||||
return hash('sha256', $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a fresh admin token to the poll (hash + encrypted copy).
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll entity
|
||||
* @return string The plain token (only ever shown in the admin URL / mail)
|
||||
*/
|
||||
public function assignNewToken(Poll $poll): string
|
||||
{
|
||||
$token = static::generateToken();
|
||||
$poll->admin_token_hash = static::hashToken($token);
|
||||
$poll->admin_token_encrypted = base64_encode(Security::encrypt($token, static::encryptionKey()));
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts the stored admin token (used by the recovery mail).
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll entity
|
||||
* @return string|null
|
||||
*/
|
||||
public function decryptToken(Poll $poll): ?string
|
||||
{
|
||||
$raw = base64_decode((string)$poll->admin_token_encrypted, true);
|
||||
if ($raw === false) {
|
||||
return null;
|
||||
}
|
||||
$token = Security::decrypt($raw, static::encryptionKey());
|
||||
if ($token === null || static::hashToken($token) !== $poll->admin_token_hash) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string 32 byte key derived from Security.salt
|
||||
*/
|
||||
protected static function encryptionKey(): string
|
||||
{
|
||||
return hash('sha256', 'plenario-admin-token|' . Security::getSalt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a join code that is not yet in use.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generateJoinCode(): string
|
||||
{
|
||||
$alphabet = self::JOIN_CODE_ALPHABET;
|
||||
$max = strlen($alphabet) - 1;
|
||||
for ($attempt = 0; $attempt < 50; $attempt++) {
|
||||
$code = '';
|
||||
for ($i = 0; $i < self::JOIN_CODE_LENGTH; $i++) {
|
||||
$code .= $alphabet[random_int(0, $max)];
|
||||
}
|
||||
if (!$this->exists(['join_code' => $code])) {
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException('Could not generate a unique join code.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalises user input like " k7p-2qx " to "K7P2QX".
|
||||
*
|
||||
* @param string $code Raw user input
|
||||
* @return string
|
||||
*/
|
||||
public static function normalizeJoinCode(string $code): string
|
||||
{
|
||||
return (string)preg_replace('/[^A-Z0-9]/', '', strtoupper($code));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and persists a new poll.
|
||||
*
|
||||
* @param array<string, mixed> $data title + email
|
||||
* @return array{0: \App\Model\Entity\Poll, 1: string|null} The entity (check getErrors()) and the plain token on success
|
||||
*/
|
||||
public function createPoll(array $data): array
|
||||
{
|
||||
$poll = $this->newEntity([
|
||||
'title' => trim((string)($data['title'] ?? '')),
|
||||
'email' => strtolower(trim((string)($data['email'] ?? ''))),
|
||||
], ['fields' => ['title', 'email']]);
|
||||
|
||||
if ($poll->hasErrors()) {
|
||||
return [$poll, null];
|
||||
}
|
||||
|
||||
$token = $this->assignNewToken($poll);
|
||||
$poll->join_code = $this->generateJoinCode();
|
||||
$poll->is_active = true;
|
||||
$poll->show_results_to_participants = true;
|
||||
|
||||
if (!$this->save($poll)) {
|
||||
return [$poll, null];
|
||||
}
|
||||
|
||||
return [$poll, $token];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\ORM\Query\SelectQuery $query Query
|
||||
* @param string $token Plain admin token
|
||||
* @return \Cake\ORM\Query\SelectQuery
|
||||
*/
|
||||
public function findByAdminToken(SelectQuery $query, string $token): SelectQuery
|
||||
{
|
||||
return $query->where(['Polls.admin_token_hash' => static::hashToken($token)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\ORM\Query\SelectQuery $query Query
|
||||
* @param string $code Join code (will be normalised)
|
||||
* @return \Cake\ORM\Query\SelectQuery
|
||||
*/
|
||||
public function findByJoinCode(SelectQuery $query, string $code): SelectQuery
|
||||
{
|
||||
return $query->where(['Polls.join_code' => static::normalizeJoinCode($code)]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user