50dfca59e1
P1 Medien: eigener Admin-Bereich /admin/medien (Grid, Mehrfach-Upload, Drag&Drop, Alt-Text, URL kopieren, Loeschen). Upload konvertiert JPG/PNG via sharp zu WebP (Qualitaet 82, max 2000px), Original wird verworfen; WebP/SVG/GIF/AVIF unveraendert; Konvertierungsfehler -> Original behalten statt 500. media um alt/width/height erweitert. Wiederverwendbarer Medien-Picker (public/media-picker.js) ersetzt den URL-Prompt im Block-Editor, Produkt-Editor (Karte/Galerie/Varianten-Bild), Slides und Popups. JSON-Quelle /api/admin/media (session-gesichert). P2 Varianten: products.options_json + Tabelle product_variants. Produkt-Editor mit Options-Definition + Matrix-Generator (Preis-Override/Bestand/SKU/Bild/aktiv je Variante). PDP-Selektoren -> Variante; Cart/Checkout tragen sku+Options, Order-Item bekommt sku/variant, Variantenpreis serverseitig verifiziert. Produkte ohne Optionen unveraendert. P3 Litestream: Binary im Dockerfile, docker-entrypoint.sh (Restore+replicate nur bei LITESTREAM_REPLICA_URL, sonst reiner Node-Start), litestream.yml, Backup-Status unter Einstellungen, README + .env.example. P4 Analytics: Bestseller, Top-Suchbegriffe, Umsatz/Quelle, Umsatz-Zeitreihe, AOV, Wiederkaufrate, Lager-Warnungen. Neue Dep sharp. +19 Unit-Tests (49 gesamt gruen), Build + Smoke (P1-P4) gruen.
95 lines
3.7 KiB
JavaScript
95 lines
3.7 KiB
JavaScript
import { recordEvent, getSetting } from './lib/store.js';
|
|
import { createHash } from 'node:crypto';
|
|
import {
|
|
currentUser, adminBase, adminPathSegment, isCustomAdminPath, canAccess, landingFor,
|
|
} from './lib/auth.js';
|
|
|
|
const SKIP = ['/api/', '/uploads/', '/_astro', '/favicon', '/_image', '/robots.txt'];
|
|
|
|
function sessionHash(request) {
|
|
const ua = request.headers.get('user-agent') || '';
|
|
const ip = request.headers.get('x-forwarded-for') || request.headers.get('x-real-ip') || 'local';
|
|
const day = new Date().toISOString().slice(0, 10);
|
|
return createHash('sha256').update(ip + ua + day).digest('hex').slice(0, 16);
|
|
}
|
|
|
|
function sectionOf(adminInner) {
|
|
const seg = adminInner.replace(/^\//, '').split('/')[0] || 'dashboard';
|
|
const map = {
|
|
'': 'dashboard', 'bestellungen': 'bestellungen', 'produkte': 'produkte', 'kunden': 'kunden',
|
|
'analytics': 'analytics', 'marketing': 'marketing', 'rabatte': 'rabatte', 'inhalte': 'inhalte', 'medien': 'medien', 'einstellungen': 'einstellungen',
|
|
'nutzer': 'nutzer', 'audit': 'audit', 'versand': 'versandzonen', 'bewertungen': 'bewertungen', 'konto': 'dashboard', 'login': 'login', 'logout': 'logout',
|
|
};
|
|
return map[seg] || 'dashboard';
|
|
}
|
|
|
|
export async function onRequest(context, next) {
|
|
const { request, locals } = context;
|
|
const url = new URL(request.url);
|
|
const path = url.pathname;
|
|
const base = adminBase(); // "/login" oder "/admin"
|
|
const custom = isCustomAdminPath();
|
|
|
|
// Interner Rewrite-Durchlauf (auf physische /admin-Routen) -> einfach durchreichen.
|
|
if (locals && locals.__hdcAdminRewrite) {
|
|
return next();
|
|
}
|
|
|
|
// Custom-Admin-Pfad: direkter Zugriff auf physische /admin-Routen blocken (404).
|
|
if (custom && (path === '/admin' || path.startsWith('/admin/'))) {
|
|
return new Response('Not Found', { status: 404 });
|
|
}
|
|
|
|
// Admin-Bereich unter konfiguriertem Pfad
|
|
const isAdmin = path === base || path.startsWith(base + '/');
|
|
if (isAdmin) {
|
|
let inner = path.slice(base.length); // "" oder "/bestellungen/3"
|
|
if (inner === '') inner = '/';
|
|
const innerSeg = inner.replace(/^\//, '').split('/')[0];
|
|
const isLoginRoute = innerSeg === 'login';
|
|
const isLogoutRoute = innerSeg === 'logout';
|
|
|
|
const user = currentUser(request);
|
|
|
|
if (!user && !isLoginRoute) {
|
|
// Nicht eingeloggt -> Login-Seite rendern (HTTP 200).
|
|
if (locals) locals.__hdcAdminRewrite = true;
|
|
return context.rewrite('/admin/login?next=' + encodeURIComponent(path));
|
|
}
|
|
|
|
if (user && !isLoginRoute && !isLogoutRoute) {
|
|
const section = sectionOf(inner);
|
|
if (section !== 'dashboard' && section !== 'login' && section !== 'logout' && !canAccess(user.role, section)) {
|
|
return Response.redirect(new URL(landingFor(user.role), url), 302);
|
|
}
|
|
if (section === 'dashboard' && !canAccess(user.role, 'dashboard')) {
|
|
return Response.redirect(new URL(landingFor(user.role), url), 302);
|
|
}
|
|
}
|
|
|
|
// Auf physische /admin-Routen umschreiben.
|
|
if (custom) {
|
|
if (locals) locals.__hdcAdminRewrite = true;
|
|
const target = '/admin' + (inner === '/' ? '' : inner) + url.search;
|
|
return context.rewrite(target);
|
|
}
|
|
return next();
|
|
}
|
|
|
|
// First-Party Pageview-Tracking (nur Storefront-GET-Seiten)
|
|
if (request.method === 'GET' && !SKIP.some(s => path.startsWith(s))) {
|
|
try {
|
|
recordEvent({
|
|
type: 'pageview', path,
|
|
referrer: request.headers.get('referer') || '',
|
|
utm_source: url.searchParams.get('utm_source') || '',
|
|
utm_medium: url.searchParams.get('utm_medium') || '',
|
|
utm_campaign: url.searchParams.get('utm_campaign') || '',
|
|
session: sessionHash(request),
|
|
});
|
|
} catch {}
|
|
}
|
|
|
|
return next();
|
|
}
|