From a6be1d10180b5b37b88199024d6d54d5d6542f80 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Thu, 25 May 2023 15:14:09 +0530 Subject: [PATCH] invites: Subscribe to default streams when not allowed to subscribe others. This commit changes the code to subscribe the invited user to default streams even if the user who invited the new user was not allowed to subscribe others to streams. --- api_docs/changelog.md | 7 +++++++ zerver/openapi/zulip.yaml | 16 ++++++++++++++++ zerver/tests/test_invite.py | 30 ++++++++++++++++++++++++++++-- zerver/views/invite.py | 23 +++++++++++++++++++---- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/api_docs/changelog.md b/api_docs/changelog.md index a54dd829cf..8fd75d43b5 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.yaml index f95a10058a..753f98a0d8 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 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. + **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 invitation is accepted. If unspecified, it will be set to empty list. If the list is empty, 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: array items: type: integer diff --git a/zerver/tests/test_invite.py b/zerver/tests/test_invite.py index a1ee93afaa..cc9d0581c4 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_name from zerver.actions.users import change_user_is_active from zerver.context_processors import common_context from 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_email from zerver.lib.streams import ensure_stream from 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.py index 4f93c6b981..03fb45f6f2 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_streams from zerver.lib.exceptions import InvitationError, JsonableError, OrganizationOwnerRequiredError from zerver.lib.request import REQ, has_request_variables from 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