recht: Meldeverfahren v2 Phasen 1-4 — Re-Sync-Sperre, Anonymisierung, Magic-Link-Intake, Self-Service-Korrektur
Phase 1: sync_locked_at auf press_releases (Importer überspringt gesperrte PMs auch bei --force), LegalRequest um Verifikation/Ausführung erweitert, verschlüsselte Vorher-Snapshots mit Löschfrist (snapshots:purge, täglich). Phase 2: AnonymizationService (Unicode-Wortgrenzen-Schwärzung, Pflicht-Diff, transaktionale Ausführung mit Snapshot, Kontakt-Detach, Bild-Löschung, Sitemap-Invalidierung, Verlauf ohne Klartext-Altwerte); Admin-Queue kann DSGVO-Anfragen anonymisieren oder depublizieren, inkl. Snapshot-Restore. Phase 3: Deflektions-Trichter „PM melden/ändern" (melden/pm) mit Kosten- Tabelle vorab; Magic-Link geht nur an die hinterlegte Pressekontaktadresse (neuer purpose press_release_report), atomare Verifikation, Anliegen-Auswahl erzeugt verifizierte Queue-Anfragen; toter-Kanal-Fallback; Owner-Einstieg. Phase 4: Panel-Abschnitt „Änderung & Korrektur" — Levenshtein-Gate (5 %, Mini-Edit-Ausnahme ≤ 2 Zeichen) stuft klein/groß ein; kleine Korrektur kostenlos sofort live (Snapshot, Sperre, Verlauf, Re-Score), größere als Change-Anfrage (Interim bis 10-€-Checkout, Phase 7). Entwicklungsplan mit Entscheidungen E1-E8 und offenen Punkten in docs/Decision-Update; 41 Pest-Tests (tests/Feature/Legal) grün. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
58c0cc3f23
commit
b4b2e013d6
42 changed files with 3868 additions and 1 deletions
230
app/Services/PressRelease/AnonymizationService.php
Normal file
230
app/Services/PressRelease/AnonymizationService.php
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\PressRelease;
|
||||
|
||||
use App\Enums\ActivityType;
|
||||
use App\Models\LegalRequest;
|
||||
use App\Models\PressRelease;
|
||||
use App\Models\PressReleaseRedactionSnapshot;
|
||||
use App\Services\ActivityLog;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* Schwärzung personenbezogener Daten in einer Pressemitteilung
|
||||
* (Meldeverfahren v2, Spur B — Phase 2).
|
||||
*
|
||||
* Ersetzt Freitext-Schwärzwerte inkl. Varianten über Wortgrenzen in den
|
||||
* Textfeldern, löst Kontakt-Zuordnungen, entfernt markierte Bilder/Anhänge
|
||||
* und sichert die Vorher-Werte verschlüsselt (Reversibilität, nie Klartext
|
||||
* in Logs). Bewusst OHNE KI-Gate und OHNE Credit-Bezug — der DSGVO-Pfad
|
||||
* ist Compliance-Pflicht (Art. 12 Abs. 5) und darf an keinem Freigabelauf
|
||||
* oder Bezahlschritt hängen.
|
||||
*/
|
||||
class AnonymizationService
|
||||
{
|
||||
/** @var list<string> */
|
||||
public const REDACTABLE_FIELDS = ['title', 'subtitle', 'text', 'boilerplate_override', 'keywords'];
|
||||
|
||||
public const DEFAULT_REPLACEMENT = '[entfernt]';
|
||||
|
||||
public function __construct(private readonly ActivityLog $activityLog) {}
|
||||
|
||||
/**
|
||||
* Pflicht-Vorschau: Was würde ersetzt? Verändert die PM nicht.
|
||||
*
|
||||
* @param list<string> $terms
|
||||
* @return array{fields: array<string, array{hits: int, after: string}>, total_hits: int}
|
||||
*/
|
||||
public function preview(PressRelease $pressRelease, array $terms, string $replacement = self::DEFAULT_REPLACEMENT): array
|
||||
{
|
||||
$fields = [];
|
||||
$totalHits = 0;
|
||||
|
||||
foreach (self::REDACTABLE_FIELDS as $field) {
|
||||
$value = (string) ($pressRelease->getAttribute($field) ?? '');
|
||||
|
||||
if ($value === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
[$after, $hits] = $this->redact($value, $terms, $replacement);
|
||||
|
||||
if ($hits > 0) {
|
||||
$fields[$field] = ['hits' => $hits, 'after' => $after];
|
||||
$totalHits += $hits;
|
||||
}
|
||||
}
|
||||
|
||||
return ['fields' => $fields, 'total_hits' => $totalHits];
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt die Schwärzung aus: Snapshot → Text-Replace → Kontakte lösen →
|
||||
* Bilder/Anhänge entfernen → Re-Sync-Sperre → Cache-Invalidierung →
|
||||
* Verlaufs-Eintrag ohne Klartext-Altwerte.
|
||||
*
|
||||
* @param list<string> $terms
|
||||
* @param list<int> $removeImageIds
|
||||
* @param list<int> $removeAttachmentIds
|
||||
*/
|
||||
public function execute(
|
||||
PressRelease $pressRelease,
|
||||
array $terms,
|
||||
string $replacement = self::DEFAULT_REPLACEMENT,
|
||||
bool $detachContacts = true,
|
||||
array $removeImageIds = [],
|
||||
array $removeAttachmentIds = [],
|
||||
?LegalRequest $legalRequest = null,
|
||||
): PressReleaseRedactionSnapshot {
|
||||
$totalHits = 0;
|
||||
|
||||
$snapshot = DB::transaction(function () use (
|
||||
$pressRelease, $terms, $replacement, $detachContacts,
|
||||
$removeImageIds, $removeAttachmentIds, $legalRequest, &$totalHits,
|
||||
): PressReleaseRedactionSnapshot {
|
||||
$contactIds = $detachContacts
|
||||
? $pressRelease->contacts()->pluck('contacts.id')->all()
|
||||
: [];
|
||||
|
||||
$images = $pressRelease->images()->whereIn('id', $removeImageIds)->get();
|
||||
$attachments = $pressRelease->attachments()->whereIn('id', $removeAttachmentIds)->get();
|
||||
|
||||
$snapshot = PressReleaseRedactionSnapshot::capture($pressRelease, [
|
||||
'fields' => $pressRelease->only(self::REDACTABLE_FIELDS),
|
||||
'contact_ids' => $contactIds,
|
||||
'images' => $images->map(fn ($image) => $image->only(['id', 'disk', 'path', 'title']))->all(),
|
||||
'attachments' => $attachments->map(fn ($attachment) => $attachment->only(['id', 'disk', 'path']))->all(),
|
||||
'terms' => $terms,
|
||||
'replacement' => $replacement,
|
||||
], $legalRequest);
|
||||
|
||||
$changedFields = [];
|
||||
|
||||
foreach (self::REDACTABLE_FIELDS as $field) {
|
||||
$value = (string) ($pressRelease->getAttribute($field) ?? '');
|
||||
|
||||
if ($value === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
[$after, $hits] = $this->redact($value, $terms, $replacement);
|
||||
|
||||
if ($hits > 0) {
|
||||
$pressRelease->setAttribute($field, $after);
|
||||
$changedFields[] = $field;
|
||||
$totalHits += $hits;
|
||||
}
|
||||
}
|
||||
|
||||
if ($changedFields !== []) {
|
||||
$pressRelease->save();
|
||||
}
|
||||
|
||||
if ($contactIds !== []) {
|
||||
$pressRelease->contacts()->detach($contactIds);
|
||||
}
|
||||
|
||||
// Klarname in Pressebild/PDF: Zeile soft-löschen (raus aus der
|
||||
// Auslieferung); auf der public-Disk zusätzlich die Dateien
|
||||
// entfernen, weil sie sonst per Direkt-URL erreichbar blieben.
|
||||
foreach ($images as $image) {
|
||||
$this->deletePublicFiles($image->disk, array_filter([
|
||||
$image->path,
|
||||
...array_values((array) ($image->variants ?? [])),
|
||||
]));
|
||||
$image->delete();
|
||||
}
|
||||
|
||||
foreach ($attachments as $attachment) {
|
||||
$this->deletePublicFiles($attachment->disk, array_filter([$attachment->path]));
|
||||
$attachment->delete();
|
||||
}
|
||||
|
||||
$pressRelease->lockSync(
|
||||
'Anonymisierung im Meldeverfahren'.($legalRequest ? ' '.$legalRequest->reference() : '')
|
||||
);
|
||||
|
||||
return $snapshot;
|
||||
});
|
||||
|
||||
$this->forgetSitemapCaches();
|
||||
|
||||
// Bewusst ohne Alt-Werte und ohne Schwärzbegriffe — sonst leben die
|
||||
// personenbezogenen Daten im Verlauf weiter.
|
||||
$this->activityLog->record(
|
||||
ActivityType::Anonymization,
|
||||
sprintf(
|
||||
'PM anonymisiert%s: %d Ersetzung(en), %d Kontakt(e) gelöst, %d Bild(er), %d Anhang/Anhänge entfernt.',
|
||||
$legalRequest ? ' ('.$legalRequest->reference().')' : '',
|
||||
$totalHits,
|
||||
count($snapshot->encrypted_before['contact_ids'] ?? []),
|
||||
count($snapshot->encrypted_before['images'] ?? []),
|
||||
count($snapshot->encrypted_before['attachments'] ?? []),
|
||||
),
|
||||
user: Auth::user(),
|
||||
subject: $pressRelease,
|
||||
meta: ['legal_request_id' => $legalRequest?->getKey(), 'snapshot_id' => $snapshot->getKey()],
|
||||
);
|
||||
|
||||
return $snapshot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ersetzung über Wortgrenzen: Lookarounds auf Unicode-Buchstaben/Ziffern
|
||||
* statt \b, damit Umlaute korrekt begrenzen und weder Teiltreffer
|
||||
* stehenbleiben noch Wörter wie „maximal" bei Begriff „Max" zerlegt
|
||||
* werden. Längste Begriffe zuerst („Matvey Teplitsky" vor „Teplitsky").
|
||||
*
|
||||
* @param list<string> $terms
|
||||
* @return array{0: string, 1: int}
|
||||
*/
|
||||
private function redact(string $value, array $terms, string $replacement): array
|
||||
{
|
||||
$totalHits = 0;
|
||||
|
||||
$normalized = collect($terms)
|
||||
->map(fn (string $term): string => trim($term))
|
||||
->filter()
|
||||
->unique()
|
||||
->sortByDesc(fn (string $term): int => mb_strlen($term))
|
||||
->values();
|
||||
|
||||
foreach ($normalized as $term) {
|
||||
$pattern = '/(?<![\pL\pN])'.preg_quote($term, '/').'(?![\pL\pN])/iu';
|
||||
$value = (string) preg_replace($pattern, $replacement, $value, -1, $hits);
|
||||
$totalHits += (int) $hits;
|
||||
}
|
||||
|
||||
return [$value, $totalHits];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $paths
|
||||
*/
|
||||
private function deletePublicFiles(?string $disk, array $paths): void
|
||||
{
|
||||
if ($disk !== 'public' || $paths === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
Storage::disk('public')->delete($paths);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sitemap-Caches aller Hosts verwerfen (SitemapController cached 1h je
|
||||
* Host) — nach einer Schwärzung darf kein Cache den alten Stand halten.
|
||||
*/
|
||||
private function forgetSitemapCaches(): void
|
||||
{
|
||||
foreach ((array) config('domains.domains', []) as $domain) {
|
||||
$host = parse_url((string) ($domain['url'] ?? ''), PHP_URL_HOST);
|
||||
|
||||
if ($host) {
|
||||
Cache::forget('web.sitemap.'.$host);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue