Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

stream: Use `can_create_topic_group` to check history privacy.

Saubhagya Patela07a3de26b31
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py
index a92f34f6035..c8b6fa4f0b8 100644
--- a/zerver/lib/streams.py
+++ b/zerver/lib/streams.py
@@ -158,6 +158,34 @@ def validate_topics_policy(
return None
+def validate_can_create_topic_group_setting_for_protected_history_streams(
+ history_public_to_subscribers: bool | None,
+ invite_only: bool,
+ can_create_topic_group: int | UserGroupMembersData,
+ system_groups_name_dict: dict[str, NamedUserGroup],
+) -> None:
+ if history_public_to_subscribers is None:
+ history_public_to_subscribers = get_default_value_for_history_public_to_subscribers(
+ invite_only, history_public_to_subscribers
+ )
+
+ if history_public_to_subscribers:
+ return
+
+ # If a group setting has value "Everyone including guests" along with additional
+ # users or groups, we do not treat it as equivalent to just "Everyone including guests".
+ # For a channel with protected history, everyone must be allowed to create new topics.
+ # As a result, enabling protected history for a channel requires the `can_create_topic_group`
+ # setting to have "Everyone including guests" group configuration only.
+ if (
+ not isinstance(can_create_topic_group, int)
+ or can_create_topic_group != system_groups_name_dict[SystemGroups.EVERYONE].id
+ ):
+ raise IncompatibleParametersError(
+ ["history_public_to_subscribers", "can_create_topic_group"]
+ )
+
+
def get_default_value_for_history_public_to_subscribers(
invite_only: bool,
history_public_to_subscribers: bool | None,
@@ -1511,6 +1539,15 @@ def access_requested_group_permissions_for_streams(
setting_value = parse_group_setting_value(
setting_request_value, system_groups_name_dict[SystemGroups.NOBODY]
)
+
+ if setting_name == "can_create_topic_group":
+ validate_can_create_topic_group_setting_for_protected_history_streams(
+ request_settings_dict["history_public_to_subscribers"],
+ request_settings_dict["invite_only"],
+ setting_value,
+ system_groups_name_dict,
+ )
+
group_settings_map[setting_name] = access_user_group_for_setting(
setting_value,
user_profile,
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index a27f8c78326..e4754d3fcaa 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -22527,9 +22527,14 @@ paths:
It's an error for this parameter to be false for a public or
web-public channel and when is_private is false.
+ This can only be `false` if `can_create_topic_group` for the channel
+ is the `role:everyone` [system group][system-groups].
+
**Changes**: Before Zulip 6.0 (feature level 136), `history_public_to_subscribers`
was silently ignored unless the request also contained either `is_private` or
`is_web_public`.
+
+ [system-groups]: /api/group-setting-values#system-groups
type: boolean
example: false
is_default_stream:
@@ -22841,10 +22846,14 @@ paths:
Note that using this permission requires also having permission to send
messages in the channel.
+ For [private channels with protected history](/help/channel-permissions#private-channels),
+ this setting can only be set to `role:everyone` [system group][system-groups].
+
**Changes**: New in Zulip 12.0 (feature level 441). Previously, if you
could send messages in a channel, you could create topics in the channel.
[update-group-setting]: /api/group-setting-values#updating-group-setting-values
+ [system-groups]: /api/group-setting-values#system-groups
example:
{
"new":
@@ -28763,11 +28772,16 @@ components:
Note that using this permission requires also having permission to
send messages in the channel.
+ Note that this must be the `role:everyone` [system group][system-groups]
+ for [private channels with protected history][private-channels].
+
**Changes**: New in Zulip 12.0 (feature level 441). Previously,
if you could send messages in a channel, you could create topics in
the channel.
[setting-values]: /api/group-setting-values
+ [private-channels]: /help/channel-permissions/#private-channels
+ [system-groups]: /api/group-setting-values#system-groups
LinkifierPattern:
description: |
The [Python regular
diff --git a/zerver/tests/test_channel_creation.py b/zerver/tests/test_channel_creation.py
index 162a3e5dffe..e2d2b0bd070 100644
--- a/zerver/tests/test_channel_creation.py
+++ b/zerver/tests/test_channel_creation.py
@@ -20,7 +20,7 @@ from zerver.lib.streams import (
)
from zerver.lib.test_classes import ZulipTestCase, get_topic_messages
from zerver.lib.test_helpers import reset_email_visibility_to_everyone_in_zulip_realm
-from zerver.lib.types import UserGroupMembersDict
+from zerver.lib.types import UserGroupMembersData, UserGroupMembersDict
from zerver.models import (
Message,
NamedUserGroup,
@@ -692,6 +692,99 @@ class TestCreateStreams(ZulipTestCase):
)
self.assertCountEqual(stream.can_administer_channel_group.direct_members.all(), [user])
+ def test_can_create_topic_group_for_protected_history_streams(self) -> None:
+ """
+ For channels with protected history, can_create_topic_group can only
+ be set to "role:everyone" system group.
+ """
+ user = self.example_user("iago")
+ realm = user.realm
+ self.login_user(user)
+
+ everyone_system_group = NamedUserGroup.objects.get(
+ name=SystemGroups.EVERYONE, realm=realm, is_system_group=True
+ )
+ moderators_system_group = NamedUserGroup.objects.get(
+ name=SystemGroups.MODERATORS, realm=realm, is_system_group=True
+ )
+ hamletcharacters_group = NamedUserGroup.objects.get(name="hamletcharacters", realm=realm)
+
+ error_msg = "Unsupported parameter combination: history_public_to_subscribers, can_create_topic_group"
+
+ def check_create_protected_history_stream(
+ can_create_topic_group: int | UserGroupMembersData,
+ expect_fail: bool = False,
+ ) -> None:
+ stream_name = "test_protected_history_stream"
+ subscriptions = [{"name": stream_name}]
+ extra_post_data = {
+ "history_public_to_subscribers": orjson.dumps(False).decode(),
+ "can_create_topic_group": orjson.dumps(can_create_topic_group).decode(),
+ }
+
+ result = self.subscribe_via_post(
+ user,
+ subscriptions,
+ extra_post_data,
+ invite_only=True,
+ subdomain="zulip",
+ allow_fail=expect_fail,
+ )
+
+ if expect_fail:
+ self.assert_json_error(result, error_msg)
+ else:
+ self.assert_json_success(result)
+ stream = get_stream(stream_name, realm)
+ self.assertFalse(stream.history_public_to_subscribers)
+ self.assertEqual(stream.can_create_topic_group_id, everyone_system_group.id)
+ # Delete the created stream so that we can create stream
+ # with same name for further cases.
+ stream.delete()
+
+ # Test creating channel using "/channels/create" endpoint as well.
+ result = self.create_channel_via_post(
+ user,
+ name=stream_name,
+ extra_post_data=extra_post_data,
+ invite_only=True,
+ )
+
+ if expect_fail:
+ self.assert_json_error(result, error_msg)
+ return
+
+ self.assert_json_success(result)
+ stream = get_stream(stream_name, realm)
+ self.assertFalse(stream.history_public_to_subscribers)
+ self.assertEqual(stream.can_create_topic_group_id, everyone_system_group.id)
+ # Delete the created stream so that we can create stream
+ # with same name for further cases.
+ stream.delete()
+
+ # Testing for everyone group.
+ check_create_protected_history_stream(everyone_system_group.id)
+
+ # Testing for a system group.
+ check_create_protected_history_stream(moderators_system_group.id, expect_fail=True)
+
+ # Testing for a user defined group.
+ check_create_protected_history_stream(hamletcharacters_group.id, expect_fail=True)
+
+ # Testing for an anonymous group.
+ check_create_protected_history_stream(
+ UserGroupMembersData(
+ direct_members=[user.id], direct_subgroups=[moderators_system_group.id]
+ ),
+ expect_fail=True,
+ )
+
+ # Testing for an anonymous group without members and
+ # only everyone group as subgroup.
+ check_create_protected_history_stream(
+ UserGroupMembersData(direct_members=[], direct_subgroups=[everyone_system_group.id]),
+ )
+
def do_test_permission_setting_on_stream_creation(self, setting_name: str) -> None:
user = self.example_user("hamlet")
realm = user.realm
diff --git a/zerver/tests/test_channel_permissions.py b/zerver/tests/test_channel_permissions.py
index f9a4a3aaa63..5966230776f 100644
--- a/zerver/tests/test_channel_permissions.py
+++ b/zerver/tests/test_channel_permissions.py
@@ -1267,7 +1267,7 @@ class ChannelAdministerPermissionTest(ZulipTestCase):
do_change_stream_permission(
stream,
invite_only=True,
- history_public_to_subscribers=False,
+ history_public_to_subscribers=True,
is_web_public=False,
acting_user=self.admin,
)
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 78e88580d78..80afc817856 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -1480,7 +1480,7 @@ class NormalActionsTest(BaseAction):
self.assertEqual(event["stream_id"], private_stream.id)
def test_events_on_changing_private_stream_permission_settings(self) -> None:
- self.make_stream("private_stream", invite_only=True)
+ self.make_stream("private_stream", invite_only=True, history_public_to_subscribers=True)
self.subscribe(self.example_user("iago"), "private_stream")
for setting_name in Stream.stream_permission_group_settings:
if setting_name in Stream.stream_permission_group_settings_granting_metadata_access:
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index fca3844b7d2..38464f6049b 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -29,6 +29,7 @@ from zerver.actions.streams import (
bulk_remove_subscriptions,
deactivated_streams_by_old_name,
do_change_stream_group_based_setting,
+ do_change_stream_permission,
do_deactivate_stream,
do_set_stream_property,
do_unarchive_stream,
@@ -70,7 +71,7 @@ from zerver.lib.subscription_info import (
from zerver.lib.test_classes import ZulipTestCase, get_topic_messages
from zerver.lib.test_helpers import HostRequestMock, cache_tries_captured
from zerver.lib.types import UserGroupMembersData
-from zerver.lib.user_groups import UserGroupMembershipDetails
+from zerver.lib.user_groups import UserGroupMembershipDetails, get_group_setting_value_for_api
from zerver.models import (
Attachment,
DefaultStream,
@@ -741,6 +742,180 @@ class StreamAdminTest(ZulipTestCase):
}
self.assertEqual(realm_audit_log.extra_data, expected_extra_data)
+ def test_updating_protected_history_and_can_create_topic_group_for_streams(self) -> None:
+ user_profile = self.example_user("iago")
+ self.login_user(user_profile)
+ realm = user_profile.realm
+ stream = self.make_stream(
+ "stream", realm, invite_only=True, history_public_to_subscribers=True
+ )
+ everyone_system_group = NamedUserGroup.objects.get(
+ name="role:everyone", realm=realm, is_system_group=True
+ )
+ moderators_system_group = NamedUserGroup.objects.get(
+ name="role:moderators", realm=realm, is_system_group=True
+ )
+ hamletcharacters_group = NamedUserGroup.objects.get(name="hamletcharacters", realm=realm)
+ anonymous_group = UserGroupMembersData(
+ direct_members=[user_profile.id],
+ direct_subgroups=[moderators_system_group.id],
+ )
+
+ error_msg = "Unsupported parameter combination: history_public_to_subscribers, can_create_topic_group"
+
+ def check_stream_property_update(
+ property_name: str,
+ setting_value: bool | int | UserGroupMembersData,
+ expect_fail: bool = False,
+ ) -> None:
+ params = {property_name: orjson.dumps(setting_value).decode()}
+ if property_name == "can_create_topic_group":
+ params = {property_name: orjson.dumps({"new": setting_value}).decode()}
+
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ stream.refresh_from_db()
+
+ if expect_fail:
+ self.assert_json_error(result, error_msg)
+ return
+
+ self.assert_json_success(result)
+ if property_name == "can_create_topic_group":
+ expected_value = setting_value
+ current_value = get_group_setting_value_for_api(stream.can_create_topic_group)
+ self.assertEqual(expected_value, current_value)
+ else:
+ self.assertEqual(getattr(stream, property_name), setting_value)
+
+ # Test changing can_create_topic_group setting for a private stream
+ # with protected history.
+ do_change_stream_permission(
+ stream,
+ invite_only=True,
+ history_public_to_subscribers=False,
+ is_web_public=False,
+ acting_user=user_profile,
+ )
+
+ check_stream_property_update(
+ "can_create_topic_group", moderators_system_group.id, expect_fail=True
+ )
+ check_stream_property_update(
+ "can_create_topic_group", hamletcharacters_group.id, expect_fail=True
+ )
+ check_stream_property_update("can_create_topic_group", anonymous_group, expect_fail=True)
+ check_stream_property_update("can_create_topic_group", everyone_system_group.id)
+
+ # Test changing stream to have protected history for different
+ # can_create_topic_group setting values.
+ do_change_stream_permission(
+ stream,
+ invite_only=True,
+ history_public_to_subscribers=True,
+ is_web_public=False,
+ acting_user=user_profile,
+ )
+ # Testing for a system group.
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", moderators_system_group, acting_user=user_profile
+ )
+ check_stream_property_update("history_public_to_subscribers", False, expect_fail=True)
+
+ # Testing for a user defined group.
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", hamletcharacters_group, acting_user=user_profile
+ )
+ check_stream_property_update("history_public_to_subscribers", False, expect_fail=True)
+
+ # Testing for an anonymous group.
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", anonymous_group, acting_user=user_profile
+ )
+ check_stream_property_update("history_public_to_subscribers", False, expect_fail=True)
+
+ # Testing for everyone group.
+ do_change_stream_group_based_setting(
+ stream, "can_create_topic_group", everyone_system_group, acting_user=user_profile
+ )
+ check_stream_property_update("history_public_to_subscribers", False)
+
+ # Reset stream permissions.
+ do_change_stream_permission(
+ stream,
+ invite_only=True,
+ history_public_to_subscribers=True,
+ is_web_public=False,
+ acting_user=user_profile,
+ )
+
+ # Test when both "history_public_to_subscribers" and "can_create_topic_group"
+ # are updated in the same request.
+ # Testing for a system group.
+ params = {
+ "history_public_to_subscribers": orjson.dumps(False).decode(),
+ "can_create_topic_group": orjson.dumps({"new": moderators_system_group.id}).decode(),
+ }
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_error(result, error_msg)
+
+ # Testing for a user defined group.
+ params = {
+ "history_public_to_subscribers": orjson.dumps(False).decode(),
+ "can_create_topic_group": orjson.dumps({"new": hamletcharacters_group.id}).decode(),
+ }
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_error(result, error_msg)
+
+ # Testing for an anonymous group.
+ params = {
+ "history_public_to_subscribers": orjson.dumps(False).decode(),
+ "can_create_topic_group": orjson.dumps({"new": anonymous_group}).decode(),
+ }
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_error(result, error_msg)
+
+ # Testing for everyone system group.
+ params = {
+ "history_public_to_subscribers": orjson.dumps(False).decode(),
+ "can_create_topic_group": orjson.dumps({"new": everyone_system_group.id}).decode(),
+ }
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_success(result)
+ stream.refresh_from_db()
+ self.assertEqual(stream.history_public_to_subscribers, False)
+ self.assertEqual(stream.can_create_topic_group.id, everyone_system_group.id)
+
+ # Stream can be set to an anonymous group if its history
+ # is also made public.
+ params = {
+ "history_public_to_subscribers": orjson.dumps(True).decode(),
+ "can_create_topic_group": orjson.dumps({"new": anonymous_group}).decode(),
+ }
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_success(result)
+ stream.refresh_from_db()
+ self.assertEqual(stream.history_public_to_subscribers, True)
+ self.assertEqual(list(stream.can_create_topic_group.direct_members.all()), [user_profile])
+ self.assertEqual(
+ list(stream.can_create_topic_group.direct_subgroups.all()), [moderators_system_group]
+ )
+
+ # Test with an anonymous group with no members and only
+ # everyone system group as subgroup.
+ anonymous_group = UserGroupMembersData(
+ direct_members=[],
+ direct_subgroups=[everyone_system_group.id],
+ )
+ params = {
+ "history_public_to_subscribers": orjson.dumps(False).decode(),
+ "can_create_topic_group": orjson.dumps({"new": anonymous_group}).decode(),
+ }
+ result = self.client_patch(f"/json/streams/{stream.id}", params)
+ self.assert_json_success(result)
+ stream.refresh_from_db()
+ self.assertEqual(stream.history_public_to_subscribers, False)
+ self.assertEqual(stream.can_create_topic_group.id, everyone_system_group.id)
+
def test_stream_permission_changes_updates_updates_attachments(self) -> None:
self.login("desdemona")
fp = StringIO("zulip!")
diff --git a/zerver/views/streams.py b/zerver/views/streams.py
index 2fb306226f8..fd7cb7d91ae 100644
--- a/zerver/views/streams.py
+++ b/zerver/views/streams.py
@@ -85,6 +85,7 @@ from zerver.lib.streams import (
list_to_streams,
stream_to_dict,
user_has_content_access,
+ validate_can_create_topic_group_setting_for_protected_history_streams,
validate_topics_policy,
)
from zerver.lib.subscription_info import gather_subscriptions
@@ -103,7 +104,7 @@ from zerver.lib.user_groups import (
UserGroupMembershipDetails,
access_user_group_api_value_for_setting,
get_group_setting_value_for_api,
- get_system_user_group_by_name,
+ get_role_based_system_groups_dict,
parse_group_setting_value,
validate_group_setting_value_change,
)
@@ -397,6 +398,15 @@ def update_stream_backend(
if validated_topics_policy is not None:
do_set_stream_property(stream, "topics_policy", validated_topics_policy.value, user_profile)
+ system_groups_name_dict = get_role_based_system_groups_dict(user_profile.realm)
+ if not proposed_history_public_to_subscribers and can_create_topic_group is None:
+ validate_can_create_topic_group_setting_for_protected_history_streams(
+ proposed_history_public_to_subscribers,
+ proposed_is_private,
+ stream.can_create_topic_group_id,
+ system_groups_name_dict,
+ )
+
if (
is_private is not None
or is_web_public is not None
@@ -455,7 +465,7 @@ def update_stream_backend(
folder = get_channel_folder_by_id(folder_id, user_profile.realm)
do_change_stream_folder(stream, folder, acting_user=user_profile)
- nobody_group = get_system_user_group_by_name(SystemGroups.NOBODY, user_profile.realm_id)
+ nobody_group = system_groups_name_dict[SystemGroups.NOBODY]
request_settings_dict = locals()
for setting_name, permission_configuration in Stream.stream_permission_group_settings.items():
assert setting_name in request_settings_dict
@@ -488,6 +498,17 @@ def update_stream_backend(
):
raise JsonableError(_("Channel content access is required."))
+ if (
+ setting_name == "can_create_topic_group"
+ and not proposed_history_public_to_subscribers
+ ):
+ validate_can_create_topic_group_setting_for_protected_history_streams(
+ proposed_history_public_to_subscribers,
+ proposed_is_private,
+ new_setting_value,
+ system_groups_name_dict,
+ )
+
with transaction.atomic(durable=True):
user_group_api_value_for_setting = access_user_group_api_value_for_setting(
new_setting_value,