Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
settings: Add moderators-only option for invite_to_stream_policy.
sahil839bc42eab9250a
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex e4d0b652696..0ba6d29cd0a 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -3341,6 +3341,7 @@ class SubscriptionAPITest(ZulipTestCase):do_set_realm_property(realm, "invite_to_stream_policy", Realm.POLICY_ADMINS_ONLY, acting_user=None)+ do_change_user_role(self.test_user, UserProfile.ROLE_MODERATOR, acting_user=None)result = self.common_subscribe_to_streams(self.test_user,["stream1"],@@ -3354,6 +3355,29 @@ class SubscriptionAPITest(ZulipTestCase):self.test_user, ["stream1"], {"principals": orjson.dumps([invitee_user_id]).decode()})+ do_set_realm_property(+ realm, "invite_to_stream_policy", Realm.POLICY_MODERATORS_ONLY, acting_user=None+ )+ do_change_user_role(self.test_user, UserProfile.ROLE_MEMBER, acting_user=None)+ # Make sure that we are checking the permission with a full member,+ # as full member is the user just below moderator in the role hierarchy.+ self.assertFalse(self.test_user.is_provisional_member)+ result = self.common_subscribe_to_streams(+ self.test_user,+ ["stream2"],+ {"principals": orjson.dumps([invitee_user_id]).decode()},+ allow_fail=True,+ )+ self.assert_json_error(+ result, "Only administrators and moderators can modify other users' subscriptions."+ )++ do_change_user_role(self.test_user, UserProfile.ROLE_MODERATOR, acting_user=None)+ self.common_subscribe_to_streams(+ self.test_user, ["stream2"], {"principals": orjson.dumps([invitee_user_id]).decode()}+ )+ self.unsubscribe(user_profile, "stream2")+do_set_realm_property(realm, "invite_to_stream_policy", Realm.POLICY_MEMBERS_ONLY, acting_user=None)@@ -3408,6 +3432,14 @@ class SubscriptionAPITest(ZulipTestCase):do_set_realm_property(othello.realm, "invite_to_stream_policy", Realm.POLICY_ADMINS_ONLY, acting_user=None)+ do_change_user_role(othello, UserProfile.ROLE_MODERATOR, acting_user=None)+ self.assertFalse(othello.can_subscribe_other_users())++ do_set_realm_property(+ othello.realm, "invite_to_stream_policy", Realm.POLICY_MODERATORS_ONLY, acting_user=None+ )+ self.assertTrue(othello.can_subscribe_other_users())+do_change_user_role(othello, UserProfile.ROLE_MEMBER, acting_user=None)# Make sure that we are checking the permission with a full member,# as full member is the user just below admin in the role hierarchy.@@ -3435,6 +3467,8 @@ class SubscriptionAPITest(ZulipTestCase):)self.assertFalse(othello.can_subscribe_other_users())+ # Ensure that the new moderators can also create streams because moderator+ # being above the full member in role hierarchy.do_change_user_role(othello, UserProfile.ROLE_MODERATOR, acting_user=None)self.assertTrue(othello.can_subscribe_other_users())diff --git a/zerver/views/streams.py b/zerver/views/streams.pyindex 36ca0b9b29b..18fce81f606 100644--- a/zerver/views/streams.py+++ b/zerver/views/streams.py@@ -514,6 +514,10 @@ def add_subscriptions_backend(if not user_profile.can_subscribe_other_users():if user_profile.realm.invite_to_stream_policy == Realm.POLICY_ADMINS_ONLY:return json_error(_("Only administrators can modify other users' subscriptions."))+ if user_profile.realm.invite_to_stream_policy == Realm.POLICY_MODERATORS_ONLY:+ return json_error(+ _("Only administrators and moderators can modify other users' subscriptions.")+ )# Realm.POLICY_MEMBERS_ONLY only fails if the# user is a guest, which happens in the decorator above.assert user_profile.realm.invite_to_stream_policy == Realm.POLICY_FULL_MEMBERS_ONLY