117 lines
4.0 KiB
Python
Executable File
117 lines
4.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import sqlite3
|
|
import getpass
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Versuche werkzeug zu importieren
|
|
try:
|
|
from werkzeug.security import generate_password_hash
|
|
except ImportError:
|
|
print("Fehler: werkzeug nicht gefunden. Installiere es mit: pip install werkzeug")
|
|
exit(1)
|
|
|
|
def main():
|
|
print("=== CMBNet Admin-Account erstellen ===\n")
|
|
|
|
# Datenbankpfad
|
|
db_path = Path(__file__).parent / 'instance' / 'cmbnet.sqlite3'
|
|
|
|
if not db_path.exists():
|
|
print(f"Fehler: Datenbank nicht gefunden: {db_path}")
|
|
print("Führe zuerst aus: flask --app cmbnet init-db")
|
|
exit(1)
|
|
|
|
conn = sqlite3.connect(db_path)
|
|
conn.row_factory = sqlite3.Row
|
|
cur = conn.cursor()
|
|
|
|
# Mandanten anzeigen
|
|
cur.execute("SELECT id, code, name FROM tenants ORDER BY code")
|
|
tenants = cur.fetchall()
|
|
|
|
if not tenants:
|
|
print("Keine Mandanten gefunden. Erstelle sie...")
|
|
cur.execute("INSERT OR IGNORE INTO tenants (code, name) VALUES ('BV', 'Bezirk BV')")
|
|
cur.execute("INSERT OR IGNORE INTO tenants (code, name) VALUES ('RV', 'Bezirk RV')")
|
|
cur.execute("INSERT OR IGNORE INTO tenants (code, name) VALUES ('SV', 'Bezirk SV')")
|
|
conn.commit()
|
|
cur.execute("SELECT id, code, name FROM tenants ORDER BY code")
|
|
tenants = cur.fetchall()
|
|
|
|
print("Verfügbare Mandanten:")
|
|
for t in tenants:
|
|
print(f" {t['code']} - {t['name']}")
|
|
print()
|
|
|
|
# Auswahl
|
|
tenant_code = input("Mandant (BV/RV/SV): ").strip().upper()
|
|
while tenant_code not in [t['code'] for t in tenants]:
|
|
print("Ungültiger Mandant!")
|
|
tenant_code = input("Mandant (BV/RV/SV): ").strip().upper()
|
|
|
|
cur.execute("SELECT id FROM tenants WHERE code = ?", (tenant_code,))
|
|
tenant_id = cur.fetchone()['id']
|
|
|
|
# Account-Daten
|
|
username = input("Benutzername: ").strip()
|
|
while not username:
|
|
print("Benutzername darf nicht leer sein!")
|
|
username = input("Benutzername: ").strip()
|
|
|
|
# Prüfe ob Account bereits existiert
|
|
cur.execute("SELECT id FROM accounts WHERE tenant_id = ? AND name = ?", (tenant_id, username))
|
|
if cur.fetchone():
|
|
print(f"Fehler: Account '{username}' existiert bereits für Mandant {tenant_code}!")
|
|
conn.close()
|
|
exit(1)
|
|
|
|
password = getpass.getpass("Passwort: ")
|
|
password_confirm = getpass.getpass("Passwort wiederholen: ")
|
|
|
|
while password != password_confirm:
|
|
print("Passwörter stimmen nicht überein!")
|
|
password = getpass.getpass("Passwort: ")
|
|
password_confirm = getpass.getpass("Passwort wiederholen: ")
|
|
|
|
while not password:
|
|
print("Passwort darf nicht leer sein!")
|
|
password = getpass.getpass("Passwort: ")
|
|
password_confirm = getpass.getpass("Passwort wiederholen: ")
|
|
while password != password_confirm:
|
|
print("Passwörter stimmen nicht überein!")
|
|
password = getpass.getpass("Passwort: ")
|
|
password_confirm = getpass.getpass("Passwort wiederholen: ")
|
|
|
|
# Rolle
|
|
print("\nRollen: Gast, Beobachter, Gruppe, Dispatcher, Administrator")
|
|
role = input("Rolle [Administrator]: ").strip()
|
|
if not role:
|
|
role = 'Administrator'
|
|
|
|
valid_roles = ['Gast', 'Beobachter', 'Gruppe', 'Dispatcher', 'Administrator']
|
|
while role not in valid_roles:
|
|
print("Ungültige Rolle!")
|
|
role = input("Rolle [Administrator]: ").strip()
|
|
if not role:
|
|
role = 'Administrator'
|
|
|
|
# Hash erstellen und Account anlegen
|
|
password_hash = generate_password_hash(password)
|
|
|
|
cur.execute(
|
|
"INSERT INTO accounts (tenant_id, name, password_hash, role) VALUES (?, ?, ?, ?)",
|
|
(tenant_id, username, password_hash, role)
|
|
)
|
|
conn.commit()
|
|
|
|
print(f"\n✅ Account erfolgreich erstellt!")
|
|
print(f" Mandant: {tenant_code}")
|
|
print(f" Benutzername: {username}")
|
|
print(f" Rolle: {role}")
|
|
|
|
conn.close()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|