realm: Create RealmAuditLog entry when removing realm playgrounds.

This commit also adds 'acting_user' parameter to
do_remove_realm_playground function.

Fixes a part of #21268.
This commit is contained in:
Sahil Batra
2022-03-11 20:10:42 +05:30
committed by Tim Abbott
parent dea3389045
commit b86b9bdc02
5 changed files with 57 additions and 3 deletions

View File

@@ -8135,9 +8135,31 @@ def do_add_realm_playground(
return realm_playground.id
def do_remove_realm_playground(realm: Realm, realm_playground: RealmPlayground) -> None:
def do_remove_realm_playground(
realm: Realm, realm_playground: RealmPlayground, *, acting_user: Optional[UserProfile]
) -> None:
removed_playground = {
"name": realm_playground.name,
"pygments_language": realm_playground.pygments_language,
"url_prefix": realm_playground.url_prefix,
}
realm_playground.delete()
realm_playgrounds = get_realm_playgrounds(realm)
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_PLAYGROUND_REMOVED,
event_time=timezone_now(),
extra_data=orjson.dumps(
{
"realm_playgrounds": realm_playgrounds,
"removed_playground": removed_playground,
}
).decode(),
)
notify_realm_playgrounds(realm, realm_playgrounds)

View File

@@ -4107,6 +4107,7 @@ class AbstractRealmAuditLog(models.Model):
REALM_DOMAIN_CHANGED = 219
REALM_DOMAIN_REMOVED = 220
REALM_PLAYGROUND_ADDED = 221
REALM_PLAYGROUND_REMOVED = 222
SUBSCRIPTION_CREATED = 301
SUBSCRIPTION_ACTIVATED = 302

View File

@@ -33,6 +33,7 @@ from zerver.lib.actions import (
do_reactivate_user,
do_regenerate_api_key,
do_remove_realm_domain,
do_remove_realm_playground,
do_rename_stream,
do_set_realm_authentication_methods,
do_set_realm_message_editing,
@@ -47,6 +48,7 @@ from zerver.models import (
Message,
Realm,
RealmAuditLog,
RealmPlayground,
Recipient,
Subscription,
UserProfile,
@@ -763,3 +765,30 @@ class TestRealmAuditLog(ZulipTestCase):
).count(),
1,
)
now = timezone_now()
realm_playground = RealmPlayground.objects.get(id=playground_id)
do_remove_realm_playground(
user.realm,
realm_playground,
acting_user=user,
)
removed_playground = {
"name": "Python playground",
"pygments_language": "Python",
"url_prefix": "https://python.example.com",
}
expected_extra_data = {
"realm_playgrounds": intial_playgrounds,
"removed_playground": removed_playground,
}
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_PLAYGROUND_REMOVED,
event_time__gte=now,
acting_user=user,
extra_data=orjson.dumps(expected_extra_data).decode(),
).count(),
1,
)

View File

@@ -1772,7 +1772,9 @@ class NormalActionsTest(BaseAction):
last_id = last_realm_playground.id
realm_playground = access_playground_by_id(self.user_profile.realm, last_id)
events = self.verify_action(
lambda: do_remove_realm_playground(self.user_profile.realm, realm_playground)
lambda: do_remove_realm_playground(
self.user_profile.realm, realm_playground, acting_user=None
)
)
check_realm_playgrounds("events[0]", events[0])

View File

@@ -62,5 +62,5 @@ def delete_realm_playground(
request: HttpRequest, user_profile: UserProfile, playground_id: int
) -> HttpResponse:
realm_playground = access_playground_by_id(user_profile.realm, playground_id)
do_remove_realm_playground(user_profile.realm, realm_playground)
do_remove_realm_playground(user_profile.realm, realm_playground, acting_user=user_profile)
return json_success(request)