-
Notifications
You must be signed in to change notification settings - Fork 0
/
🔑_login.py
149 lines (121 loc) · 4.78 KB
/
🔑_login.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
login page
"""
import datetime
import json
import locale
import streamlit as st
import streamlit_authenticator as stauth
from dotenv import load_dotenv
from streamlit_lottie import st_lottie
from modules import def_dics as dics
from modules import global_variables as gv
from modules import streamlit_menus as sm
from modules import user_authentication as uauth
locale.setlocale(locale.LC_ALL, "")
# setup
PAGE = st.session_state["page"] = "login"
sm.page_setup(PAGE)
@dics.timer()
def load_lottie_file(path: str) -> json:
"""Load a Lottie animation by providing a json file"""
with open(path) as file:
return json.load(file)
col1, col2 = st.columns(2)
with col1:
# user authentication
load_dotenv(".streamlit/secrets.toml")
if "li_all_users" not in st.session_state:
st.session_state["li_all_users"] = uauth.list_all_users()
# users = st.session_state["li_all_users"]
if "dic_credentials" not in st.session_state:
st.session_state["dic_credentials"] = {
"usernames": {
user["key"]: {
"name": user["name"],
"email": user["email"],
"password": user["password"],
}
for user in st.session_state["li_all_users"]
}
}
if "authenticator" not in st.session_state:
st.session_state["authenticator"] = stauth.Authenticate(
credentials=st.session_state["dic_credentials"],
cookie_name="utec_tools",
key="uauth",
cookie_expiry_days=30,
)
name, authentication_status, username = st.session_state["authenticator"].login(
"Login", "main"
)
if authentication_status:
gv.users = st.session_state["li_all_users"]
gv.access_lvl_user = [
u["access_lvl"]
for u in gv.users
if u["key"] == st.session_state.get("username")
][0]
st.session_state["access_lvl"] = gv.access_lvl_user
if gv.access_lvl_user in ("god", "full"):
st.session_state["access_pages"] = list(dics.PAGES.keys())
st.session_state["access_until"] = datetime.date.max
else:
st.session_state["access_pages"] = gv.access_lvl_user
st.session_state["access_until"] = [
datetime.datetime.strptime(u["access_until"], "%Y-%m-%d").date()
for u in gv.users
if u["key"] == st.session_state.get("username")
][0]
if st.session_state.get("username") in ("utec"):
st.markdown(
"""
Du bist mit dem allgemeinen UTEC-Account angemeldet. \n \n
Viel Spaß mit den Tools!
"""
)
else:
st.markdown(
f"""
Angemeldet als "{st.session_state.get('name')}"
"""
)
if st.session_state.get("access_until") < datetime.date.max:
st.markdown(
f"""
Mit diesem Account kann auf folgende Module bis zum {st.session_state['access_until']:%d.%m.%Y} zugegriffen werden:
"""
)
else:
st.markdown(
"""
Mit diesem Account kann auf folgende Module zugegriffen werden:
"""
)
for page in st.session_state["access_pages"]:
if page != "login":
st.markdown(f"- {dics.PAGES[page]['page_tit']}")
st.markdown("###")
st.session_state["authenticator"].logout("Logout", "main")
if not isinstance(gv.access_lvl_user, list) and gv.access_lvl_user in ("god"):
sm.user_accounts()
# neuen Benutzer eintragen
if st.session_state.get("butt_sub_new_user"):
with st.spinner("Momentle bitte, Benutzer wird hinzugefügt..."):
uauth.insert_new_user(
st.session_state.get("new_user_user"),
st.session_state.get("new_user_name"),
st.session_state.get("new_user_email"),
st.session_state.get("new_user_pw"),
st.session_state.get("new_user_access"),
str(st.session_state.get("new_user_until")),
)
# Benutzer löschen
if st.session_state.get("butt_sub_del_user"):
uauth.delete_user()
elif authentication_status is None:
st.warning("Bitte Benutzernamen und Passwort eingeben")
elif authentication_status is False:
st.error("Benutzername oder Passwort falsch")
with col2:
st_lottie(load_lottie_file("animations/login.json"), height=450, key="lottie_login")