Rollen Chunk 7: Benutzerverwaltung und Sicherung strikt Admin-only
Docker Image / test (push) Successful in 1m30s
Docker Image / image (push) Successful in 31s

This commit is contained in:
loki committed 2026-10-11 20:35:58 +02:00
1 parent 3a898fb7ab
commit 4d0c7527ea
1 file changed
+42
+42
View File
@@ -80,6 +80,48 @@ export function userExists(db: Db, userId: string): boolean {
return row !== undefined;
}
export function countAdmins(db: Db): number {
const row = db
.prepare("SELECT COUNT(*) AS count FROM \"user\" WHERE role = 'admin'")
.get() as { count: number };
return row.count;
}
export function setUserRole(db: Db, userId: string, role: UserRole): boolean {
const result = db
.prepare('UPDATE "user" SET role = ?, updatedAt = ? WHERE id = ?')
.run(role, Date.now(), userId);
return result.changes > 0;
}
/** Konto samt aller Zeitdaten loeschen (Admin). Gibt false bei unbekannter ID. */
export function deleteUserAccount(db: Db, userId: string): boolean {
const remove = db.transaction((): boolean => {
const user = db
.prepare('DELETE FROM "user" WHERE id = ?')
.run(userId);
if (user.changes === 0) {
return false;
}
for (const table of [
"work_entries",
"manual_adjustments",
"sick_days",
"vacation_requests",
"vacation_days",
"preferences",
"weekly_targets",
"year_settings",
]) {
db.prepare(`DELETE FROM ${table} WHERE user_id = ?`).run(userId);
}
db.prepare("DELETE FROM session WHERE userId = ?").run(userId);
db.prepare("DELETE FROM account WHERE userId = ?").run(userId);
return true;
});
return remove();
}
export interface TeamUser {
id: string;
name: string;