1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-08 21:21:06 +00:00

Remove admin or assistant instance if role was removed and there is no dependency

This commit is contained in:
Mo 2022-09-19 17:05:16 +02:00
parent adbe8d0eb7
commit cf433cb692

View file

@ -255,8 +255,10 @@ class UserView(SecureAdminModelView):
def on_model_change(self, form, model, is_created):
if not is_created:
if model == current_user:
# Prevent locking out
if not form.active.data:
raise ModelViewException("Tried to deactivate yourself as user!")
if not model.has_role("admin"):
raise ModelViewException("Tried to remove your admin role!")
@ -268,16 +270,35 @@ class UserView(SecureAdminModelView):
model, password, notify=False
) # Password is automatically hashed with this function
if model.has_role("assistant") and model.assistant is None:
user_assistant = model.assistant
if model.has_role("assistant"):
if user_assistant is None:
# Create assistant instance after new role assignment
semester_experiments = form.semester_experiments.data if form.semester_experiments else []
assistant = Assistant(user=model, semester_experiments=semester_experiments)
self.session.add(assistant)
elif (
user_assistant is not None
and not user_assistant.semester_experiments
and not user_assistant.appointments
and not user_assistant.experiment_marks
):
# Delete assistant instance if there is no dependency
# Useful for undoing an unwanted role assignment
self.session.delete(user_assistant)
if model.has_role("admin") and model.admin is None:
flash("Admin role was assigned!", "danger")
user_admin = model.admin
if model.has_role("admin"):
if user_admin is None:
# Create admin instance after new role assignment
flash(f"Admin role was assigned to {model}!", "danger")
admin = Admin(user=model)
self.session.add(admin)
elif user_admin is not None and not user_admin.experiment_marks:
# Delete admin instance if there is no dependency
# Useful for undoing an unwanted role assignment
self.session.delete(user_admin)
# Lower email
model.email = model.email.lower()