diff --git a/srv/poe_manager/app.py b/srv/poe_manager/app.py index cf76918..f9e9ea0 100644 --- a/srv/poe_manager/app.py +++ b/srv/poe_manager/app.py @@ -311,58 +311,103 @@ def toggle_device(mac): def switches(): conn = get_db_connection() - # Inline-Add + # 🔒 Admin-Prüfung + if request.method == 'POST' and not current_user.is_admin: + flash("Zugriff verweigert!") + conn.close() + return redirect(url_for('switches')) + + # ------------------------- + # 🔹 SWITCH HINZUFÜGEN + # ------------------------- if request.method == 'POST' and 'add_switch' in request.form: - if not current_user.is_admin: - flash("Zugriff verweigert!") - return redirect(url_for('switches')) - hostname = request.form['hostname'] - ip = request.form['ip'] - username = request.form['username'] - password = encrypt_password(request.form['password']) - try: - conn.execute("INSERT INTO switches (hostname, ip, username, password) VALUES (?, ?, ?, ?)", - (hostname, ip, username, password)) - conn.commit() - flash(f"Switch {hostname} hinzugefügt.") - except sqlite3.IntegrityError: - flash("Hostname existiert bereits oder Eingabefehler!") + hostname = request.form['hostname'].strip() + ip = request.form['ip'].strip() + username = request.form['username'].strip() + password = encrypt_password(request.form['password'].strip()) - # Inline-Edit - if request.method == 'POST' and 'edit_switch' in request.form: - if not current_user.is_admin: - flash("Zugriff verweigert!") + # Prüfung auf doppelte Hostnamen + existing_host = conn.execute( + "SELECT hostname FROM switches WHERE hostname = ?", (hostname,) + ).fetchone() + if existing_host: + flash(f"Hostname '{hostname}' existiert bereits!") + conn.close() return redirect(url_for('switches')) + + # Prüfung auf doppelte IP + existing_ip = conn.execute( + "SELECT hostname FROM switches WHERE ip = ?", (ip,) + ).fetchone() + if existing_ip: + flash(f"IP-Adresse {ip} wird bereits vom Switch '{existing_ip['hostname']}' verwendet!") + conn.close() + return redirect(url_for('switches')) + + # Einfügen + conn.execute( + "INSERT INTO switches (hostname, ip, username, password) VALUES (?, ?, ?, ?)", + (hostname, ip, username, password) + ) + conn.commit() + flash(f"Switch '{hostname}' erfolgreich hinzugefügt.") + + # ------------------------- + # 🔹 SWITCH BEARBEITEN + # ------------------------- + elif request.method == 'POST' and 'edit_switch' in request.form: old_hostname = request.form['old_hostname'] - hostname = request.form['hostname'] - ip = request.form['ip'] - username = request.form['username'] - password = encrypt_password(request.form['password']) - try: - conn.execute(""" - UPDATE switches - SET hostname=?, ip=?, username=?, password=? - WHERE hostname=? - """, (hostname, ip, username, password, old_hostname)) - conn.commit() - flash(f"Switch {hostname} aktualisiert.") - except sqlite3.IntegrityError: - flash("Hostname existiert bereits oder Eingabefehler!") + hostname = request.form['hostname'].strip() + ip = request.form['ip'].strip() + username = request.form['username'].strip() + password = encrypt_password(request.form['password'].strip()) - # Inline-Delete - if request.method == 'POST' and 'delete_switch' in request.form: - if not current_user.is_admin: - flash("Zugriff verweigert!") + # Hostname prüfen + existing_host = conn.execute( + "SELECT hostname FROM switches WHERE hostname = ? AND hostname != ?", + (hostname, old_hostname) + ).fetchone() + if existing_host: + flash(f"Hostname '{hostname}' existiert bereits für einen anderen Switch!") + conn.close() return redirect(url_for('switches')) + + # IP prüfen + existing_ip = conn.execute( + "SELECT hostname FROM switches WHERE ip = ? AND hostname != ?", + (ip, old_hostname) + ).fetchone() + if existing_ip: + flash(f"IP-Adresse {ip} wird bereits vom Switch '{existing_ip['hostname']}' verwendet!") + conn.close() + return redirect(url_for('switches')) + + # Update durchführen + conn.execute(""" + UPDATE switches + SET hostname=?, ip=?, username=?, password=? + WHERE hostname=? + """, (hostname, ip, username, password, old_hostname)) + conn.commit() + flash(f"Switch '{hostname}' erfolgreich aktualisiert.") + + # ------------------------- + # 🔹 SWITCH LÖSCHEN + # ------------------------- + elif request.method == 'POST' and 'delete_switch' in request.form: del_hostname = request.form['delete_switch'] conn.execute("UPDATE devices SET switch_hostname=NULL WHERE switch_hostname=?", (del_hostname,)) conn.execute("DELETE FROM switches WHERE hostname=?", (del_hostname,)) conn.commit() - flash(f"Switch {del_hostname} gelöscht.") + flash(f"Switch '{del_hostname}' gelöscht.") - switches = conn.execute("SELECT hostname, ip, username FROM switches").fetchall() + # ------------------------- + # 🔹 SWITCHES LADEN + # ------------------------- + switches = conn.execute("SELECT hostname, ip, username FROM switches ORDER BY hostname ASC").fetchall() conn.close() - return render_template('switche.html', switches=switches) + return render_template('switches.html', switches=switches) + @app.route("/get_log") @login_required