From 7db965b4001b71f61c4a1b6dc79e30bf817d19c8 Mon Sep 17 00:00:00 2001 From: thiagocamerato757 Date: Sun, 3 Nov 2024 14:24:28 -0300 Subject: [PATCH] RealmAuditLog: Add old_value and new_value fields and update User_email_change migrations lint fix --- zerver/actions/user_settings.py | 12 +++++----- ...itlog_new_value_realmauditlog_old_value.py | 22 +++++++++++++++++++ zerver/migrations/0625_merge_20241117_1821.py | 12 ++++++++++ zerver/models/realm_audit_logs.py | 3 +++ zerver/tests/test_audit_log.py | 6 +++-- 5 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 zerver/migrations/0624_realmauditlog_new_value_realmauditlog_old_value.py create mode 100644 zerver/migrations/0625_merge_20241117_1821.py diff --git a/zerver/actions/user_settings.py b/zerver/actions/user_settings.py index 708cc41967..9026318ef6 100644 --- a/zerver/actions/user_settings.py +++ b/zerver/actions/user_settings.py @@ -114,6 +114,8 @@ def do_change_user_delivery_email( ) -> None: delete_user_profile_caches([user_profile], user_profile.realm_id) + old_email = user_profile.delivery_email # Armazene o email antigo + user_profile.delivery_email = new_email if user_profile.email_address_is_realm_public(): user_profile.email = new_email @@ -121,22 +123,16 @@ def do_change_user_delivery_email( else: user_profile.save(update_fields=["delivery_email"]) - # We notify all the users who have access to delivery email. + # Notificar os usuários com acesso ao email de entrega. payload = dict(user_id=user_profile.id, delivery_email=new_email) event = dict(type="realm_user", op="update", person=payload) delivery_email_visible_user_ids = get_users_with_access_to_real_email(user_profile) - send_event_on_commit(user_profile.realm, event, delivery_email_visible_user_ids) if user_profile.avatar_source == UserProfile.AVATAR_FROM_GRAVATAR: - # If the user is using Gravatar to manage their email address, - # their Gravatar just changed, and we need to notify other - # clients. notify_avatar_url_change(user_profile) if user_profile.email_address_is_realm_public(): - # Additionally, if we're also changing the publicly visible - # email, we send a new_email event as well. send_user_email_update_event(user_profile) event_time = timezone_now() @@ -146,6 +142,8 @@ def do_change_user_delivery_email( modified_user=user_profile, event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time=event_time, + # store the old and new email in the extra_data field + extra_data={"old_value": old_email, "new_value": new_email}, ) diff --git a/zerver/migrations/0624_realmauditlog_new_value_realmauditlog_old_value.py b/zerver/migrations/0624_realmauditlog_new_value_realmauditlog_old_value.py new file mode 100644 index 0000000000..6b21785772 --- /dev/null +++ b/zerver/migrations/0624_realmauditlog_new_value_realmauditlog_old_value.py @@ -0,0 +1,22 @@ +# Generated by Django 5.0.9 on 2024-11-03 17:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("zerver", "0623_merge_20241030_1835"), + ] + + operations = [ + migrations.AddField( + model_name="realmauditlog", + name="new_value", + field=models.TextField(blank=True, null=True), + ), + migrations.AddField( + model_name="realmauditlog", + name="old_value", + field=models.TextField(blank=True, null=True), + ), + ] diff --git a/zerver/migrations/0625_merge_20241117_1821.py b/zerver/migrations/0625_merge_20241117_1821.py new file mode 100644 index 0000000000..5cf15299e5 --- /dev/null +++ b/zerver/migrations/0625_merge_20241117_1821.py @@ -0,0 +1,12 @@ +# Generated by Django 5.0.9 on 2024-11-17 18:21 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("zerver", "0624_alter_realmexport_tarball_size_bytes"), + ("zerver", "0624_realmauditlog_new_value_realmauditlog_old_value"), + ] + + operations = [] diff --git a/zerver/models/realm_audit_logs.py b/zerver/models/realm_audit_logs.py index a9487ea197..b824777eca 100644 --- a/zerver/models/realm_audit_logs.py +++ b/zerver/models/realm_audit_logs.py @@ -232,6 +232,9 @@ class RealmAuditLog(AbstractRealmAuditLog): on_delete=CASCADE, ) event_last_message_id = models.IntegerField(null=True) + # This field is used to store the old and new values of the field that was changed. + old_value = models.TextField(null=True, blank=True) + new_value = models.TextField(null=True, blank=True) class Meta: ordering = ["id"] diff --git a/zerver/tests/test_audit_log.py b/zerver/tests/test_audit_log.py index ab846e52eb..4c031fafbb 100644 --- a/zerver/tests/test_audit_log.py +++ b/zerver/tests/test_audit_log.py @@ -278,6 +278,7 @@ class TestRealmAuditLog(ZulipTestCase): def test_change_email(self) -> None: now = timezone_now() user = self.example_user("hamlet") + old_email = user.delivery_email # store the old email new_email = "test@example.com" do_change_user_delivery_email(user, new_email, acting_user=user) self.assertEqual( @@ -287,8 +288,6 @@ class TestRealmAuditLog(ZulipTestCase): 1, ) self.assertEqual(new_email, user.delivery_email) - - # Test the RealmAuditLog stringification audit_entry = RealmAuditLog.objects.get( event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time__gte=now ) @@ -297,6 +296,9 @@ class TestRealmAuditLog(ZulipTestCase): f" None: now = timezone_now()