From 2cd6ee777f8c2d3531559588bcfb18627ffb5d2c Mon Sep 17 00:00:00 2001 From: Carlos Quintana <74399022+cquintana92@users.noreply.github.com> Date: Thu, 17 Oct 2024 09:55:04 +0200 Subject: [PATCH] chore: emit some missing contact audit logs (#2269) * chore: emit some missing contact audit logs * chore: improve alias change mailboxes audit message --- app/alias_mailbox_utils.py | 6 +++--- app/api/views/alias.py | 15 ++++++++++++++- tests/test_alias_mailbox_utils.py | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) 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})"