diff --git a/webapp/admin.py b/webapp/admin.py index aae9f4d..ad13e94 100644 --- a/webapp/admin.py +++ b/webapp/admin.py @@ -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 diff --git a/webapp/email.py b/webapp/email.py index 904ba3a..53bc8ce 100644 --- a/webapp/email.py +++ b/webapp/email.py @@ -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")