RealmAuditLog: Add old_value and new_value fields and update User_email_change

migrations lint fix
This commit is contained in:
thiagocamerato757 2024-11-03 14:24:28 -03:00
parent 5d1de4c037
commit 7db965b400
5 changed files with 46 additions and 9 deletions

View File

@ -114,6 +114,8 @@ def do_change_user_delivery_email(
) -> None: ) -> None:
delete_user_profile_caches([user_profile], user_profile.realm_id) 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 user_profile.delivery_email = new_email
if user_profile.email_address_is_realm_public(): if user_profile.email_address_is_realm_public():
user_profile.email = new_email user_profile.email = new_email
@ -121,22 +123,16 @@ def do_change_user_delivery_email(
else: else:
user_profile.save(update_fields=["delivery_email"]) 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) payload = dict(user_id=user_profile.id, delivery_email=new_email)
event = dict(type="realm_user", op="update", person=payload) event = dict(type="realm_user", op="update", person=payload)
delivery_email_visible_user_ids = get_users_with_access_to_real_email(user_profile) 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) send_event_on_commit(user_profile.realm, event, delivery_email_visible_user_ids)
if user_profile.avatar_source == UserProfile.AVATAR_FROM_GRAVATAR: 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) notify_avatar_url_change(user_profile)
if user_profile.email_address_is_realm_public(): 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) send_user_email_update_event(user_profile)
event_time = timezone_now() event_time = timezone_now()
@ -146,6 +142,8 @@ def do_change_user_delivery_email(
modified_user=user_profile, modified_user=user_profile,
event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_type=AuditLogEventType.USER_EMAIL_CHANGED,
event_time=event_time, 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},
) )

View File

@ -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),
),
]

View File

@ -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 = []

View File

@ -232,6 +232,9 @@ class RealmAuditLog(AbstractRealmAuditLog):
on_delete=CASCADE, on_delete=CASCADE,
) )
event_last_message_id = models.IntegerField(null=True) 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: class Meta:
ordering = ["id"] ordering = ["id"]

View File

@ -278,6 +278,7 @@ class TestRealmAuditLog(ZulipTestCase):
def test_change_email(self) -> None: def test_change_email(self) -> None:
now = timezone_now() now = timezone_now()
user = self.example_user("hamlet") user = self.example_user("hamlet")
old_email = user.delivery_email # store the old email
new_email = "test@example.com" new_email = "test@example.com"
do_change_user_delivery_email(user, new_email, acting_user=user) do_change_user_delivery_email(user, new_email, acting_user=user)
self.assertEqual( self.assertEqual(
@ -287,8 +288,6 @@ class TestRealmAuditLog(ZulipTestCase):
1, 1,
) )
self.assertEqual(new_email, user.delivery_email) self.assertEqual(new_email, user.delivery_email)
# Test the RealmAuditLog stringification
audit_entry = RealmAuditLog.objects.get( audit_entry = RealmAuditLog.objects.get(
event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time__gte=now event_type=AuditLogEventType.USER_EMAIL_CHANGED, event_time__gte=now
) )
@ -297,6 +296,9 @@ class TestRealmAuditLog(ZulipTestCase):
f"<RealmAuditLog: {AuditLogEventType.USER_EMAIL_CHANGED.name} " f"<RealmAuditLog: {AuditLogEventType.USER_EMAIL_CHANGED.name} "
) )
) )
# verify the extra_data field
self.assertEqual(audit_entry.extra_data["old_value"], old_email)
self.assertEqual(audit_entry.extra_data["new_value"], new_email)
def test_change_avatar_source(self) -> None: def test_change_avatar_source(self) -> None:
now = timezone_now() now = timezone_now()