Panel admin propio: gestión de noticias
- lib/admin.ts: isAdmin(session) por allowlist ADMIN_EMAILS (normalizada) o gmlevel de AzerothCore (account_access, resiliente si no existe). ADMIN_EMAILS/ADMIN_GMLEVEL en .env.local. - lib/admin-news.ts: listNews / createNews / deleteNews (home_noticia). - Routes /api/admin/news (POST, guard isAdmin) y /api/admin/news/[id] (DELETE). - Páginas /admin (dashboard) y /admin/news (protegidas: no-admin -> redirect). AdminNewsManager (cliente: crear + listar + borrar). Verificado: /admin sin permiso redirige, APIs 403; el pipeline crear noticia -> se renderiza en la home (SSR) funciona. Turnstile bloquea el login por curl (correcto). Pendiente admin: precios de servicios, foros, sitios de voto, usuarios. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import type { RowDataPacket, ResultSetHeader } from 'mysql2'
|
||||
import { db, DB } from './db'
|
||||
|
||||
export interface NewsItem {
|
||||
id: number
|
||||
titulo: string
|
||||
fecha: string | null
|
||||
enlace: string | null
|
||||
}
|
||||
|
||||
export async function listNews(): Promise<NewsItem[]> {
|
||||
const [rows] = await db(DB.default).query<RowDataPacket[]>(
|
||||
'SELECT id, titulo, fecha_publicacion, enlace FROM home_noticia ORDER BY fecha_publicacion DESC',
|
||||
)
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
titulo: r.titulo,
|
||||
fecha: r.fecha_publicacion ? new Date(r.fecha_publicacion).toISOString() : null,
|
||||
enlace: r.enlace || null,
|
||||
}))
|
||||
}
|
||||
|
||||
export async function createNews(titulo: string, contenido: string, enlace: string | null): Promise<number> {
|
||||
const [res] = await db(DB.default).query<ResultSetHeader>(
|
||||
'INSERT INTO home_noticia (titulo, contenido, fecha_publicacion, enlace) VALUES (?, ?, NOW(), ?)',
|
||||
[titulo, contenido, enlace || null],
|
||||
)
|
||||
return res.insertId
|
||||
}
|
||||
|
||||
export async function deleteNews(id: number): Promise<void> {
|
||||
await db(DB.default).query('DELETE FROM home_noticia WHERE id = ?', [id])
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { RowDataPacket } from 'mysql2'
|
||||
import { db, DB } from './db'
|
||||
import { normalizeEmail } from './bnet'
|
||||
import type { SessionData } from './session'
|
||||
|
||||
/** ¿Es administrador? Por allowlist de email (ADMIN_EMAILS) o por gmlevel de AzerothCore. */
|
||||
export async function isAdmin(session: SessionData): Promise<boolean> {
|
||||
if (!session.bnetId) return false
|
||||
|
||||
const allow = (process.env.ADMIN_EMAILS || '')
|
||||
.split(',')
|
||||
.map((e) => normalizeEmail(e))
|
||||
.filter(Boolean)
|
||||
if (session.bnetEmail && allow.includes(normalizeEmail(session.bnetEmail))) return true
|
||||
|
||||
try {
|
||||
const level = Number(process.env.ADMIN_GMLEVEL || 3)
|
||||
const [rows] = await db(DB.auth).query<RowDataPacket[]>(
|
||||
'SELECT gmlevel FROM account_access WHERE id = ? ORDER BY gmlevel DESC LIMIT 1',
|
||||
[session.accountId ?? 0],
|
||||
)
|
||||
if (rows[0] && Number(rows[0].gmlevel) >= level) return true
|
||||
} catch {
|
||||
// account_access puede no existir (acore sin poblar)
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user