Plugin & Theme System: - Add plugin type definitions, permissions (30+), and validation constants - Add IndexedDB storage layer for plugin code, theme CSS, and previews - Add theme CSS sanitization, injection, and safety validation - Add HookBus event system with 130+ hooks across 20 domains - Add plugin ZIP extraction and manifest validation with JS security checks - Add sandboxed PluginAPI factory with scoped storage, logging, and permission gating - Add plugin loader with blob URL dynamic import and auto-disable circuit breaker - Add 3 built-in themes (Nord, Catppuccin, Solarized) - Add Zustand plugin store with install/uninstall/enable/disable lifecycle - Add PluginSlot, PluginSlotRenderer, and PluginErrorBoundary components - Add plugins and themes settings UI panels - Integrate plugin slots into email viewer, composer, navigation rail, sidebar, and context menu - Extend theme store with custom theme installation and activation Admin Dashboard: - Add admin authentication with scrypt password hashing and AES-256-GCM sessions - Add rate-limited login (5 attempts/15min per IP) - Add config manager with admin override > env var > default priority - Add settings policy system with feature gates and per-setting restrictions - Add audit logging with rotation - Add admin API routes (login, logout, config, policy, audit, password change) - Add admin UI pages (login, dashboard, config, policy, audit) - Add policy store for client-side feature gate enforcement - Wire admin password initialization into server instrumentation Tests: - Add 139 tests across 10 test files covering all plugin/theme modules
95 lines
3.3 KiB
TypeScript
95 lines
3.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { configManager } from '@/lib/admin/config-manager';
|
|
import { requireAdminAuth, getClientIP } from '@/lib/admin/session';
|
|
import { auditLog } from '@/lib/admin/audit';
|
|
import { CONFIG_ENV_MAP } from '@/lib/admin/types';
|
|
import { logger } from '@/lib/logger';
|
|
|
|
/**
|
|
* GET /api/admin/config — Get full config with sources (admin-protected)
|
|
*/
|
|
export async function GET() {
|
|
try {
|
|
const result = await requireAdminAuth();
|
|
if ('error' in result) return result.error;
|
|
|
|
await configManager.ensureLoaded();
|
|
const config = configManager.getAllWithSources();
|
|
|
|
return NextResponse.json(config, {
|
|
headers: { 'Cache-Control': 'no-store' },
|
|
});
|
|
} catch (error) {
|
|
logger.error('Admin config read error', { error: error instanceof Error ? error.message : 'Unknown error' });
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* PATCH /api/admin/config — Update config overrides (admin-protected)
|
|
*/
|
|
export async function PATCH(request: NextRequest) {
|
|
try {
|
|
const result = await requireAdminAuth();
|
|
if ('error' in result) return result.error;
|
|
|
|
const ip = getClientIP(request);
|
|
const updates = await request.json();
|
|
|
|
if (!updates || typeof updates !== 'object' || Array.isArray(updates)) {
|
|
return NextResponse.json({ error: 'Request body must be an object' }, { status: 400 });
|
|
}
|
|
|
|
// Validate keys
|
|
const validKeys = Object.keys(CONFIG_ENV_MAP);
|
|
const invalidKeys = Object.keys(updates).filter(k => !validKeys.includes(k));
|
|
if (invalidKeys.length > 0) {
|
|
return NextResponse.json({ error: `Unknown config keys: ${invalidKeys.join(', ')}` }, { status: 400 });
|
|
}
|
|
|
|
// Get old values for audit
|
|
const oldValues: Record<string, unknown> = {};
|
|
for (const key of Object.keys(updates)) {
|
|
oldValues[key] = configManager.get(key);
|
|
}
|
|
|
|
await configManager.setAdminConfig(updates);
|
|
await auditLog('config.update', { changes: Object.keys(updates).map(k => ({ key: k, old: oldValues[k], new: updates[k] })) }, ip);
|
|
|
|
return NextResponse.json({ ok: true });
|
|
} catch (error) {
|
|
logger.error('Admin config update error', { error: error instanceof Error ? error.message : 'Unknown error' });
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/admin/config — Remove admin override for a key (revert to env/default)
|
|
*/
|
|
export async function DELETE(request: NextRequest) {
|
|
try {
|
|
const result = await requireAdminAuth();
|
|
if ('error' in result) return result.error;
|
|
|
|
const ip = getClientIP(request);
|
|
const { key } = await request.json();
|
|
|
|
if (!key || typeof key !== 'string') {
|
|
return NextResponse.json({ error: 'Key is required' }, { status: 400 });
|
|
}
|
|
|
|
if (!CONFIG_ENV_MAP[key]) {
|
|
return NextResponse.json({ error: `Unknown config key: ${key}` }, { status: 400 });
|
|
}
|
|
|
|
const oldValue = configManager.get(key);
|
|
await configManager.removeAdminOverride(key);
|
|
await auditLog('config.revert', { key, oldValue }, ip);
|
|
|
|
return NextResponse.json({ ok: true });
|
|
} catch (error) {
|
|
logger.error('Admin config revert error', { error: error instanceof Error ? error.message : 'Unknown error' });
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|