From d331119758d0b5eea0c47c7292c84a01c1d060a8 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 10 Jun 2019 11:30:10 +0100 Subject: Hide new users from the user directory if enabled in the server config. --- synapse/config/server.py | 11 +++++++++++ synapse/handlers/register.py | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/synapse/config/server.py b/synapse/config/server.py index 1b8968608e..2913f64dd2 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -178,6 +178,12 @@ class ServerConfig(Config): # events with profile information that differ from the target's global profile. self.allow_per_room_profiles = config.get("allow_per_room_profiles", True) + # Whether to show the users on this homeserver in the user directory. Defaults to + # True. + self.show_users_in_user_directory = config.get( + "show_users_in_user_directory", True, + ) + self.listeners = [] for listener in config.get("listeners", []): if not isinstance(listener.get("port", None), int): @@ -577,6 +583,11 @@ class ServerConfig(Config): # Defaults to 'true'. # #allow_per_room_profiles: false + + # Whether to show the users on this homeserver in the user directory. Defaults to + # 'true'. + # + #show_users_in_user_directory: false """ % locals() def read_arguments(self, args): diff --git a/synapse/handlers/register.py b/synapse/handlers/register.py index 68d87163af..bd8f522fad 100644 --- a/synapse/handlers/register.py +++ b/synapse/handlers/register.py @@ -74,6 +74,8 @@ class RegistrationHandler(BaseHandler): ) self._server_notices_mxid = hs.config.server_notices_mxid + self._show_in_user_directory = self.hs.show_users_in_user_directory + if hs.config.worker_app: self._register_client = ReplicationRegisterServlet.make_client(hs) self._register_device_client = ( @@ -289,6 +291,14 @@ class RegistrationHandler(BaseHandler): user_id, threepid_dict, None, False, ) + # Prevent the new user from showing up in the user directory if the server + # mandates it. + if self._show_in_user_directory: + yield self.store.add_account_data_for_user( + user_id, "im.vector.hide_profile", {'hide_profile': True}, + ) + yield self.profile_handler.set_active(user, False, True) + defer.returnValue((user_id, token)) @defer.inlineCallbacks -- cgit 1.5.1 From 0e63dd89a49f5448b08f89100c053cb95fab620c Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 10 Jun 2019 12:10:29 +0100 Subject: Fix condition --- synapse/handlers/register.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/register.py b/synapse/handlers/register.py index bd8f522fad..c73f636122 100644 --- a/synapse/handlers/register.py +++ b/synapse/handlers/register.py @@ -293,7 +293,7 @@ class RegistrationHandler(BaseHandler): # Prevent the new user from showing up in the user directory if the server # mandates it. - if self._show_in_user_directory: + if not self._show_in_user_directory: yield self.store.add_account_data_for_user( user_id, "im.vector.hide_profile", {'hide_profile': True}, ) -- cgit 1.5.1 From deee82aebc95b471988002fcea17768c3e57b53f Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 10 Jun 2019 17:16:03 +0100 Subject: Fix variable definition --- synapse/handlers/register.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/register.py b/synapse/handlers/register.py index c73f636122..744ead6c01 100644 --- a/synapse/handlers/register.py +++ b/synapse/handlers/register.py @@ -74,7 +74,7 @@ class RegistrationHandler(BaseHandler): ) self._server_notices_mxid = hs.config.server_notices_mxid - self._show_in_user_directory = self.hs.show_users_in_user_directory + self._show_in_user_directory = self.hs.config.show_users_in_user_directory if hs.config.worker_app: self._register_client = ReplicationRegisterServlet.make_client(hs) -- cgit 1.5.1 From 1924848dfad30b9f6853039b65d307c4af2e2876 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 10 Jun 2019 17:16:17 +0100 Subject: Add test case --- tests/rest/client/v2_alpha/test_register.py | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/rest/client/v2_alpha/test_register.py b/tests/rest/client/v2_alpha/test_register.py index 1628db501c..4cb27972da 100644 --- a/tests/rest/client/v2_alpha/test_register.py +++ b/tests/rest/client/v2_alpha/test_register.py @@ -21,6 +21,10 @@ import os import pkg_resources +from mock import Mock + +from twisted.internet import defer + import synapse.rest.admin from synapse.api.constants import LoginType from synapse.api.errors import Codes @@ -205,6 +209,53 @@ class RegisterRestServletTestCase(unittest.HomeserverTestCase): self.assertEquals(channel.result["code"], b"200", channel.result) +class RegisterHideProfileTestCase(unittest.HomeserverTestCase): + + servlets = [ + synapse.rest.admin.register_servlets_for_client_rest_resource, + ] + + def make_homeserver(self, reactor, clock): + + self.url = b"/_matrix/client/r0/register" + + config = self.default_config() + config["enable_registration"] = True + config["show_users_in_user_directory"] = False + config["replicate_user_profiles_to"] = ["fakeserver"] + + mock_http_client = Mock(spec=[ + "get_json", + "post_json_get_json", + ]) + mock_http_client.post_json_get_json.return_value = defer.succeed((200, "{}")) + + self.hs = self.setup_test_homeserver( + config=config, + simple_http_client=mock_http_client, + ) + + return self.hs + + def test_profile_hidden(self): + user_id = self.register_user("kermit", "monkey") + + post_json = self.hs.get_simple_http_client().post_json_get_json + + # We expect post_json_get_json to have been called twice: once with the original + # profile and once with the None profile resulting from the request to hide it + # from the user directory. + self.assertEqual(post_json.call_count, 2, post_json.call_args_list) + + # Get the args (and not kwargs) passed to post_json. + args = post_json.call_args[0] + # Make sure the last call was attempting to replicate profiles. + split_uri = args[0].split("/") + self.assertEqual(split_uri[len(split_uri)-1], "replicate_profiles", args[0]) + # Make sure the last profile update was overriding the user's profile to None. + self.assertEqual(args[1]["batch"][user_id], None, args[1]) + + class AccountValidityTestCase(unittest.HomeserverTestCase): servlets = [ -- cgit 1.5.1 From 9ece96f5c8581af3253b3d36233f87361fc7dbc9 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 10 Jun 2019 17:17:18 +0100 Subject: Changelog --- changelog.d/5420.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/5420.feature diff --git a/changelog.d/5420.feature b/changelog.d/5420.feature new file mode 100644 index 0000000000..745864b903 --- /dev/null +++ b/changelog.d/5420.feature @@ -0,0 +1 @@ +Add configuration option to hide new users from the user directory. -- cgit 1.5.1 From 10510f1e7ec067c4bc14a4c746e979e6f82777df Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 10 Jun 2019 17:23:11 +0100 Subject: Lint --- tests/rest/client/v2_alpha/test_register.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rest/client/v2_alpha/test_register.py b/tests/rest/client/v2_alpha/test_register.py index 4cb27972da..f17f34daa3 100644 --- a/tests/rest/client/v2_alpha/test_register.py +++ b/tests/rest/client/v2_alpha/test_register.py @@ -251,7 +251,7 @@ class RegisterHideProfileTestCase(unittest.HomeserverTestCase): args = post_json.call_args[0] # Make sure the last call was attempting to replicate profiles. split_uri = args[0].split("/") - self.assertEqual(split_uri[len(split_uri)-1], "replicate_profiles", args[0]) + self.assertEqual(split_uri[len(split_uri) - 1], "replicate_profiles", args[0]) # Make sure the last profile update was overriding the user's profile to None. self.assertEqual(args[1]["batch"][user_id], None, args[1]) -- cgit 1.5.1 From c10226581a85a4b47b31bffacfd99c47dc653066 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 10 Jun 2019 17:25:29 +0100 Subject: Lint --- tests/rest/client/v2_alpha/test_register.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/rest/client/v2_alpha/test_register.py b/tests/rest/client/v2_alpha/test_register.py index f17f34daa3..3b05e36080 100644 --- a/tests/rest/client/v2_alpha/test_register.py +++ b/tests/rest/client/v2_alpha/test_register.py @@ -19,10 +19,10 @@ import datetime import json import os -import pkg_resources - from mock import Mock +import pkg_resources + from twisted.internet import defer import synapse.rest.admin -- cgit 1.5.1 From 389aa20170fd3f5ebddffef38e72ccf2e4e917ec Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 10 Jun 2019 17:31:45 +0100 Subject: Generate sample config --- docs/sample_config.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/sample_config.yaml b/docs/sample_config.yaml index ca8f6c406d..7fa561aa03 100644 --- a/docs/sample_config.yaml +++ b/docs/sample_config.yaml @@ -282,6 +282,11 @@ listeners: # #allow_per_room_profiles: false +# Whether to show the users on this homeserver in the user directory. Defaults to +# 'true'. +# +#show_users_in_user_directory: false + ## TLS ## -- cgit 1.5.1 From 1c7628eb3aee3c182b3ef4b39811915becd9e7e8 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Wed, 12 Jun 2019 16:36:28 +0100 Subject: Lint --- synapse/rest/client/v1/profile.py | 1 - 1 file changed, 1 deletion(-) diff --git a/synapse/rest/client/v1/profile.py b/synapse/rest/client/v1/profile.py index 202a9f21ad..064bcddaeb 100644 --- a/synapse/rest/client/v1/profile.py +++ b/synapse/rest/client/v1/profile.py @@ -22,7 +22,6 @@ from synapse.http.servlet import RestServlet, parse_json_object_from_request from synapse.rest.client.v2_alpha._base import client_patterns from synapse.types import UserID - logger = logging.getLogger(__name__) -- cgit 1.5.1 From 7c2d936802e59ef16d8ca2241b9c74abe2c4e21b Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Wed, 12 Jun 2019 16:38:00 +0100 Subject: Fix bogus changelog --- changelog.d/5083.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/5083.feature b/changelog.d/5083.feature index f6014c6b43..2ffdd37eef 100644 --- a/changelog.d/5083.feature +++ b/changelog.d/5083.feature @@ -1 +1 @@ -Adds auth_profile_reqs option to require access_token to GET /profile endpoints on CS API +Adds auth_profile_reqs option to require access_token to GET /profile endpoints on CS API. -- cgit 1.5.1