From 2d67bf36895b1a1f13b4178eeb0a47ba0b42518b Mon Sep 17 00:00:00 2001 From: Carlos Quintana <74399022+cquintana92@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:17:47 +0200 Subject: [PATCH] feat: show audit logs on admin panel (#2273) * feat: show audit logs on admin panel * fix: make audit logs desc --- app/admin_model.py | 18 +++++++++-- templates/admin/email_search.html | 52 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/app/admin_model.py b/app/admin_model.py index 03830a39..1906019f 100644 --- a/app/admin_model.py +++ b/app/admin_model.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import Optional +from typing import Optional, List import arrow import sqlalchemy @@ -35,6 +35,8 @@ from app.models import ( DomainDeletedAlias, PartnerUser, AliasMailbox, + AliasAuditLog, + UserAuditLog, ) from app.newsletter_utils import send_newsletter_to_user, send_newsletter_to_address @@ -737,11 +739,13 @@ class InvalidMailboxDomainAdmin(SLModelView): class EmailSearchResult: no_match: bool = True alias: Optional[Alias] = None - mailbox: list[Mailbox] = [] + alias_audit_log: Optional[List[AliasAuditLog]] = None + mailbox: List[Mailbox] = [] mailbox_count: int = 0 deleted_alias: Optional[DeletedAlias] = None deleted_custom_alias: Optional[DomainDeletedAlias] = None user: Optional[User] = None + user_audit_log: Optional[List[UserAuditLog]] = None @staticmethod def from_email(email: str) -> EmailSearchResult: @@ -749,10 +753,20 @@ class EmailSearchResult: alias = Alias.get_by(email=email) if alias: output.alias = alias + output.alias_audit_log = ( + AliasAuditLog.filter_by(alias_id=alias.id) + .order_by(AliasAuditLog.created_at.desc()) + .all() + ) output.no_match = False user = User.get_by(email=email) if user: output.user = user + output.user_audit_log = ( + UserAuditLog.filter_by(user_id=user.id) + .order_by(UserAuditLog.created_at.desc()) + .all() + ) output.no_match = False mailboxes = ( Mailbox.filter_by(email=email).order_by(Mailbox.id.desc()).limit(10).all() diff --git a/templates/admin/email_search.html b/templates/admin/email_search.html index 8498fbae..da538ca5 100644 --- a/templates/admin/email_search.html +++ b/templates/admin/email_search.html @@ -154,6 +154,56 @@ {{ show_user(data.domain_deleted_alias.domain.user) }} {%- endmacro %} +{% macro list_alias_audit_log(alias_audit_log) %} +

Alias Audit Log

+ + + + + + + + + + + + + {% for entry in alias_audit_log %} + + + + + + + + + {% endfor %} + +
User IDAlias IDAlias EmailActionMessageTime
{{ entry.user_id }}{{ entry.alias_id }}{{ entry.alias_email }}{{ entry.action }}{{ entry.message }}{{ entry.created_at }}
+{% endmacro %} +{% macro list_user_audit_log(user_audit_log) %} +

User Audit Log

+ + + + + + + + + + + {% for entry in user_audit_log %} + + + + + + + {% endfor %} + +
User emailActionMessageTime
{{ entry.user_email }}{{ entry.action }}{{ entry.message }}{{ entry.created_at }}
+{% endmacro %} {% block body %}
@@ -177,6 +227,7 @@

Found Alias {{ data.alias.email }}

{{ list_alias(1,[data.alias]) }} + {{ list_alias_audit_log(data.alias_audit_log) }} {{ list_mailboxes("Mailboxes for alias", helper.alias_mailbox_count(data.alias), helper.alias_mailboxes(data.alias)) }} {{ show_user(data.alias.user) }}
@@ -188,6 +239,7 @@ {{ show_user(data.user) }} {{ list_mailboxes("Mailboxes for user", helper.mailbox_count(data.user) , helper.mailbox_list(data.user) ) }} {{ list_alias(helper.alias_count(data.user) ,helper.alias_list(data.user)) }} + {{ list_user_audit_log(data.user_audit_log) }}
{% endif %} {% if data.mailbox_count > 10 %}