admin: bring back papertrail
Some checks reported warnings
CI / Check CI (pull_request) Has been cancelled

This commit is contained in:
radex 2024-07-08 17:40:06 +02:00
parent b4aff98ddd
commit 06e4b8f40e
Signed by: radex
SSH key fingerprint: SHA256:hvqRXAGG1h89yqnS+cyFTLKQbzjWD4uXIqw7Y+0ws30
2 changed files with 30 additions and 0 deletions

View file

@ -140,6 +140,33 @@ class AdminMixin:
return "User is protected"
return None
def _on_form_submit(
self, dn: str, conn: ldap.ldapobject, form: flask_wtf.FlaskForm
) -> None:
self._send_papertrail(form)
super()._on_form_submit(dn, conn, form)
def _send_papertrail(self, form: flask_wtf.FlaskForm) -> None:
attr_name = self.attr_name
old_value = self.attr.value.decode() if hasattr(self, "attr") else None
new_value = form.value.data if hasattr(form, "value") else None
profile = app.get_profile(self.dn)
assert profile is not None
title_prefix = {"add": "Adding", "mod": "Modifying", "del": "Deleting"}[
self.kind
]
new_text = f"New {attr_name}: {new_value}" if new_value else ""
old_text = f"Old {attr_name}: {old_value}" if old_value else ""
text = f"{old_text}\n{new_text}".strip()
email.send_papertrail(
f"{title_prefix} {attr_name} for user {profile.username}",
text,
)
class AdminOperationAdd(AdminMixin, vcard.OperationAdd):
pass

View file

@ -18,6 +18,7 @@ def test_connection_open(conn: smtplib.SMTP) -> bool:
def create_connection() -> smtplib.SMTP:
print("Connecting to SMTP...")
conn = smtplib.SMTP_SSL(config.smtp_server)
conn.login(config.smtp_user, config.smtp_password)
return conn
@ -47,6 +48,8 @@ def send_email(
def send_papertrail(title: str, description: str) -> None:
print("Sending papertrail: ", title, description)
username = flask.session.get("username")
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")