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.
This commit is contained in:
Sahil Batra 2023-05-25 15:14:09 +05:30 committed by Tim Abbott
parent 4d407c6b8d
commit a6be1d1018
4 changed files with 70 additions and 6 deletions

View File

@ -20,6 +20,13 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 9.0 ## 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**: **Feature level 260**:
* [`PATCH /user_groups/{user_group_id}`](/api/update-user-group): * [`PATCH /user_groups/{user_group_id}`](/api/update-user-group):

View File

@ -12165,6 +12165,14 @@ paths:
is accepted. If the list is empty, then the new user will not be is accepted. If the list is empty, then the new user will not be
subscribed to any streams. 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` **Changes**: Before Zulip 7.0 (feature level 180), specifying `stream_ids`
as an empty list resulted in an error. as an empty list resulted in an error.
type: array type: array
@ -12304,6 +12312,14 @@ paths:
newly created user will be automatically subscribed to if the invitation 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 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. 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 type: array
items: items:
type: integer type: integer

View File

@ -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.actions.users import change_user_is_active
from zerver.context_processors import common_context from zerver.context_processors import common_context
from zerver.lib.create_user import create_user 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.send_email import FromAddress, deliver_scheduled_emails, send_future_email
from zerver.lib.streams import ensure_stream from zerver.lib.streams import ensure_stream
from zerver.lib.test_classes import ZulipTestCase 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." 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, []) result = self.invite(invitee, [])
self.assert_json_success(result) self.assert_json_success(result)
self.check_sent_emails([invitee]) 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() mail.outbox.pop()
self.login("iago") 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"]) result = self.invite(invitee, ["Denmark", "Scotland"])
self.assert_json_success(result) self.assert_json_success(result)
self.check_sent_emails([invitee]) 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: def test_invitation_reminder_email(self) -> None:
# All users belong to zulip realm # All users belong to zulip realm
@ -2515,7 +2537,11 @@ class MultiuseInviteTest(ZulipTestCase):
"invite_expires_in_minutes": 2 * 24 * 60, "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") self.login("iago")
result = self.client_post( result = self.client_post(

View File

@ -16,6 +16,7 @@ from zerver.actions.invites import (
do_send_user_invite_email, do_send_user_invite_email,
) )
from zerver.decorator import require_member_or_admin 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.exceptions import InvitationError, JsonableError, OrganizationOwnerRequiredError
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
@ -91,8 +92,15 @@ def invite_users_backend(
) )
streams.append(stream) streams.append(stream)
if len(streams) and not user_profile.can_subscribe_other_users(): if not user_profile.can_subscribe_other_users():
raise JsonableError(_("You do not have permission to subscribe other users to channels.")) 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( skipped = do_invite_users(
user_profile, user_profile,
@ -240,8 +248,15 @@ def generate_multiuse_invite_backend(
) )
streams.append(stream) streams.append(stream)
if len(streams) and not user_profile.can_subscribe_other_users(): if not user_profile.can_subscribe_other_users():
raise JsonableError(_("You do not have permission to subscribe other users to channels.")) 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( invite_link = do_create_multiuse_invite_link(
user_profile, invite_as, invite_expires_in_minutes, streams user_profile, invite_as, invite_expires_in_minutes, streams