Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
saml: Support "sync-all-groups" config mode.
Mateusz Mandera67e47ec13c4f
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.pyindex 2b9589459cc..af446a88a79 100644--- a/zerver/tests/test_auth_backends.py+++ b/zerver/tests/test_auth_backends.py@@ -3588,6 +3588,72 @@ class SAMLAuthBackendTest(SocialAuthBase):))+ def test_social_auth_group_sync_sync_all_groups_mode(self) -> None:+ realm = get_realm("zulip")+ hamlet = self.example_user("hamlet")+ testgroup1 = create_user_group_in_database("testgroup1", [], realm, acting_user=hamlet)+ testgroup2 = create_user_group_in_database("testgroup2", [], realm, acting_user=hamlet)++ hamletcharacters_group = NamedUserGroup.objects.get(+ name="hamletcharacters", realm_for_sharding=realm+ )++ sync_custom_attrs_dict = {+ "zulip": {+ "saml": {+ "role": "zulip_role",+ # This config syntax enables the "sync all groups" mode.+ "groups": "*",+ }+ }+ }++ with (+ self.settings(+ SOCIAL_AUTH_SYNC_ATTRS_DICT=sync_custom_attrs_dict,+ ),+ self.assertLogs(self.logger_string) as mock_log,+ ):+ account_data_dict = self.get_account_data_dict(email=self.email, name=self.name)+ result = self.social_auth_test(+ account_data_dict,+ subdomain="zulip",+ extra_attributes=dict(zulip_groups=["testgroup1", "testgroup2"]),+ )+ data = load_subdomain_token(result)+ self.assertEqual(data["email"], self.email)+ self.assertEqual(result.status_code, 302)++ self.assertTrue(+ is_user_in_group(+ testgroup1.id,+ hamlet,+ direct_member_only=True,+ )+ )+ self.assertTrue(+ is_user_in_group(+ testgroup2.id,+ hamlet,+ direct_member_only=True,+ )+ )+ self.assertFalse(+ is_user_in_group(+ hamletcharacters_group.id,+ hamlet,+ direct_member_only=True,+ )+ )+ # Verify the expected log line revealing the internal details of the incoming groups -> Zulip groups translation.+ self.assertIn(+ self.logger_output(+ f"social_auth_sync_user_attributes:<user:{hamlet.id}>: received group names: ['testgroup1', 'testgroup2']|intended Zulip groups: ['testgroup1', 'testgroup2']. group mapping used: {{'hamletcharacters': 'hamletcharacters', 'testgroup1': 'testgroup1', 'testgroup2': 'testgroup2'}}",+ type="info",+ ),+ mock_log.output,+ )+@override_settings(TERMS_OF_SERVICE_VERSION=None)def test_social_auth_create_user_with_synced_role_and_groups(self) -> None:email = "[email protected]"diff --git a/zproject/backends.py b/zproject/backends.pyindex 758c31e5fda..4e3bdd56392 100644--- a/zproject/backends.py+++ b/zproject/backends.py@@ -1915,30 +1915,44 @@ class SocialAuthGroupInfo:def process_social_auth_group_sync_info(backend: Any,+ realm: Realm,user_profile: UserProfile | None,attrs_config: dict[str, Any],extra_attrs: dict[str, Any],) -> SocialAuthGroupInfo | None:external_group_name_to_zulip_group_name: dict[str, str] = {}- groups_config_list = cast(list[str | tuple[str, str]], attrs_config.get("groups", []))- if not groups_config_list:- return None+ groups_config = cast(str | list[str | tuple[str, str]], attrs_config.get("groups", []))+ match groups_config:+ case []:+ return None+ case list():+ for group_name in groups_config:+ match group_name:+ # the objects in the config are either straight-forward group names+ # or tuples (<saml group name>, <zulip group name>) indicating the+ # obvious mapping.+ case str():+ external_group_name_to_zulip_group_name[group_name] = group_name+ case (saml_group_name, zulip_group_name):+ external_group_name_to_zulip_group_name[saml_group_name] = zulip_group_name+ case _: # nocoverage+ raise AssertionError(+ f"Unsupported item in group sync config list: {group_name}"+ )+ case "*":+ # When using this configuration, a user's full list of intended group memberships+ # should be passed in zulip_groups.+ all_realm_groups = NamedUserGroup.objects.filter(+ realm=realm, deactivated=False, is_system_group=False+ )+ for group in all_realm_groups:+ external_group_name_to_zulip_group_name[group.name] = group.name+ case str(): # nocoverage+ raise AssertionError(f'unsupported groups config string: {groups_config}; expected "*"')# Group sync is only supported for SAML for the foreseeable time.assert backend.name == "saml"- for group_name in groups_config_list:- match group_name:- # the objects in the config are either straight-forward group names- # or tuples (<saml group name>, <zulip group name>) indicating the- # obvious mapping.- case str():- external_group_name_to_zulip_group_name[group_name] = group_name- case (saml_group_name, zulip_group_name):- external_group_name_to_zulip_group_name[saml_group_name] = zulip_group_name- case _: # nocoverage- raise AssertionError(f"Unsupported item in group sync config list: {group_name}")-syncable_group_names = set(external_group_name_to_zulip_group_name.values())# pop zulip_groups from extra_attrs, so that only user attribute sync@@ -2015,7 +2029,7 @@ def social_auth_sync_user_attributes()group_sync_config = process_social_auth_group_sync_info(- backend, user_profile, attrs_config, extra_attrs+ backend, realm, user_profile, attrs_config, extra_attrs)should_sync_user_attrs = extra_attrs and profile_field_name_to_attr_nameif not (should_sync_user_attrs or group_sync_config is not None):