audit_log: Log RealmAuditLog when stream is deactivated.

This commit is contained in:
arpit551 2020-06-29 18:32:07 +05:30 committed by Tim Abbott
parent e0c9b0fdff
commit 6af337c07e
5 changed files with 23 additions and 4 deletions

View File

@ -892,7 +892,7 @@ def do_deactivate_user(user_profile: UserProfile,
for profile in bot_profiles:
do_deactivate_user(profile, acting_user=acting_user, _cascade=False)
def do_deactivate_stream(stream: Stream, log: bool=True) -> None:
def do_deactivate_stream(stream: Stream, log: bool=True, acting_user: Optional[UserProfile]=None) -> None:
# Get the affected user ids *before* we deactivate everybody.
affected_user_ids = can_access_stream_user_ids(stream)
@ -940,6 +940,11 @@ def do_deactivate_stream(stream: Stream, log: bool=True) -> None:
streams=[stream_dict])
send_event(stream.realm, event, affected_user_ids)
event_time = timezone_now()
RealmAuditLog.objects.create(realm=stream.realm, acting_user=acting_user,
modified_stream=stream, event_type=RealmAuditLog.STREAM_DEACTIVATED,
event_time=event_time)
def send_user_email_update_event(user_profile: UserProfile) -> None:
payload = dict(user_id=user_profile.id,
new_email=user_profile.email)
@ -3113,7 +3118,7 @@ def bulk_remove_subscriptions(users: Iterable[UserProfile],
if new_vacant_private_streams:
# Deactivate any newly-vacant private streams
for stream in new_vacant_private_streams:
do_deactivate_stream(stream)
do_deactivate_stream(stream, acting_user=acting_user)
return (
[(sub.user_profile, stream) for (sub, stream) in subs_to_deactivate],

View File

@ -69,7 +69,7 @@ class Command(ZulipBaseCommand):
bulk_remove_subscriptions([sub.user_profile for sub in subs_to_deactivate],
[stream_to_destroy],
self.get_client(), acting_user=None)
do_deactivate_stream(stream_to_destroy)
do_deactivate_stream(stream_to_destroy, acting_user=None)
if len(users_to_activate) > 0:
print(f"Adding {len(users_to_activate)} subscriptions")
bulk_add_subscriptions([stream_to_keep], users_to_activate)

View File

@ -2744,6 +2744,7 @@ class AbstractRealmAuditLog(models.Model):
CUSTOMER_SWITCHED_FROM_MONTHLY_TO_ANNUAL_PLAN = 503
STREAM_CREATED = 601
STREAM_DEACTIVATED = 602
event_type: int = models.PositiveSmallIntegerField()

View File

@ -18,6 +18,7 @@ from zerver.lib.actions import (
do_change_user_role,
do_create_user,
do_deactivate_realm,
do_deactivate_stream,
do_deactivate_user,
do_reactivate_realm,
do_reactivate_user,
@ -222,3 +223,15 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(RealmAuditLog.objects.filter(realm=realm, event_type=RealmAuditLog.STREAM_CREATED,
event_time__gte=now, acting_user=user,
modified_stream=stream).count(), 1)
def test_deactivate_stream(self) -> None:
now = timezone_now()
realm = get_realm('zulip')
user = self.example_user('hamlet')
stream_name = 'test'
stream = self.make_stream(stream_name, realm)
do_deactivate_stream(stream, acting_user=user)
self.assertEqual(RealmAuditLog.objects.filter(realm=realm, event_type=RealmAuditLog.STREAM_DEACTIVATED,
event_time__gte=now, acting_user=user,
modified_stream=stream).count(), 1)
self.assertEqual(stream.deactivated, True)

View File

@ -137,7 +137,7 @@ def deactivate_stream_backend(request: HttpRequest,
user_profile: UserProfile,
stream_id: int) -> HttpResponse:
stream = access_stream_for_delete_or_update(user_profile, stream_id)
do_deactivate_stream(stream)
do_deactivate_stream(stream, acting_user=user_profile)
return json_success()
@require_realm_admin