Einwilligung, die tatsächlich zurückhält, und Pflichtseiten
Ein Hinweis, der nur fragt und sonst nichts ändert, ist Theater. Deshalb zwei Hälften, und die zweite ist die wichtigere. Die Frage: serverseitig gerendert, per Formular beantwortet, ohne JavaScript - die öffentlichen Seiten laufen mit script-src 'none', und ein Banner mit Skript wäre das eine, was diese Regel bricht. "Alle akzeptieren" und "Nur notwendige" sind dieselbe Art Knopf, gleich groß, nebeneinander. Die Entscheidung steht in einem eigenen Cookie: Fassung der Frage, gewährte Gruppen, Zeitpunkt. Keine Kennung, kein Serverabgleich, ein halbes Jahr. Kein CSRF-Token, absichtlich: die Anfrage ändert nichts auf dem Server, und ein Token bräuchte für jeden Besucher eine Sitzung - genau das, was ein Einwilligungshinweis vermeiden soll. Geprüft wird stattdessen das Rücksprungziel. Das Zurückhalten: der neue Block "Externer Inhalt" (Dokumentschema v14) lädt ohne Einwilligung nichts - kein iframe, keine Adresse im Markup, frame-src 'none'. Der Platzhalter nennt den Anbieter; sein Knopf gewährt genau diese Gruppe und nimmt die vorher gewährten mit. Erst danach kommt der Host in die Sicherheitsrichtlinie, und nur dieser. Im Editor bleibt es immer ein Platzhalter. Datenschutz und Impressum sind keine Einstellung mehr: fehlen sie, warnt die Verwaltung und legt sie auf Knopfdruck als Gerüst an, mit NOCH AUSZUFÜLLEN an jeder Pflichtangabe. Den Text schreibt das CMS nicht - das ist eine rechtliche Aussage über eine Firma. Verlinkt werden beide aus dem Template, nicht aus dem Fußbereich: ein Pflichtlink, den man löschen kann, ist keiner. Dazu der Fehler, den Umut im Screenshot gesehen hat: die Wortmarke stand als Buchstabenkolonne in einem 60 Pixel breiten Kasten. .site-header war noch ein Flexcontainer aus der Zeit des handgeschriebenen Markups und quetschte die Sektion zusammen, und design.css band jede Designklasse an #main - wo Kopf- und Fußbereich seit ADR 026 nicht mehr liegen. Beides behoben. Neu im Ausbauplan: Statistik (serverseitige, wiedererkennungsfreie Zählung, die deshalb keine Einwilligung braucht), Inhalte und Ladeverhalten prüfen (Blättern überall, minimal laden, Ladebudget), Installer (Schritt für Schritt im Browser, Migration inklusive). 245 PHP-Tests / 976 Assertions unter PHP 8.2.12 und 8.4.22. Der Browsertest fand zwei Fehler von mir: grauer Text auf dunklem Fußbereich mit 2,24:1, und das fixierte Banner verdeckte den Einwilligungsknopf am Seitenende. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
84d471acd9
commit
ee446f1ea1
30 files changed
+1298
-154
No files matched your search
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Cookie;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* The visitor's decision about groups of content that may not load without consent.
|
||||
*
|
||||
* Two things matter here, and the banner is the smaller one. The banner only asks; what makes the
|
||||
* answer worth anything is that an embed does not load until it has been given - that is why
|
||||
* `granted()` is asked in the renderer and not only in the template.
|
||||
*
|
||||
* The decision lives in one first-party cookie and nowhere else: no server record, no identifier,
|
||||
* no third party. The cookie holds the version of the question, the groups that were granted and
|
||||
* when - nothing about the person. It is deliberately not signed: it describes what this browser
|
||||
* chose, so a forged value can only change what that same browser gets shown.
|
||||
*/
|
||||
final class Consent
|
||||
{
|
||||
public const COOKIE = 'kant_consent';
|
||||
|
||||
/**
|
||||
* Necessary content is not a group: it is what remains when nothing is granted, and it is
|
||||
* never asked about. Only these two can be asked about.
|
||||
*/
|
||||
public const GROUPS = [
|
||||
'statistics' => 'Statistik',
|
||||
'external' => 'Externe Inhalte',
|
||||
];
|
||||
|
||||
public const GROUP_HELP = [
|
||||
'statistics' => 'Messung, wie die Website benutzt wird.',
|
||||
'external' => 'Inhalte von anderen Anbietern, etwa Karten, Videos, Bewertungen oder ein Chat. Sie sehen die Adresse dieser Seite und deine IP-Adresse, sobald sie geladen werden.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Half a year, then the question is asked again. Long enough not to be a nuisance, short
|
||||
* enough that a decision is not silently eternal.
|
||||
*/
|
||||
public const MAX_AGE = 15552000;
|
||||
|
||||
/** Raise this when the groups or their meaning change: an old answer then no longer counts. */
|
||||
public const VERSION = 1;
|
||||
|
||||
public function __construct(private SiteSettings $settings)
|
||||
{
|
||||
}
|
||||
|
||||
/** Which groups this site asks about at all. Empty means: nothing to consent to. */
|
||||
public function askedGroups(): array
|
||||
{
|
||||
$settings = $this->settings->load();
|
||||
if (!$settings['consentBanner']) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values(array_intersect(array_keys(self::GROUPS), $settings['consentGroups']));
|
||||
}
|
||||
|
||||
/**
|
||||
* The stored decision, or null when there is none that still counts.
|
||||
*
|
||||
* @return array{granted: list<string>, at: int}|null
|
||||
*/
|
||||
public function decision(Request $request): ?array
|
||||
{
|
||||
$raw = $request->cookies->get(self::COOKIE);
|
||||
if (!is_string($raw) || strlen($raw) > 200) {
|
||||
return null;
|
||||
}
|
||||
$parts = explode('|', $raw);
|
||||
if (3 !== count($parts) || (string) self::VERSION !== $parts[0]) {
|
||||
return null;
|
||||
}
|
||||
$at = filter_var($parts[2], FILTER_VALIDATE_INT);
|
||||
if (false === $at || $at <= 0 || $at + self::MAX_AGE < time()) {
|
||||
return null;
|
||||
}
|
||||
$granted = array_values(array_intersect(
|
||||
array_keys(self::GROUPS),
|
||||
'' === $parts[1] ? [] : explode(',', $parts[1]),
|
||||
));
|
||||
|
||||
return ['granted' => $granted, 'at' => $at];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a group may load.
|
||||
*
|
||||
* A group this site never asks about counts as granted: otherwise switching the banner off
|
||||
* would silently hide content that needs no consent.
|
||||
*/
|
||||
public function granted(Request $request, string $group): bool
|
||||
{
|
||||
if (!in_array($group, $this->askedGroups(), true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return in_array($group, $this->decision($request)['granted'] ?? [], true);
|
||||
}
|
||||
|
||||
/** Whether this request should be shown the question. */
|
||||
public function needsBanner(Request $request): bool
|
||||
{
|
||||
return [] !== $this->askedGroups() && null === $this->decision($request);
|
||||
}
|
||||
|
||||
/** @param list<string> $groups */
|
||||
public function remember(Request $request, Response $response, array $groups): void
|
||||
{
|
||||
$granted = array_values(array_intersect($this->askedGroups(), $groups));
|
||||
$response->headers->setCookie(Cookie::create(
|
||||
self::COOKIE,
|
||||
sprintf('%d|%s|%d', self::VERSION, implode(',', $granted), time()),
|
||||
time() + self::MAX_AGE,
|
||||
'/',
|
||||
null,
|
||||
$request->isSecure(),
|
||||
// No script needs to read it: the banner is rendered by the server.
|
||||
true,
|
||||
false,
|
||||
Cookie::SAMESITE_LAX,
|
||||
));
|
||||
}
|
||||
|
||||
public function forget(Response $response): void
|
||||
{
|
||||
$response->headers->clearCookie(self::COOKIE, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* The two mandatory pages, as link path => label, for the note every page carries.
|
||||
*
|
||||
* It sits in this class because the templates already have it at hand, and because the note
|
||||
* belongs next to the consent link: both are things a visitor must be able to reach from
|
||||
* anywhere, not things an editor can delete out of the footer by accident.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function legalLinks(): array
|
||||
{
|
||||
$settings = $this->settings->load();
|
||||
$links = [];
|
||||
foreach (['privacyPath' => 'Datenschutz', 'imprintPath' => 'Impressum'] as $key => $label) {
|
||||
if ('' !== $settings[$key]) {
|
||||
$links[$settings[$key]] = $label;
|
||||
}
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/** What the banner and the consent page need to render. */
|
||||
public function state(Request $request): array
|
||||
{
|
||||
$settings = $this->settings->load();
|
||||
$decision = $this->decision($request);
|
||||
|
||||
return [
|
||||
'groups' => array_map(
|
||||
static fn (string $group): array => [
|
||||
'key' => $group,
|
||||
'name' => self::GROUPS[$group],
|
||||
'help' => self::GROUP_HELP[$group],
|
||||
'granted' => in_array($group, $decision['granted'] ?? [], true),
|
||||
],
|
||||
$this->askedGroups(),
|
||||
),
|
||||
'decided' => null !== $decision,
|
||||
'decidedAt' => $decision['at'] ?? null,
|
||||
'text' => $settings['consentText'],
|
||||
'privacyPath' => $settings['privacyPath'],
|
||||
'imprintPath' => $settings['imprintPath'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user