Files
harheimertc/scripts/setup-playstore-review.js
Torsten Schulz (local) c550fe8581
All checks were successful
Code Analysis and Production Deploy / analyze (push) Successful in 5m48s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Successful in 2m42s
fix: verbessere Benutzerzuweisung für Playstore-Review-Konto
2026-10-29 22:13:46 +01:00

45 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
import 'dotenv/config'
import { promises as fs } from 'node:fs'
import path from 'node:path'
import crypto from 'node:crypto'
import { decryptObject } from '../server/utils/encryption.js'
import { hashPassword, writeUsers } from '../server/utils/auth.js'
// Run in the target installation's root, with its encryption key loaded.
const email = String(process.env.PLAYSTORE_REVIEW_EMAIL || 'playstore-review@harheimertc.invalid').trim().toLowerCase()
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) throw new Error('Ungültige PLAYSTORE_REVIEW_EMAIL')
if (!process.env.ENCRYPTION_KEY) throw new Error('ENCRYPTION_KEY der Zielinstallation fehlt')
const root = process.cwd().endsWith('.output') ? path.resolve('..') : process.cwd()
const file = path.join(root, 'server/data/users.json')
// Fail closed: never replace an unreadable user database with an empty list.
const raw = await fs.readFile(file, 'utf8')
let users
try { users = JSON.parse(raw) } catch { users = decryptObject(raw, process.env.ENCRYPTION_KEY) }
if (!Array.isArray(users)) throw new Error('Ungültiges Format der Benutzerdaten')
let user = users.find(candidate => String(candidate.email || '').trim().toLowerCase() === email)
if (user && user.accountType !== 'playstore_review') throw new Error('E-Mail wird bereits von einem regulären Konto verwendet')
if (user && !process.env.PLAYSTORE_REVIEW_PASSWORD) {
console.log(`Review-Konto existiert bereits: ${email}. Passwort bleibt unverändert.`)
} else {
const password = process.env.PLAYSTORE_REVIEW_PASSWORD || crypto.randomBytes(24).toString('base64url')
if (password.length < 16) throw new Error('Review-Passwort muss mindestens 16 Zeichen haben')
if (!user) {
user = { id: crypto.randomUUID(), email, created: new Date().toISOString() }
users.push(user)
}
user.name = 'Google Play Review'
user.password = await hashPassword(password)
user.roles = ['mitglied']
user.active = true
user.hidden = true
user.accountType = 'playstore_review'
user.visibility = { showBirthday: false }
delete user.role
await fs.copyFile(file, `${file}.before-playstore-${Date.now()}`)
if (!await writeUsers(users)) throw new Error('Review-Konto konnte nicht gespeichert werden')
console.log(`E-Mail: ${email}`)
if (!process.env.PLAYSTORE_REVIEW_PASSWORD) console.log(`Passwort (sicher ablegen): ${password}`)
else console.log('Passwort: aus PLAYSTORE_REVIEW_PASSWORD gesetzt')
}