Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
users: Grant access to deactivated DM-group partners consistently.
Sahil Batra29fbc01fa823
diff --git a/zerver/lib/users.py b/zerver/lib/users.pyindex b24fcd0e6c3..55cb6325059 100644--- a/zerver/lib/users.py+++ b/zerver/lib/users.py@@ -774,11 +774,14 @@ def check_can_access_user(recipient__type__in=[Recipient.STREAM, Recipient.DIRECT_MESSAGE_GROUP],).values_list("recipient_id", flat=True)+ # A user retains access to a deactivated user they+ # share a direct message group with.return Subscription.objects.filter(+ Q(recipient__type=Recipient.STREAM, is_user_active=True)+ | Q(recipient__type=Recipient.DIRECT_MESSAGE_GROUP),recipient_id__in=subscribed_recipient_ids,user_profile=target_user,active=True,- is_user_active=True,).exists()@@ -795,11 +798,14 @@ def get_inaccessible_users_queryset(recipient__type__in=[Recipient.STREAM, Recipient.DIRECT_MESSAGE_GROUP],).values("recipient_id")+ # Deactivated users remain accessible through a shared+ # direct message group.common_subscriptions = Subscription.objects.filter(+ Q(recipient__type=Recipient.STREAM, is_user_active=True)+ | Q(recipient__type=Recipient.DIRECT_MESSAGE_GROUP),recipient_id__in=acting_user_recipient_ids,user_profile_id=OuterRef("pk"),active=True,- is_user_active=True,)# All users can access all the bots, so we exclude them.target_human_users = UserProfile.objects.filter(id__in=target_user_ids, is_bot=False)diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 0d06ea94aa5..5b9e944e648 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -3812,6 +3812,7 @@ class GetProfileTest(ZulipTestCase):shiva = self.example_user("shiva")hamlet = self.example_user("hamlet")prospero = self.example_user("prospero")+ aaron = self.example_user("aaron")inaccessible_user_ids = get_inaccessible_user_ids([bot.id, hamlet.id, othello.id, shiva.id, prospero.id], polonius@@ -3835,6 +3836,18 @@ class GetProfileTest(ZulipTestCase):)self.assertEqual(inaccessible_user_ids, {othello.id})+ # A deactivated user remains accessible through a shared direct+ # message group (prospero, a 1:1 DM partner and aaron, a group DM+ # partner), but a deactivated user only sharing a stream (hamlet)+ # becomes inaccessible.+ do_deactivate_user(prospero, acting_user=None)+ do_deactivate_user(hamlet, acting_user=None)+ do_deactivate_user(aaron, acting_user=None)+ inaccessible_user_ids = get_inaccessible_user_ids(+ [hamlet.id, prospero.id, aaron.id], polonius+ )+ self.assertEqual(inaccessible_user_ids, {hamlet.id})+def test_get_inaccessible_user_ids_with_null_recipient(self) -> None:polonius = self.example_user("polonius")@@ -3857,13 +3870,28 @@ class GetProfileTest(ZulipTestCase):polonius = self.example_user("polonius")prospero = self.example_user("prospero")desdemona = self.example_user("desdemona")+ aaron = self.example_user("aaron")+ hamlet = self.example_user("hamlet")# prospero and polonius had personal messages historyself.assertTrue(check_can_access_user(prospero, polonius))+ # polonius and aaron shared a group direct message+ self.assertTrue(check_can_access_user(aaron, polonius))+# no personal messages history between desdemona and poloniusself.assertFalse(check_can_access_user(desdemona, polonius))+ # A guest keeps access to a deactivated user they share a direct+ # message group with (prospero and aaron), but loses access to a+ # deactivated user they only shared a stream with (hamlet).+ do_deactivate_user(prospero, acting_user=None)+ do_deactivate_user(hamlet, acting_user=None)+ do_deactivate_user(aaron, acting_user=None)+ self.assertTrue(check_can_access_user(prospero, polonius))+ self.assertTrue(check_can_access_user(aaron, polonius))+ self.assertFalse(check_can_access_user(hamlet, polonius))+def test_check_can_access_user_for_a_guest_with_themself(self) -> None:"""Test that the behaviour and query count are the same