Enhance authentication checks in CMS API endpoints; implement user role validation for admin and board access. Refactor Spielpläne API to remove unnecessary logging and improve error handling. Update tests to mock user authentication and ensure proper validation of file uploads.
This commit is contained in:
@@ -3,6 +3,7 @@ import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { exec } from 'child_process'
|
||||
import { promisify } from 'util'
|
||||
import { getUserFromToken } from '../../utils/auth.js'
|
||||
|
||||
const execAsync = promisify(exec)
|
||||
|
||||
@@ -51,6 +52,23 @@ export default defineEventHandler(async (event) => {
|
||||
})
|
||||
}
|
||||
|
||||
let token = getCookie(event, 'auth_token')
|
||||
const currentUser = token ? await getUserFromToken(token) : null
|
||||
|
||||
if (!currentUser) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Nicht authentifiziert'
|
||||
})
|
||||
}
|
||||
|
||||
if (currentUser.role !== 'admin' && currentUser.role !== 'vorstand') {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: 'Keine Berechtigung'
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
// Multer-Middleware für File-Upload
|
||||
await new Promise((resolve, reject) => {
|
||||
|
||||
@@ -1,8 +1,26 @@
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { getUserFromToken } from '../../utils/auth.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const token = getCookie(event, 'auth_token')
|
||||
const currentUser = token ? await getUserFromToken(token) : null
|
||||
|
||||
if (!currentUser) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Nicht authentifiziert'
|
||||
})
|
||||
}
|
||||
|
||||
if (currentUser.role !== 'admin' && currentUser.role !== 'vorstand') {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: 'Keine Berechtigung'
|
||||
})
|
||||
}
|
||||
|
||||
const { filename, content } = await readBody(event)
|
||||
|
||||
if (!filename || !content) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import multer from 'multer'
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { getUserFromToken } from '../../utils/auth.js'
|
||||
|
||||
// Multer-Konfiguration für PDF-Uploads
|
||||
const storage = multer.diskStorage({
|
||||
@@ -31,12 +32,35 @@ const upload = multer({
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Prüfe Authentifizierung
|
||||
const authHeader = getHeader(event, 'authorization')
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
let token = getCookie(event, 'auth_token')
|
||||
|
||||
if (!token) {
|
||||
const authHeader = getHeader(event, 'authorization')
|
||||
if (authHeader && authHeader.startsWith('Bearer ')) {
|
||||
token = authHeader.substring(7).trim()
|
||||
}
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Nicht autorisiert'
|
||||
statusMessage: 'Nicht authentifiziert'
|
||||
})
|
||||
}
|
||||
|
||||
const currentUser = await getUserFromToken(token)
|
||||
|
||||
if (!currentUser) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Nicht authentifiziert'
|
||||
})
|
||||
}
|
||||
|
||||
if (currentUser.role !== 'admin' && currentUser.role !== 'vorstand') {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: 'Keine Berechtigung'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -4,21 +4,16 @@ import path from 'path'
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const spielplaeneDir = path.join(process.cwd(), 'public', 'spielplaene')
|
||||
|
||||
console.log('=== SPIELPLÄNE API ===')
|
||||
console.log('Verzeichnis:', spielplaeneDir)
|
||||
|
||||
|
||||
// Prüfe, ob das Verzeichnis existiert
|
||||
try {
|
||||
await fs.access(spielplaeneDir)
|
||||
} catch {
|
||||
console.log('Verzeichnis nicht gefunden')
|
||||
return []
|
||||
}
|
||||
|
||||
// Lese alle Dateien im Verzeichnis
|
||||
const dateien = await fs.readdir(spielplaeneDir)
|
||||
console.log('Alle Dateien:', dateien)
|
||||
|
||||
// Filtere nur relevante Dateitypen
|
||||
const erlaubteExtensions = ['.pdf', '.xlsx', '.xls', '.doc', '.docx']
|
||||
@@ -26,10 +21,7 @@ export default defineEventHandler(async (event) => {
|
||||
const ext = path.extname(datei).toLowerCase()
|
||||
return erlaubteExtensions.includes(ext)
|
||||
})
|
||||
|
||||
console.log('Gefilterte Dateien:', gefiltert)
|
||||
console.log('Anzahl:', gefiltert.length)
|
||||
|
||||
|
||||
return gefiltert
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Lesen der Spielpläne:', error)
|
||||
|
||||
Reference in New Issue
Block a user