2022-02-04 20:28:37 +00:00
|
|
|
from flask import (
|
|
|
|
render_template,
|
|
|
|
request,
|
|
|
|
flash,
|
|
|
|
redirect,
|
|
|
|
send_file,
|
|
|
|
Blueprint,
|
|
|
|
current_app,
|
|
|
|
)
|
2021-03-28 19:50:44 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
2022-02-04 20:28:37 +00:00
|
|
|
from flask_security import (
|
|
|
|
auth_required,
|
|
|
|
hash_password,
|
|
|
|
current_user,
|
|
|
|
roles_required
|
|
|
|
)
|
2021-04-17 12:26:04 +00:00
|
|
|
from flask_security.views import change_password
|
2021-04-10 11:46:56 +00:00
|
|
|
from email_validator import validate_email
|
2021-03-27 21:58:31 +00:00
|
|
|
|
2021-03-28 19:50:44 +00:00
|
|
|
import json
|
2021-03-27 21:58:31 +00:00
|
|
|
import secrets
|
2021-03-27 15:41:51 +00:00
|
|
|
from pathlib import Path
|
2021-03-28 19:50:44 +00:00
|
|
|
import tempfile
|
2021-03-15 21:51:51 +00:00
|
|
|
|
2021-08-24 16:48:54 +00:00
|
|
|
from datetime import date, datetime, timedelta
|
2021-03-15 21:51:51 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
from imaginaerraum_door_admin import db, security
|
2022-02-04 20:28:37 +00:00
|
|
|
from imaginaerraum_door_admin.forms import (
|
|
|
|
AdminCreationForm,
|
|
|
|
ConfirmDeleteForm,
|
|
|
|
TokenForm,
|
|
|
|
)
|
2022-01-29 22:48:58 +00:00
|
|
|
|
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
door_app = Blueprint("door_app", __name__, template_folder="templates")
|
2021-03-27 21:58:31 +00:00
|
|
|
|
2021-03-15 21:51:51 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
# we override the change_password view from flask security to only allow local
|
|
|
|
# users to change their passwords
|
2022-01-25 20:42:35 +00:00
|
|
|
# LDAP users should use the LDAP self service for changing passwords
|
|
|
|
# this route needs to be defined before the Flask Security setup
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/change", methods=["GET", "POST"])
|
2022-01-25 20:42:35 +00:00
|
|
|
@auth_required()
|
|
|
|
def change_pw():
|
2022-02-04 20:28:37 +00:00
|
|
|
if current_user.has_role("local"):
|
2022-01-25 20:42:35 +00:00
|
|
|
# local users can change their password
|
|
|
|
return change_password()
|
2021-04-10 11:46:56 +00:00
|
|
|
else:
|
2022-01-25 20:42:35 +00:00
|
|
|
# LDAP users get redirected to the LDAP self service
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("https://ldap.imaginaerraum.de/")
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-01-25 20:42:35 +00:00
|
|
|
# admin user management
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/manage_admins", methods=["GET", "POST"])
|
|
|
|
@roles_required("super_admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def manage_admins():
|
|
|
|
form = AdminCreationForm()
|
2022-02-04 20:28:37 +00:00
|
|
|
if request.method == "GET":
|
2022-01-29 22:48:58 +00:00
|
|
|
users = security.datastore.user_model.query.all()
|
2022-02-04 20:28:37 +00:00
|
|
|
admin_data = [
|
|
|
|
{
|
|
|
|
"username": u.username,
|
|
|
|
"email": u.email,
|
|
|
|
"active": u.is_active,
|
|
|
|
"admin": u.has_role("admin"),
|
|
|
|
"super_admin": u.has_role("super_admin"),
|
|
|
|
}
|
|
|
|
for u in users
|
|
|
|
]
|
|
|
|
return render_template("admins.html", admin_data=admin_data, form=form)
|
2022-01-25 20:42:35 +00:00
|
|
|
elif form.validate():
|
2022-02-04 20:28:37 +00:00
|
|
|
if (
|
|
|
|
security.datastore.find_user(username=form.name.data) is not None
|
|
|
|
or security.datastore.find_user(email=form.email.data) is not None
|
|
|
|
):
|
|
|
|
flash(
|
|
|
|
"Ein Benutzer mit diesem Nutzernamen oder dieser E-Mail-Adresse"
|
|
|
|
" existiert bereits!"
|
|
|
|
)
|
2021-03-28 19:50:44 +00:00
|
|
|
else:
|
2022-01-25 20:42:35 +00:00
|
|
|
pw = secrets.token_urlsafe(16)
|
2022-02-04 20:28:37 +00:00
|
|
|
new_user = security.datastore.create_user(
|
|
|
|
username=form.name.data,
|
|
|
|
email=form.email.data,
|
|
|
|
password=hash_password(pw),
|
|
|
|
)
|
|
|
|
security.datastore.add_role_to_user(new_user, "local")
|
2022-01-27 22:46:45 +00:00
|
|
|
current_app.logger.info(
|
2022-02-04 20:28:37 +00:00
|
|
|
f"Super admin {current_user.username} created new user account "
|
|
|
|
f"for {new_user.username} <{new_user.email}>"
|
|
|
|
)
|
|
|
|
flash(
|
|
|
|
f"Ein Account für den Nutzer {new_user.username} wurde "
|
|
|
|
f"erstellt. Verwende das Passwort {pw} um den Nutzer "
|
|
|
|
f"einzuloggen."
|
|
|
|
)
|
2022-01-25 20:42:35 +00:00
|
|
|
db.session.commit()
|
2022-01-29 22:48:58 +00:00
|
|
|
else:
|
|
|
|
flash(f"Ungültige Eingabe: {form.errors}")
|
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2021-03-27 21:58:31 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/delete_admins/<username>", methods=["GET", "POST"])
|
|
|
|
@roles_required("super_admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def delete_admins(username):
|
2022-01-29 22:48:58 +00:00
|
|
|
user = security.datastore.find_user(username=username)
|
2022-01-25 20:42:35 +00:00
|
|
|
if user is None:
|
|
|
|
flash(f"Ungültiger Nutzer {username}")
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
|
|
|
if user.has_role("super_admin"):
|
|
|
|
flash("Super-Admins können nicht gelöscht werden!")
|
|
|
|
return redirect("/manage_admins")
|
2022-01-25 20:42:35 +00:00
|
|
|
if user.is_active:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(
|
|
|
|
"Aktive Nutzer können nicht gelöscht werden! Bitte den Benutzer "
|
|
|
|
"zuerst deaktivieren."
|
|
|
|
)
|
|
|
|
return redirect("/manage_admins")
|
2021-03-15 21:51:51 +00:00
|
|
|
|
2022-01-25 20:42:35 +00:00
|
|
|
# set up form for confirming deletion
|
|
|
|
form = ConfirmDeleteForm()
|
|
|
|
form.name_confirm.data = username
|
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
if request.method == "GET":
|
2022-01-25 20:42:35 +00:00
|
|
|
# return page asking the user to confirm delete
|
2022-02-04 20:28:37 +00:00
|
|
|
return render_template("delete_user.html", username=username, form=form)
|
2022-01-25 20:42:35 +00:00
|
|
|
elif form.validate():
|
2022-01-29 22:48:58 +00:00
|
|
|
security.datastore.delete_user(user)
|
2022-01-25 20:42:35 +00:00
|
|
|
flash(f"Benutzer {username} wurde gelöscht.")
|
2022-02-04 20:28:37 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
f"Super admin {current_user.username} deleted admin user {username}"
|
|
|
|
)
|
2021-04-07 14:15:39 +00:00
|
|
|
db.session.commit()
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
2022-01-25 20:42:35 +00:00
|
|
|
else:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(
|
|
|
|
"Der eingegebene Nutzername stimmt nicht überein. Der Benutzer "
|
|
|
|
"wurde nicht gelöscht!"
|
|
|
|
)
|
|
|
|
return redirect("/manage_admins")
|
2021-04-07 14:15:39 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/admin_toggle_active/<username>")
|
|
|
|
@roles_required("super_admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def admin_toggle_active(username):
|
2022-01-29 22:48:58 +00:00
|
|
|
user = security.datastore.find_user(username=username)
|
2022-01-25 20:42:35 +00:00
|
|
|
if user is None:
|
|
|
|
flash(f"Ungültiger Nutzer {username}")
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
|
|
|
if user.has_role("super_admin"):
|
|
|
|
flash("Super-Admins können nicht deaktiviert werden!")
|
|
|
|
return redirect("/manage_admins")
|
2022-01-29 22:48:58 +00:00
|
|
|
security.datastore.toggle_active(user)
|
2022-01-25 20:42:35 +00:00
|
|
|
if user.is_active:
|
2022-02-04 20:28:37 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
f"Super admin {current_user.username} activated access for admin "
|
|
|
|
f"user {username}"
|
|
|
|
)
|
2022-01-25 20:42:35 +00:00
|
|
|
else:
|
2022-02-04 20:28:37 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
f"Super admin {current_user.username} deactivated access for admin "
|
|
|
|
f"user {username}"
|
|
|
|
)
|
2022-01-25 20:42:35 +00:00
|
|
|
db.session.commit()
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/promote_admin/<username>")
|
|
|
|
@roles_required("super_admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def promote_admin(username):
|
2022-01-29 22:48:58 +00:00
|
|
|
user = security.datastore.find_user(username=username)
|
2022-01-25 20:42:35 +00:00
|
|
|
if user is None:
|
|
|
|
flash(f"Ungültiger Nutzer {username}")
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
|
|
|
if user.has_role("admin"):
|
|
|
|
flash(f"Benutzer {username} hat bereits Admin-Rechte!")
|
|
|
|
return redirect("/manage_admins")
|
|
|
|
security.datastore.add_role_to_user(user, "admin")
|
|
|
|
current_app.logger.info(
|
|
|
|
f"Super admin {current_user.username} granted admin privileges to "
|
|
|
|
f"user {username}"
|
|
|
|
)
|
2022-01-25 20:42:35 +00:00
|
|
|
db.session.commit()
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-01-30 22:08:18 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/demote_admin/<username>")
|
|
|
|
@roles_required("super_admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def demote_admin(username):
|
2022-01-29 22:48:58 +00:00
|
|
|
user = security.datastore.find_user(username=username)
|
2022-01-25 20:42:35 +00:00
|
|
|
if user is None:
|
|
|
|
flash(f"Ungültiger Nutzer {username}")
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
|
|
|
if user.has_role("super_admin"):
|
|
|
|
flash(
|
|
|
|
f"Benutzer {username} hat Super-Admin-Rechte und kann nicht "
|
|
|
|
"verändert werden!"
|
|
|
|
)
|
|
|
|
return redirect("/manage_admins")
|
|
|
|
if user.has_role("admin"):
|
|
|
|
security.datastore.remove_role_from_user(user, "admin")
|
|
|
|
current_app.logger.info(
|
|
|
|
f"Super admin {current_user.username} revoked "
|
|
|
|
f"admin privileges of user {username}"
|
|
|
|
)
|
2022-01-25 20:42:35 +00:00
|
|
|
db.session.commit()
|
|
|
|
else:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(f"Benutzer {username} ist bereits kein Admin!")
|
|
|
|
return redirect("/manage_admins")
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/backup_user_datastore")
|
|
|
|
@roles_required("super_admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def backup_user_datastore():
|
|
|
|
# get list of defined admin users for backup
|
2022-01-29 22:48:58 +00:00
|
|
|
users = security.datastore.user_model.query.all()
|
2022-02-04 20:28:37 +00:00
|
|
|
user_data = [
|
|
|
|
{
|
|
|
|
"username": u.username,
|
|
|
|
"email": u.email,
|
|
|
|
"active": u.is_active,
|
|
|
|
"password_hash": u.password,
|
|
|
|
"roles": [r.name for r in u.roles],
|
|
|
|
}
|
|
|
|
for u in users
|
|
|
|
if not u.has_role("super_admin")
|
|
|
|
]
|
2022-01-25 20:42:35 +00:00
|
|
|
try:
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
2022-02-04 20:28:37 +00:00
|
|
|
file = Path(tmpdir, "user_data.txt")
|
2022-01-25 20:42:35 +00:00
|
|
|
file.write_text(json.dumps(user_data))
|
|
|
|
return send_file(file, as_attachment=True, cache_timeout=-1)
|
|
|
|
except Exception as e:
|
|
|
|
return str(e)
|
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/restore_user_datastore", methods=["POST"])
|
|
|
|
@roles_required("super_admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def restore_user_datastore():
|
|
|
|
# check if the post request has the file part
|
2022-02-04 20:28:37 +00:00
|
|
|
if "file" not in request.files:
|
|
|
|
flash("Keine Datei ausgewählt!")
|
2022-01-25 20:42:35 +00:00
|
|
|
return redirect(request.url)
|
2022-02-04 20:28:37 +00:00
|
|
|
file = request.files["file"]
|
2022-01-25 20:42:35 +00:00
|
|
|
# if user does not select file, browser also
|
|
|
|
# submit an empty part without filename
|
2022-02-04 20:28:37 +00:00
|
|
|
if file.filename == "":
|
|
|
|
flash("Keine Datei ausgewählt!")
|
|
|
|
return redirect("/manage_admins")
|
2022-01-25 20:42:35 +00:00
|
|
|
filename = secure_filename(file.filename)
|
2022-02-04 20:28:37 +00:00
|
|
|
if file and filename.endswith(".txt"):
|
2022-01-25 20:42:35 +00:00
|
|
|
data = file.stream.read()
|
2021-03-28 19:50:44 +00:00
|
|
|
try:
|
2022-01-25 20:42:35 +00:00
|
|
|
# check validity of user data
|
|
|
|
user_data = json.loads(data)
|
|
|
|
valid = type(user_data) == list
|
|
|
|
valid &= all(type(d) == dict for d in user_data)
|
|
|
|
if valid:
|
|
|
|
for d in user_data:
|
2022-02-04 20:28:37 +00:00
|
|
|
entry_valid = set(d.keys()) == {
|
|
|
|
"active",
|
|
|
|
"email",
|
|
|
|
"password_hash",
|
|
|
|
"username",
|
|
|
|
"roles",
|
|
|
|
}
|
|
|
|
entry_valid &= all(
|
|
|
|
len(d[key]) > 0
|
|
|
|
for key in ["email", "password_hash", "username"]
|
|
|
|
)
|
|
|
|
entry_valid &= type(d["active"]) == bool
|
|
|
|
entry_valid &= type(d["roles"]) == list
|
|
|
|
validate_email(d["email"])
|
2022-01-25 20:42:35 +00:00
|
|
|
if entry_valid:
|
2022-02-04 20:28:37 +00:00
|
|
|
existing_user = security.datastore.find_user(
|
|
|
|
username=d["username"], email=d["email"]
|
|
|
|
)
|
2022-01-25 20:42:35 +00:00
|
|
|
if existing_user is None:
|
2022-02-04 20:28:37 +00:00
|
|
|
security.datastore.create_user(
|
|
|
|
username=d["username"],
|
|
|
|
email=d["email"],
|
|
|
|
password=d["password_hash"],
|
|
|
|
active=d["active"],
|
|
|
|
roles=d["roles"],
|
|
|
|
)
|
|
|
|
flash(
|
|
|
|
f"Account für Benutzer '{d['username']} wurde "
|
|
|
|
f"wiederhergestellt."
|
|
|
|
)
|
2022-01-25 20:42:35 +00:00
|
|
|
else:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(
|
|
|
|
f"Benutzer '{d['username']} existiert bereits."
|
|
|
|
f" Eintrag wird übersprungen."
|
|
|
|
)
|
2022-01-25 20:42:35 +00:00
|
|
|
else:
|
|
|
|
raise ValueError(f"Ungültige Daten für User Entry {d}")
|
|
|
|
else:
|
|
|
|
raise ValueError("Admin User Datei hat ungültiges Format.")
|
2021-03-28 19:50:44 +00:00
|
|
|
except Exception as e:
|
2022-01-25 20:42:35 +00:00
|
|
|
flash(f"Die Datei konnte nicht gelesen werden. Exception: {e}")
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
2022-01-25 20:42:35 +00:00
|
|
|
flash("Benutzer aus Datei gelesen.")
|
|
|
|
db.session.commit()
|
|
|
|
else:
|
|
|
|
flash("Ungültige Dateiendung")
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/manage_admins")
|
2022-01-29 22:48:58 +00:00
|
|
|
|
|
|
|
|
2022-01-25 20:42:35 +00:00
|
|
|
# main page
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/")
|
2022-01-25 20:42:35 +00:00
|
|
|
def door_lock():
|
2022-02-04 20:28:37 +00:00
|
|
|
return render_template(
|
|
|
|
"index.html",
|
|
|
|
door_state=current_app.door.state,
|
|
|
|
encoder_position=current_app.door.encoder_position,
|
|
|
|
)
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-01-25 20:42:35 +00:00
|
|
|
|
|
|
|
# token overview
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/tokens")
|
|
|
|
@roles_required("admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def list_tokens():
|
2022-01-27 22:46:45 +00:00
|
|
|
tokens = current_app.door.get_tokens()
|
2022-02-04 20:28:37 +00:00
|
|
|
assigned_tokens = {t: data for t, data in tokens.items()
|
|
|
|
if not data["inactive"]}
|
|
|
|
inactive_tokens = {t: data for t, data in tokens.items()
|
|
|
|
if data["inactive"]}
|
|
|
|
return render_template(
|
|
|
|
"tokens.html",
|
|
|
|
assigned_tokens=assigned_tokens,
|
|
|
|
inactive_tokens=inactive_tokens
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@door_app.route("/token-log")
|
|
|
|
@roles_required("super_admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def token_log():
|
|
|
|
log = []
|
|
|
|
try:
|
2022-02-04 20:28:37 +00:00
|
|
|
with open(current_app.config["NFC_LOG"]) as f:
|
2022-01-25 20:42:35 +00:00
|
|
|
log += f.readlines()
|
|
|
|
log.reverse()
|
2022-02-04 20:28:37 +00:00
|
|
|
log = [l.split(" - ") for l in log]
|
|
|
|
return render_template("token_log.html", log=log)
|
2022-01-25 20:42:35 +00:00
|
|
|
except Exception as e:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(
|
|
|
|
f"NFC logfile {Path(current_app.config['NFC_LOG']).absolute()} "
|
|
|
|
f"konnte nicht gelesen werden. Exception: {e}"
|
|
|
|
)
|
|
|
|
return redirect("/")
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-01-30 22:08:18 +00:00
|
|
|
def store_token(token_data):
|
|
|
|
"""Store token to the token file on disk.
|
|
|
|
|
|
|
|
This will use the token id and the associated data and create/modify a
|
|
|
|
token and store the new token file to disk.
|
|
|
|
"""
|
2022-02-04 20:28:37 +00:00
|
|
|
token = token_data["token"]
|
2022-01-30 22:08:18 +00:00
|
|
|
tokens = current_app.door.get_tokens()
|
2022-02-04 20:28:37 +00:00
|
|
|
tokens[token] = {
|
|
|
|
"name": token_data["name"],
|
|
|
|
"email": token_data["email"],
|
|
|
|
"valid_thru": token_data["valid_thru"],
|
|
|
|
"inactive": token_data["inactive"],
|
|
|
|
"organization": token_data["organization"],
|
|
|
|
}
|
2022-01-30 22:08:18 +00:00
|
|
|
try:
|
|
|
|
current_app.door.store_tokens(tokens)
|
2022-02-04 20:28:37 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
f"Token {token} stored in database by admin user "
|
|
|
|
f"{current_user.username}"
|
|
|
|
)
|
2022-01-30 22:08:18 +00:00
|
|
|
except Exception as e:
|
|
|
|
flash(f"Error during store_tokens. Exception: {e}")
|
|
|
|
|
|
|
|
|
2022-01-25 20:42:35 +00:00
|
|
|
# routes for registering, editing and deleting tokens
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/register-token", methods=["GET", "POST"])
|
|
|
|
@roles_required("admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def register():
|
|
|
|
"""Register new token for locking and unlocking the door.
|
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
This route displays the most recently scanned invalid token as reported in
|
|
|
|
the logfile and provides a form for entering user info (name, email, valid
|
|
|
|
thru date (optional)) for the new token.
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
If the route is called via POST the provided form data is checked and if
|
|
|
|
the check succeeds the /store-token route will be called which adds the new
|
|
|
|
token to the database.
|
2022-01-25 20:42:35 +00:00
|
|
|
"""
|
2022-01-27 22:46:45 +00:00
|
|
|
token = current_app.door.get_most_recent_token()
|
2022-01-25 20:42:35 +00:00
|
|
|
|
|
|
|
recent_token = {}
|
2022-02-04 20:28:37 +00:00
|
|
|
if {"token", "timestamp"}.issubset(set(token.keys())):
|
|
|
|
dt = datetime.utcnow() - token["timestamp"]
|
2022-01-30 22:08:18 +00:00
|
|
|
if dt < timedelta(minutes=10):
|
2022-01-25 20:42:35 +00:00
|
|
|
recent_token = token
|
2022-02-04 20:28:37 +00:00
|
|
|
recent_token["timedelta_minutes"] = int(dt.total_seconds() / 60.0)
|
2022-01-25 20:42:35 +00:00
|
|
|
|
|
|
|
form = TokenForm()
|
2022-02-04 20:28:37 +00:00
|
|
|
if request.method == "GET":
|
|
|
|
# set default valid thru date to today to make sure form validity check
|
|
|
|
# passes
|
2022-01-25 20:42:35 +00:00
|
|
|
# (will not be used if limited validity is disabled)
|
|
|
|
form.valid_thru.data = date.today()
|
2022-02-04 20:28:37 +00:00
|
|
|
elif request.method == "POST" and form.validate():
|
2022-01-30 22:08:18 +00:00
|
|
|
token_data = {
|
2022-02-04 20:28:37 +00:00
|
|
|
"token": current_app.door.get_most_recent_token()["token"],
|
|
|
|
"name": form.name.data,
|
|
|
|
"email": form.email.data,
|
|
|
|
"organization": form.organization.data,
|
|
|
|
"inactive": not form.active.data,
|
|
|
|
"valid_thru": form.valid_thru.data.isoformat()
|
|
|
|
if form.limit_validity.data
|
|
|
|
else "",
|
2022-01-30 22:08:18 +00:00
|
|
|
}
|
|
|
|
store_token(token_data)
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/tokens")
|
2022-01-25 20:42:35 +00:00
|
|
|
else:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(f"Token konnte nicht registiert werden. Fehler: {form.errors}")
|
|
|
|
return render_template("register.html", token=recent_token, form=form)
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/edit-token/<token>", methods=["GET", "POST"])
|
|
|
|
@roles_required("admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def edit_token(token):
|
2022-02-04 20:28:37 +00:00
|
|
|
"""Edit data in the token file (name, email, valid_thru date, active).
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
If the route is accessed via GET it will provide a form for editing the
|
|
|
|
currently stored data for the user.
|
|
|
|
If the route is accessed via POST it will check if the form data is good
|
|
|
|
and then store the modified user data in the database (by redirecting to
|
|
|
|
the /store-token route)
|
2022-01-25 20:42:35 +00:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
token : str
|
|
|
|
The token for which data should be edited.
|
|
|
|
"""
|
|
|
|
form = TokenForm(request.form)
|
2022-02-04 20:28:37 +00:00
|
|
|
form.dsgvo.validators = (
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
# we skip the validation of the DSGVO checkbox here because we assume
|
|
|
|
# the user agreed to it before
|
|
|
|
if request.method == "GET":
|
2022-01-27 22:46:45 +00:00
|
|
|
tokens = current_app.door.get_tokens()
|
2022-01-25 20:42:35 +00:00
|
|
|
if token in tokens:
|
|
|
|
# set default for form according to values from the token file
|
|
|
|
et = tokens[token]
|
2022-02-04 20:28:37 +00:00
|
|
|
form.active.data = not et["inactive"]
|
|
|
|
form.name.data = et["name"] if et["name"] else ""
|
|
|
|
form.email.data = et["email"] if et["email"] else ""
|
|
|
|
form.organization.data = et["organization"] \
|
|
|
|
if et["organization"] else ""
|
|
|
|
|
|
|
|
# for the valid thru date we use today's date in case there is not
|
|
|
|
# valid date in the database
|
2022-01-25 20:42:35 +00:00
|
|
|
try:
|
2022-02-04 20:28:37 +00:00
|
|
|
form.valid_thru.data = date.fromisoformat(et["valid_thru"])
|
2022-01-25 20:42:35 +00:00
|
|
|
form.limit_validity.data = True
|
|
|
|
except Exception:
|
|
|
|
form.valid_thru.data = date.today()
|
2021-08-24 16:48:54 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
return render_template("edit.html", token=token, form=form)
|
2022-01-25 20:42:35 +00:00
|
|
|
else:
|
2022-02-04 20:28:37 +00:00
|
|
|
# flash an error message if the route is accessed with an invalid
|
|
|
|
# token
|
|
|
|
flash(f"Ungültiger Token {token}!")
|
|
|
|
return redirect("/tokens")
|
|
|
|
elif request.method == "POST":
|
2022-01-25 20:42:35 +00:00
|
|
|
if form.validate():
|
2022-01-30 22:08:18 +00:00
|
|
|
# store data in token_data cookie
|
2022-02-04 20:28:37 +00:00
|
|
|
token_data = {
|
|
|
|
"token": token,
|
|
|
|
"name": form.name.data,
|
|
|
|
"organization": form.organization.data,
|
|
|
|
"email": form.email.data,
|
|
|
|
"inactive": not form.active.data,
|
|
|
|
"valid_thru": form.valid_thru.data.isoformat()
|
|
|
|
if form.limit_validity.data
|
|
|
|
else "",
|
|
|
|
}
|
2022-01-30 22:08:18 +00:00
|
|
|
store_token(token_data)
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/tokens")
|
2021-03-15 21:51:51 +00:00
|
|
|
else:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(f"Token konnte nicht editiert werden. Fehler: {form.errors}")
|
|
|
|
return render_template("edit.html", token=token, form=form)
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/delete-token/<token>", methods=["GET", "POST"])
|
|
|
|
@roles_required("admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def delete_token(token):
|
2022-02-04 20:28:37 +00:00
|
|
|
"""Delete the given token from the token file and store the new token file.
|
2021-03-15 21:51:51 +00:00
|
|
|
|
2022-01-25 20:42:35 +00:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
token : str
|
|
|
|
The token to delete from the database.
|
|
|
|
"""
|
2022-01-27 22:46:45 +00:00
|
|
|
tokens = current_app.door.get_tokens()
|
2021-03-15 21:51:51 +00:00
|
|
|
|
2022-01-31 21:27:38 +00:00
|
|
|
if token not in tokens:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(f"Ungültiger Token {token} für Löschung.")
|
|
|
|
return redirect("/tokens")
|
2021-03-15 21:51:51 +00:00
|
|
|
|
2022-01-31 21:27:38 +00:00
|
|
|
token_to_delete = tokens[token]
|
|
|
|
|
|
|
|
# set up form for confirming deletion
|
|
|
|
form = ConfirmDeleteForm()
|
2022-02-04 20:28:37 +00:00
|
|
|
form.name_confirm.data = token_to_delete["name"]
|
2022-01-31 21:27:38 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
if request.method == "GET":
|
2022-01-31 21:27:38 +00:00
|
|
|
# return page asking the user to confirm delete
|
2022-02-04 20:28:37 +00:00
|
|
|
return render_template("delete.html", token=token_to_delete, form=form)
|
2022-01-31 21:27:38 +00:00
|
|
|
elif form.validate():
|
|
|
|
# form validation successful -> can delete the token
|
|
|
|
tokens.pop(token)
|
|
|
|
try:
|
|
|
|
current_app.door.store_tokens(tokens)
|
2022-02-04 20:28:37 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
f"Token {token} was deleted from database "
|
|
|
|
f"by admin user {current_user.username}"
|
|
|
|
)
|
2022-01-31 21:27:38 +00:00
|
|
|
except Exception as e:
|
|
|
|
flash(f"Error during store_tokens. Exception: {e}")
|
|
|
|
flash(f"Token {token} wurde gelöscht!")
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/tokens")
|
2022-01-31 21:27:38 +00:00
|
|
|
else:
|
|
|
|
# form validation failed -> return to token overview and flash message
|
|
|
|
flash(
|
|
|
|
f"Der eingegebene Name stimmt nicht überein. Error: {form.errors}"
|
|
|
|
f"Der Token {token} von {token_to_delete['name']} wurde nicht "
|
|
|
|
"gelöscht."
|
|
|
|
)
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/tokens")
|
2022-01-31 21:27:38 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/deactivate-token/<token>")
|
|
|
|
@roles_required("admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def deactivate_token(token):
|
2022-02-04 20:28:37 +00:00
|
|
|
"""Deactivate access for the given token.
|
|
|
|
|
|
|
|
This updates the token file on disk.
|
2022-01-25 20:42:35 +00:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
token : str
|
|
|
|
The token to deactivate.
|
|
|
|
"""
|
2022-01-27 22:46:45 +00:00
|
|
|
tokens = current_app.door.get_tokens()
|
2022-01-31 21:27:38 +00:00
|
|
|
|
|
|
|
if token not in tokens:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(f"Ungültiger Token {token} für Deaktivierung.")
|
|
|
|
return redirect("/tokens")
|
2022-01-31 21:27:38 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
tokens[token]["inactive"] = True
|
2022-01-31 21:27:38 +00:00
|
|
|
try:
|
|
|
|
current_app.door.store_tokens(tokens)
|
2022-02-04 20:28:37 +00:00
|
|
|
current_app.logger.info(
|
|
|
|
f"Token {token} deactivated by admin user {current_user.username}"
|
|
|
|
)
|
2022-01-31 21:27:38 +00:00
|
|
|
except Exception as e:
|
|
|
|
flash(f"Error during store_tokens. Exception: {e}")
|
2022-02-04 20:28:37 +00:00
|
|
|
return redirect("/tokens")
|
2021-03-21 14:53:33 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/backup_tokens")
|
|
|
|
@roles_required("admin")
|
2022-01-25 20:42:35 +00:00
|
|
|
def backup_tokens():
|
|
|
|
# get list of defined admin users for backup
|
2022-01-27 22:46:45 +00:00
|
|
|
tokens = current_app.door.get_tokens()
|
2022-01-25 20:42:35 +00:00
|
|
|
try:
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
2022-02-04 20:28:37 +00:00
|
|
|
file = Path(tmpdir, "token_data.txt")
|
2022-01-25 20:42:35 +00:00
|
|
|
file.write_text(json.dumps(tokens))
|
|
|
|
return send_file(file, as_attachment=True, cache_timeout=-1)
|
|
|
|
except Exception as e:
|
|
|
|
return str(e)
|
2021-03-21 14:53:33 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/open")
|
2022-01-25 20:42:35 +00:00
|
|
|
@auth_required()
|
|
|
|
def open_door():
|
|
|
|
try:
|
2022-01-27 22:46:45 +00:00
|
|
|
current_app.door.open_door(user=current_user.username)
|
2022-02-04 20:28:37 +00:00
|
|
|
current_app.logger.info(f"Door opened by admin user "
|
|
|
|
f"{current_user.username}")
|
2022-01-25 20:42:35 +00:00
|
|
|
except Exception as e:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(f"Could not open door. Exception: {e}")
|
|
|
|
return redirect("/")
|
2021-03-27 21:58:31 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
|
2022-01-25 20:42:35 +00:00
|
|
|
# routes for opening and closing the door via the web interface
|
2022-02-04 20:28:37 +00:00
|
|
|
@door_app.route("/close")
|
2022-01-25 20:42:35 +00:00
|
|
|
@auth_required()
|
|
|
|
def close_door():
|
|
|
|
try:
|
2022-01-27 22:46:45 +00:00
|
|
|
current_app.door.close_door(user=current_user.username)
|
2022-02-04 20:28:37 +00:00
|
|
|
current_app.logger.info(f"Door closed by admin user "
|
|
|
|
f"{current_user.username}")
|
2022-01-25 20:42:35 +00:00
|
|
|
except Exception as e:
|
2022-02-04 20:28:37 +00:00
|
|
|
flash(f"Could not close door. Exception: {e}")
|
|
|
|
return redirect("/")
|