108 lines
3.4 KiB
Python
108 lines
3.4 KiB
Python
import streamlit as st
|
|
import yaml
|
|
from yaml.loader import SafeLoader
|
|
import streamlit_authenticator as stauth
|
|
from streamlit_authenticator.utilities.exceptions import LoginError
|
|
from auth import (
|
|
load_credentials_from_db,
|
|
needs_password_change,
|
|
update_password,
|
|
get_role_for_user,
|
|
get_fullname_for_user,
|
|
get_sidebar
|
|
)
|
|
|
|
|
|
st.markdown("""
|
|
<style>
|
|
/* Streamlit Hamburger-Menü ausblenden */
|
|
div[data-testid="stToolbar"] {
|
|
visibility: hidden !important;
|
|
}
|
|
|
|
/* Optional: ganz entfernen statt nur unsichtbar machen */
|
|
div[data-testid="stDecoration"] {
|
|
display: none !important;
|
|
}
|
|
</style>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
def get_authenticator():
|
|
with open("config/auth.yaml", "r", encoding="utf-8") as f:
|
|
base_config = yaml.load(f, Loader=SafeLoader)
|
|
|
|
db_creds = load_credentials_from_db() # Hier wird name, email und password geladen
|
|
base_config["credentials"] = db_creds
|
|
|
|
authenticator = stauth.Authenticate(
|
|
base_config["credentials"],
|
|
base_config["cookie"]["name"],
|
|
base_config["cookie"]["key"],
|
|
base_config["cookie"]["expiry_days"],
|
|
)
|
|
return authenticator
|
|
|
|
|
|
def require_login():
|
|
"""Sicherstellen, dass der User eingeloggt ist. Auf JEDER Page verwenden."""
|
|
authenticator = get_authenticator()
|
|
|
|
try:
|
|
authenticator.login(location="main", key="Login")
|
|
except LoginError:
|
|
authenticator.logout("ForceLogout", "sidebar")
|
|
st.error("Sitzung ungültig. Bitte neu einloggen.")
|
|
st.stop()
|
|
|
|
auth_status = st.session_state.get("authentication_status")
|
|
if auth_status is False:
|
|
st.error("Login fehlgeschlagen.")
|
|
st.stop()
|
|
if auth_status is None:
|
|
st.warning("Bitte Benutzername und Passwort eingeben.")
|
|
st.stop()
|
|
|
|
#--------------------------------------------------------------------------------------
|
|
# Ab hier bin ich eingeloggt
|
|
#--------------------------------------------------------------------------------------
|
|
|
|
username = st.session_state.get("username")
|
|
|
|
if "df_sidebar" not in st.session_state:
|
|
role_text = get_role_for_user(username)
|
|
fullname = get_fullname_for_user(username)
|
|
sidebar = get_sidebar(role_text, username)
|
|
|
|
st.session_state["role_text"] = role_text
|
|
st.session_state["fullname"] = fullname
|
|
st.session_state["df_sidebar"] = sidebar
|
|
|
|
# Passwortwechsel erzwingen
|
|
|
|
if needs_password_change(username):
|
|
st.warning("Du musst dein Passwort ändern, bevor du die Anwendung nutzen kannst.")
|
|
|
|
with st.form("pw_change_form"):
|
|
pw1 = st.text_input("Neues Passwort", type="password")
|
|
pw2 = st.text_input("Neues Passwort (Wiederholung)", type="password")
|
|
submitted = st.form_submit_button("Passwort ändern")
|
|
|
|
if submitted:
|
|
if not pw1 or not pw2:
|
|
st.error("Bitte beide Passwortfelder ausfüllen.")
|
|
st.stop()
|
|
if pw1 != pw2:
|
|
st.error("Passwörter stimmen nicht überein.")
|
|
st.stop()
|
|
if len(pw1) < 8:
|
|
st.error("Passwort sollte mindestens 8 Zeichen lang sein.")
|
|
st.stop()
|
|
|
|
update_password(username, pw1, reset_flag=True)
|
|
st.success("Passwort wurde geändert.")
|
|
st.rerun()
|
|
|
|
st.stop()
|
|
|
|
return authenticator |