Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
invites: Subscribe to default streams when not allowed to subscribe others.
Sahil Batraa6be1d10180b
diff --git a/api_docs/changelog.md b/api_docs/changelog.mdindex a54dd829cf7..8fd75d43b53 100644--- a/api_docs/changelog.md+++ b/api_docs/changelog.md@@ -20,6 +20,13 @@ format used by the Zulip server that they are interacting with.## Changes in Zulip 9.0+**Feature level 261**++* [`POST /invites`](/api/send-invites), [`POST+ /invites/multiuse`](/api/create-invite-link): Newly created user is+ now subscribed to the default streams in the organization even when+ the user sending the invite is not allowed to subscribe others.+**Feature level 260**:* [`PATCH /user_groups/{user_group_id}`](/api/update-user-group):diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex f95a10058a4..753f98a0d8d 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -12165,6 +12165,14 @@ paths:is accepted. If the list is empty, then the new user will not besubscribed to any streams.+ If the user is not allowed to subscribe others, then this list should+ be empty but the newly created user will still be subscribed to default+ streams in the organization.++ **Changes**: Prior to Zulip 9.0 (feature level 261), if the user was not+ allowed to subscribe others to stream, the newly created user was not+ subscribed to any streams.+**Changes**: Before Zulip 7.0 (feature level 180), specifying `stream_ids`as an empty list resulted in an error.type: array@@ -12304,6 +12312,14 @@ paths:newly created user will be automatically subscribed to if the invitationis accepted. If unspecified, it will be set to empty list. If the list isempty, then the new user will not be subscribed to any streams.++ If the user is not allowed to subscribe others, then this list should+ be empty but the newly created user will still be subscribed to default+ streams in the organization.++ **Changes**: Prior to Zulip 9.0 (feature level 261), if the user was not+ allowed to subscribe others to stream, the newly created user was not+ subscribed to any streams.type: arrayitems:type: integerdiff --git a/zerver/tests/test_invite.py b/zerver/tests/test_invite.pyindex a1ee93afaab..cc9d0581c4b 100644--- a/zerver/tests/test_invite.py+++ b/zerver/tests/test_invite.py@@ -46,7 +46,10 @@ from zerver.actions.user_settings import do_change_full_namefrom zerver.actions.users import change_user_is_activefrom zerver.context_processors import common_contextfrom zerver.lib.create_user import create_user-from zerver.lib.default_streams import get_default_streams_for_realm_as_dicts+from zerver.lib.default_streams import (+ get_default_streams_for_realm_as_dicts,+ get_slim_realm_default_streams,+)from zerver.lib.send_email import FromAddress, deliver_scheduled_emails, send_future_emailfrom zerver.lib.streams import ensure_streamfrom zerver.lib.test_classes import ZulipTestCase@@ -1298,9 +1301,17 @@ so we didn't send them an invitation. We did send invitations to everyone else!"result, "You do not have permission to subscribe other users to channels.")+ # User will be subscribed to default streams even when the+ # referrer does not have permission to subscribe others.result = self.invite(invitee, [])self.assert_json_success(result)self.check_sent_emails([invitee])++ self.submit_reg_form_for_user(invitee, "password")++ default_streams = get_slim_realm_default_streams(realm.id)+ self.assert_length(default_streams, 3)+ self.check_user_subscribed_only_to_streams("alice", default_streams)mail.outbox.pop()self.login("iago")@@ -1318,6 +1329,17 @@ so we didn't send them an invitation. We did send invitations to everyone else!"result = self.invite(invitee, ["Denmark", "Scotland"])self.assert_json_success(result)self.check_sent_emails([invitee])+ mail.outbox.pop()++ invitee = self.nonreg_email("test1")+ # User will not be subscribed to any streams, because the streams+ # list is empty when referrer has permission to subscribe others.+ result = self.invite(invitee, [])+ self.assert_json_success(result)+ self.check_sent_emails([invitee])++ self.submit_reg_form_for_user(invitee, "password")+ self.check_user_subscribed_only_to_streams("test1", [])def test_invitation_reminder_email(self) -> None:# All users belong to zulip realm@@ -2515,7 +2537,11 @@ class MultiuseInviteTest(ZulipTestCase):"invite_expires_in_minutes": 2 * 24 * 60,},)- self.assert_json_success(result)+ invite_link = self.assert_json_success(result)["invite_link"]+ self.check_user_able_to_register(self.nonreg_email("alice"), invite_link)+ default_streams = get_slim_realm_default_streams(realm.id)+ self.assert_length(default_streams, 3)+ self.check_user_subscribed_only_to_streams("alice", default_streams)self.login("iago")result = self.client_post(diff --git a/zerver/views/invite.py b/zerver/views/invite.pyindex 4f93c6b9816..03fb45f6f23 100644--- a/zerver/views/invite.py+++ b/zerver/views/invite.py@@ -16,6 +16,7 @@ from zerver.actions.invites import (do_send_user_invite_email,)from zerver.decorator import require_member_or_admin+from zerver.lib.default_streams import get_slim_realm_default_streamsfrom zerver.lib.exceptions import InvitationError, JsonableError, OrganizationOwnerRequiredErrorfrom zerver.lib.request import REQ, has_request_variablesfrom zerver.lib.response import json_success@@ -91,8 +92,15 @@ def invite_users_backend()streams.append(stream)- if len(streams) and not user_profile.can_subscribe_other_users():- raise JsonableError(_("You do not have permission to subscribe other users to channels."))+ if not user_profile.can_subscribe_other_users():+ if len(streams):+ raise JsonableError(+ _("You do not have permission to subscribe other users to channels.")+ )++ # We would subscribe the invited user to default streams even when the user+ # inviting them does not have permission to subscribe others.+ streams = get_slim_realm_default_streams(user_profile.realm_id)skipped = do_invite_users(user_profile,@@ -240,8 +248,15 @@ def generate_multiuse_invite_backend()streams.append(stream)- if len(streams) and not user_profile.can_subscribe_other_users():- raise JsonableError(_("You do not have permission to subscribe other users to channels."))+ if not user_profile.can_subscribe_other_users():+ if len(streams) != 0:+ raise JsonableError(+ _("You do not have permission to subscribe other users to channels.")+ )++ # We would subscribe the invited user to default streams even when the user+ # inviting them does not have permission to subscribe others.+ streams = get_slim_realm_default_streams(user_profile.realm_id)invite_link = do_create_multiuse_invite_link(user_profile, invite_as, invite_expires_in_minutes, streams