diff --git a/app/alias_mailbox_utils.py b/app/alias_mailbox_utils.py index c0e1b01a..1388460c 100644 --- a/app/alias_mailbox_utils.py +++ b/app/alias_mailbox_utils.py @@ -53,9 +53,9 @@ def set_mailboxes_for_alias( AliasMailbox.create(alias_id=alias.id, mailbox_id=mailbox.id) emit_alias_audit_log( - alias, - AliasAuditLogAction.ChangedMailboxes, - message=",".join(map(str, mailbox_ids)), + alias=alias, + action=AliasAuditLogAction.ChangedMailboxes, + message=",".join([f"{mailbox.id} ({mailbox.email})" for mailbox in mailboxes]), ) return None diff --git a/app/api/views/alias.py b/app/api/views/alias.py index d81747fa..e91556cd 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -1,3 +1,5 @@ +from typing import Optional + from deprecated import deprecated from flask import g from flask import jsonify @@ -29,6 +31,7 @@ from app.errors import ( from app.extensions import limiter from app.log import LOG from app.models import Alias, Contact, Mailbox, AliasDeleteReason +from app.user_audit_log_utils import emit_user_audit_log, UserAuditLogAction @deprecated @@ -447,11 +450,16 @@ def delete_contact(contact_id): 200 """ user = g.user - contact = Contact.get(contact_id) + contact: Optional[Contact] = Contact.get(contact_id) if not contact or contact.alias.user_id != user.id: return jsonify(error="Forbidden"), 403 + emit_user_audit_log( + user=user, + action=UserAuditLogAction.DeleteContact, + message=f"Deleted contact {contact_id} ({contact.email})", + ) Contact.delete(contact_id) Session.commit() @@ -475,6 +483,11 @@ def toggle_contact(contact_id): return jsonify(error="Forbidden"), 403 contact.block_forward = not contact.block_forward + emit_user_audit_log( + user=user, + action=UserAuditLogAction.UpdateContact, + message=f"Set contact state {contact.id} {contact.email} -> {contact.website_email} to blocked {contact.block_forward}", + ) Session.commit() return jsonify(block_forward=contact.block_forward), 200 diff --git a/tests/test_alias_mailbox_utils.py b/tests/test_alias_mailbox_utils.py index 889b799a..004e891a 100644 --- a/tests/test_alias_mailbox_utils.py +++ b/tests/test_alias_mailbox_utils.py @@ -67,4 +67,4 @@ def test_set_mailboxes_for_alias_mailbox_success(): assert len(audit_logs) == 2 assert audit_logs[0].action == AliasAuditLogAction.CreateAlias.value assert audit_logs[1].action == AliasAuditLogAction.ChangedMailboxes.value - assert audit_logs[1].message == f"{mb1.id},{mb2.id}" + assert audit_logs[1].message == f"{mb1.id} ({mb1.email}),{mb2.id} ({mb2.email})"