Extend export/import to cover site settings and permission groups

Phase 2 of the dashboard roadmap. app_settings (phone, maintenance
texts, package-card theme, mail templates, etc.) and permission_groups
now round-trip through export/import like every other section - this
was the main gap making a hoster migration more manual than it needed
to be. Admin accounts stay opt-in behind an explicit checkbox (default
off) since they carry password hashes and 2FA secrets; real credentials
(SMTP password, Google Places API key) and the Google Reviews cache are
deliberately still excluded and must be re-entered per environment.

Also fixed exportData() to actually select only the allow-listed
columns per table instead of `SELECT *`, matching what the surrounding
comment already claimed - needed so app_settings' secret columns can't
leak into an export file just because a new column gets added later.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Maaxxs 2026-09-09 01:38:51 +02:00
parent 63dcd23977
commit 5a56fec2c7
4 changed files with 141 additions and 28 deletions

View file

@ -19,6 +19,18 @@ const SECTIONS = [
hint: 'Galerie-Marken, Projekte und Fotos (inkl. Bilder)',
tables: ['gallery_brands', 'gallery_projects', 'gallery_photos'],
},
{
key: 'settings',
label: 'Einstellungen',
hint: 'Telefon/Adresse, Wartungsmodus-Texte, Kachel-Design, Mail-Vorlagen (ohne Zugangsdaten)',
tables: ['app_settings'],
},
{
key: 'permissions',
label: 'Berechtigungsgruppen',
hint: 'Rollen-Vorlagen und ihre Rechte (ohne Benutzerkonten selbst)',
tables: ['permission_groups', 'permission_group_permissions'],
},
];
const TABLE_LABELS = {
@ -32,6 +44,12 @@ const TABLE_LABELS = {
gallery_brands: 'Galerie-Marken',
gallery_projects: 'Galerie-Projekte',
gallery_photos: 'Galerie-Fotos',
app_settings: 'Einstellungen',
permission_groups: 'Berechtigungsgruppen',
permission_group_permissions: 'Berechtigungsgruppen-Rechte',
admin_users: 'Benutzerkonten',
admin_user_permissions: 'Benutzer-Einzelrechte',
admin_user_groups: 'Benutzer-Gruppenzuweisungen',
};
// Kurze, lesbare Zusammenfassung ("3 Marken, 12 Modelle, 45 Pakete") aus den im Export
@ -50,6 +68,8 @@ export default function ExportImportSettings() {
const [parsedData, setParsedData] = useState(null);
const [parseError, setParseError] = useState('');
const [selectedSections, setSelectedSections] = useState(() => new Set(SECTIONS.map((s) => s.key)));
const [exportAccounts, setExportAccounts] = useState(false);
const [importAccounts, setImportAccounts] = useState(false);
const [importConfirmed, setImportConfirmed] = useState(false);
const [importBusy, setImportBusy] = useState(false);
const [importResult, setImportResult] = useState(null);
@ -63,6 +83,7 @@ export default function ExportImportSettings() {
setParsedData(null);
setParseError('');
setSelectedSections(new Set(SECTIONS.map((s) => s.key)));
setImportAccounts(false);
if (!file) return;
try {
const text = await file.text();
@ -96,11 +117,13 @@ export default function ExportImportSettings() {
const formData = new FormData();
formData.append('file', importFile);
formData.append('sections', JSON.stringify([...selectedSections]));
formData.append('include_accounts', importAccounts ? '1' : '0');
const result = await api.post('/settings/import', formData);
setImportResult(result);
setImportFile(null);
setParsedData(null);
setImportConfirmed(false);
setImportAccounts(false);
e.target.reset();
} catch (err) {
setImportError(err.message);
@ -110,18 +133,30 @@ export default function ExportImportSettings() {
};
const selectedLabels = SECTIONS.filter((s) => selectedSections.has(s.key)).map((s) => s.label);
if (importAccounts) selectedLabels.push('Benutzerkonten');
return (
<div className="max-w-2xl space-y-6">
<section className="rounded-xl border border-neutral-200 bg-white p-6 dark:border-neutral-800 dark:bg-neutral-900">
<h2 className="mb-1 font-semibold text-neutral-900 dark:text-white">Daten exportieren</h2>
<p className="mb-4 text-sm text-neutral-500 dark:text-neutral-400">
Lädt eine Datei mit allen Marken, Modellen, Paketen, Produkten, Upgrades, Leistungen, FAQs und der
kompletten Bildergalerie (inkl. Bilder) herunter. Benutzerkonten und Kontaktanfragen sind bewusst
<strong> nicht</strong> enthalten.
Lädt eine Datei mit allen Marken, Modellen, Paketen, Produkten, Upgrades, Leistungen, FAQs, der
kompletten Bildergalerie (inkl. Bilder), den Einstellungen und den Berechtigungsgruppen herunter
(ohne Zugangsdaten wie SMTP-Passwort oder API-Keys – die müssen auf jedem Server neu eingetragen
werden). Kontaktanfragen sind bewusst <strong>nicht</strong> enthalten.
</p>
<label className="mb-3 flex items-start gap-2 text-sm text-neutral-700 dark:text-neutral-300">
<input
type="checkbox"
checked={exportAccounts}
onChange={(e) => setExportAccounts(e.target.checked)}
className="mt-0.5 h-4 w-4 rounded border-neutral-300 text-brand-600 focus:ring-brand-500"
/>
Benutzerkonten mit exportieren (enthält Passwort-Hashes und 2FA-Geheimnisse – nur für Backups an
einem vertrauenswürdigen Ort)
</label>
<a
href={`${API_BASE}/settings/export`}
href={`${API_BASE}/settings/export${exportAccounts ? '?include_accounts=1' : ''}`}
className="inline-block rounded-md bg-brand-500 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-600"
>
Jetzt exportieren
@ -132,8 +167,8 @@ export default function ExportImportSettings() {
<h2 className="mb-1 font-semibold text-neutral-900 dark:text-white">Daten importieren</h2>
<p className="mb-4 text-sm text-neutral-500 dark:text-neutral-400">
Lädt eine zuvor exportierte Datei auf diesen Server. Ersetzt dabei die ausgewählten Bereiche komplett
durch den Inhalt der Datei – das kann nicht rückgängig gemacht werden. Kontaktanfragen und
Benutzerkonten bleiben immer unangetastet.
durch den Inhalt der Datei – das kann nicht rückgängig gemacht werden. Kontaktanfragen bleiben immer
unangetastet, Benutzerkonten nur wenn unten explizit angehakt.
</p>
<form onSubmit={handleImport} className="space-y-4">
<input
@ -171,6 +206,23 @@ export default function ExportImportSettings() {
</span>
</label>
))}
<label className="flex cursor-pointer items-start gap-3 px-4 py-3 hover:bg-neutral-50 dark:hover:bg-neutral-800/50">
<input
type="checkbox"
checked={importAccounts}
onChange={(e) => setImportAccounts(e.target.checked)}
className="mt-0.5 h-4 w-4 rounded border-neutral-300 text-brand-600 focus:ring-brand-500"
/>
<span className="flex-1">
<span className="block text-sm font-medium text-neutral-800 dark:text-neutral-100">
Benutzerkonten
</span>
<span className="block text-xs text-neutral-500 dark:text-neutral-400">
Nur wenn die Datei mit "Benutzerkonten mit exportieren" erstellt wurde. Ersetzt auch dein
eigenes Konto – danach ggf. mit den Zugangsdaten aus der Export-Datei neu einloggen.
</span>
</span>
</label>
</div>
</div>
)}

View file

@ -24,6 +24,35 @@ class SettingsController
'gallery_brands' => ['id', 'name', 'slug', 'cover_image_path', 'sort_order', 'created_at', 'updated_at'],
'gallery_projects' => ['id', 'gallery_brand_id', 'name', 'slug', 'cover_image_path', 'sort_order', 'created_at', 'updated_at'],
'gallery_photos' => ['id', 'gallery_project_id', 'image_path', 'caption', 'sort_order', 'created_at', 'updated_at'],
// Absichtlich OHNE mail_password und google_places_api_key (echte Zugangsdaten/API-Keys -
// sollen nie in einer portablen Export-Datei landen, muessen pro Umgebung neu eingetragen
// werden), ohne die reinen Google-Reviews-Cache-Felder (google_rating, google_rating_count,
// google_reviews_updated_at, google_reviews_error - aktualisieren sich nach einem Umzug von
// selbst, aus denselben Gruenden wie die komplett ausgeschlossene google_reviews-Tabelle)
// und ohne id/updated_at (Singleton-Zeile mit fester id=1, kein Fremdschluessel zeigt
// darauf - updated_at bekommt beim Import ohnehin automatisch den aktuellen Zeitstempel).
'app_settings' => [
'maintenance_global', 'maintenance_global_message',
'maintenance_services', 'maintenance_services_message',
'maintenance_vehicles', 'maintenance_vehicles_message',
'phone', 'whatsapp', 'contact_email', 'hero_image_path',
'ga_measurement_id', 'ga_property_id', 'google_place_id',
'package_card_theme', 'package_card_layout', 'package_wheel_hint_interval',
'mail_host', 'mail_port', 'mail_username', 'mail_encryption',
'mail_from_email', 'mail_from_name', 'mail_notify_email',
'mail_customer_subject', 'mail_customer_body', 'mail_owner_subject', 'mail_owner_body',
],
'permission_groups' => ['id', 'name', 'description', 'created_at', 'updated_at'],
'permission_group_permissions' => ['permission_group_id', 'permission'],
];
// Separat von TABLE_COLUMNS gehalten (nicht Teil der normalen Export-/Import-Schleife):
// Benutzerkonten enthalten Passwort-Hashes und 2FA-Geheimnisse und werden nur exportiert/
// importiert, wenn explizit per include_accounts=1 angefordert (siehe exportData()/importData()).
private const ACCOUNT_TABLES = [
'admin_users' => ['id', 'username', 'password_hash', 'is_super_admin', 'two_factor_secret', 'two_factor_enabled', 'two_factor_recovery_codes', 'last_active_at', 'created_at'],
'admin_user_permissions' => ['admin_user_id', 'permission'],
'admin_user_groups' => ['admin_user_id', 'permission_group_id'],
];
// Gruppierung fuer den wahlweisen Import: jede Gruppe wird beim Import als Ganzes
@ -36,6 +65,8 @@ class SettingsController
'services' => ['services'],
'faqs' => ['faqs'],
'gallery' => ['gallery_brands', 'gallery_projects', 'gallery_photos'],
'settings' => ['app_settings'],
'permissions' => ['permission_groups', 'permission_group_permissions'],
];
// Welche Tabellenspalte pro Tabelle einen Bild-Pfad enthaelt - dieselbe Zuordnung
@ -58,15 +89,25 @@ class SettingsController
public static function exportData(): void
{
$includeAccounts = ($_GET['include_accounts'] ?? '') === '1';
$tableColumns = self::TABLE_COLUMNS;
if ($includeAccounts) {
$tableColumns += self::ACCOUNT_TABLES;
}
$db = Database::connection();
$data = [];
foreach (array_keys(self::TABLE_COLUMNS) as $table) {
$data[$table] = $db->query("SELECT * FROM `$table`")->fetchAll();
foreach ($tableColumns as $table => $columns) {
// Explizite Spaltenliste statt SELECT * - so bestimmt TABLE_COLUMNS/ACCOUNT_TABLES
// wirklich (nicht nur beim Import), welche Spalten eine Export-Datei je verlassen,
// was fuer app_settings wichtig ist (siehe Kommentar dort zu ausgeschlossenen Spalten).
$columnList = '`' . implode('`,`', $columns) . '`';
$data[$table] = $db->query("SELECT $columnList FROM `$table`")->fetchAll();
}
$images = [];
foreach (self::IMAGE_COLUMNS as $table => $column) {
foreach ($data[$table] as $row) {
foreach (($data[$table] ?? []) as $row) {
self::collectImage($row[$column] ?? null, $images);
}
}
@ -163,6 +204,12 @@ class SettingsController
$tables = array_merge($tables, self::IMPORT_SECTIONS[$section]);
}
// Benutzerkonten sind bewusst nicht Teil von IMPORT_SECTIONS/sections - nur wenn dieses
// separate Feld explizit gesetzt ist, werden admin_users & Zuweisungen mit ersetzt (siehe
// Kommentar bei ACCOUNT_TABLES). Achtung: ersetzt das eigene, gerade eingeloggte Konto
// mit - danach ggf. mit den Zugangsdaten aus der Export-Datei neu einloggen.
$includeAccounts = ($_POST['include_accounts'] ?? '') === '1';
$db = Database::connection();
$counts = [];
@ -176,6 +223,14 @@ class SettingsController
$counts[$table] = self::insertRows($db, $table, self::TABLE_COLUMNS[$table], $rows);
}
if ($includeAccounts) {
foreach (self::ACCOUNT_TABLES as $table => $columns) {
$rows = is_array($data[$table] ?? null) ? $data[$table] : [];
$db->exec("DELETE FROM `$table`");
$counts[$table] = self::insertRows($db, $table, $columns, $rows);
}
}
// Nur Bilder wiederherstellen, die von Zeilen der tatsaechlich importierten
// Tabellen referenziert werden - sonst wuerden z.B. Galerie-Bilder auf den
// Server geschrieben, obwohl "Bildergalerie" beim Import abgewaehlt war.
@ -201,7 +256,13 @@ class SettingsController
Http::error('Import fehlgeschlagen, es wurde nichts verändert: ' . $e->getMessage(), 500);
}
Http::send(['ok' => true, 'sections' => $sections, 'counts' => $counts, 'images_restored' => count($imagesToRestore)]);
Http::send([
'ok' => true,
'sections' => $sections,
'accounts_included' => $includeAccounts,
'counts' => $counts,
'images_restored' => count($imagesToRestore),
]);
}
public static function resetServicesToDefaults(): void

File diff suppressed because one or more lines are too long

View file

@ -43,7 +43,7 @@
"sameAs": ["https://www.youtube.com/@hifiplanet2812"]
}
</script>
<script type="module" crossorigin src="/assets/index-PD_IcG1Q.js"></script>
<script type="module" crossorigin src="/assets/index-DSPSdxQO.js"></script>
<link rel="modulepreload" crossorigin href="/assets/lucide-icons-66ioQXWI.js">
<link rel="stylesheet" crossorigin href="/assets/index-Cvi6-oeK.css">
</head>