import { apiDelete, apiGet, apiPost, apiPut, isAuthRedirectError } from '../api.js'; import { guard } from '../core/auth-guard.js'; import { esc } from '../core/utils.js'; const loadingEl = document.getElementById('page-loading'); const mainEl = document.getElementById('page-main'); const errEl = document.getElementById('page-error'); function showError(msg) { loadingEl.hidden = true; mainEl.hidden = true; errEl.hidden = false; errEl.textContent = msg; } function formatName(u) { const a = [u.firstName, u.lastName].filter(Boolean); return a.length ? a.map((x) => esc(String(x))).join(' ') : '—'; } function renderRows(users) { const tbody = document.getElementById('users-table-body'); tbody.innerHTML = users .map( (u) => ` ${esc(u.username)} ${formatName(u)} ${u.role === 'admin' ? 'Admin' : 'Benutzer'} ${u.source === 'ldap' ? 'LDAP' : 'Lokal'} ${u.active ? 'Ja' : 'Nein'} ${u.source === 'local' ? `` : ''} `, ) .join(''); } async function run() { const users = await apiGet('/users'); renderRows(users); document.getElementById('form-new-user').onsubmit = async (e) => { e.preventDefault(); const fd = new FormData(e.target); await apiPost('/users', { username: fd.get('username'), password: fd.get('password'), role: fd.get('role'), }); e.target.reset(); location.reload(); }; const root = document.getElementById('page-main'); root.querySelectorAll('.btn-pw').forEach((btn) => { btn.onclick = async () => { const uid = btn.getAttribute('data-id'); const pw = window.prompt('Neues Passwort (min. 8 Zeichen):'); if (!pw || pw.length < 8) return; await apiPut(`/users/${uid}`, { password: pw }); location.reload(); }; }); root.querySelectorAll('.btn-toggle').forEach((btn) => { btn.onclick = async () => { const uid = btn.getAttribute('data-id'); const active = btn.getAttribute('data-active') === '1'; await apiPut(`/users/${uid}`, { active: !active }); location.reload(); }; }); root.querySelectorAll('.btn-del-user').forEach((btn) => { btn.onclick = async () => { if (!window.confirm('Benutzer wirklich löschen?')) return; const uid = btn.getAttribute('data-id'); await apiDelete(`/users/${uid}`); location.reload(); }; }); } async function init() { const st = await guard({ needsAdmin: true, activeNav: 'users' }); if (!st) return; loadingEl.hidden = true; mainEl.hidden = false; try { await run(); } catch (e) { if (isAuthRedirectError(e)) return; showError(e.message || 'Fehler'); } } init();