Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4895 from matrix-org/erikj/disable_user_search
Browse files Browse the repository at this point in the history
Add option to disable searching in the user dir
  • Loading branch information
erikjohnston authored Mar 20, 2019
2 parents 4d53017 + 3660d24 commit 263f2c9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/4895.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add option to disable searching the user directory.
5 changes: 5 additions & 0 deletions docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -962,13 +962,18 @@ password_config:

# User Directory configuration
#
# 'enabled' defines whether users can search the user directory. If
# false then empty responses are returned to all queries. Defaults to
# true.
#
# 'search_all_users' defines whether to search all users visible to your HS
# when searching the user directory, rather than limiting to users visible
# in public rooms. Defaults to false. If you set it True, you'll have to run
# UPDATE user_directory_stream_pos SET stream_id = NULL;
# on your database to tell it to rebuild the user_directory search indexes.
#
#user_directory:
# enabled: true
# search_all_users: false


Expand Down
9 changes: 9 additions & 0 deletions synapse/config/user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ class UserDirectoryConfig(Config):
"""

def read_config(self, config):
self.user_directory_search_enabled = True
self.user_directory_search_all_users = False
user_directory_config = config.get("user_directory", None)
if user_directory_config:
self.user_directory_search_enabled = (
user_directory_config.get("enabled", True)
)
self.user_directory_search_all_users = (
user_directory_config.get("search_all_users", False)
)
Expand All @@ -33,12 +37,17 @@ def default_config(self, config_dir_path, server_name, **kwargs):
return """
# User Directory configuration
#
# 'enabled' defines whether users can search the user directory. If
# false then empty responses are returned to all queries. Defaults to
# true.
#
# 'search_all_users' defines whether to search all users visible to your HS
# when searching the user directory, rather than limiting to users visible
# in public rooms. Defaults to false. If you set it True, you'll have to run
# UPDATE user_directory_stream_pos SET stream_id = NULL;
# on your database to tell it to rebuild the user_directory search indexes.
#
#user_directory:
# enabled: true
# search_all_users: false
"""
6 changes: 6 additions & 0 deletions synapse/rest/client/v2_alpha/user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def on_POST(self, request):
requester = yield self.auth.get_user_by_req(request, allow_guest=False)
user_id = requester.user.to_string()

if not self.hs.config.user_directory_search_enabled:
defer.returnValue((200, {
"limited": False,
"results": [],
}))

body = parse_json_object_from_request(request)

limit = body.get("limit", 10)
Expand Down
52 changes: 52 additions & 0 deletions tests/handlers/test_user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from synapse.api.constants import UserTypes
from synapse.rest.client.v1 import admin, login, room
from synapse.rest.client.v2_alpha import user_directory
from synapse.storage.roommember import ProfileInfo

from tests import unittest
Expand Down Expand Up @@ -317,3 +318,54 @@ def test_initial_share_all_users(self):
u4 = self.register_user("user4", "pass")
s = self.get_success(self.handler.search_users(u1, u4, 10))
self.assertEqual(len(s["results"]), 1)


class TestUserDirSearchDisabled(unittest.HomeserverTestCase):
user_id = "@test:test"

servlets = [
user_directory.register_servlets,
room.register_servlets,
login.register_servlets,
admin.register_servlets,
]

def make_homeserver(self, reactor, clock):
config = self.default_config()
config.update_user_directory = True
hs = self.setup_test_homeserver(config=config)

self.config = hs.config

return hs

def test_disabling_room_list(self):
self.config.user_directory_search_enabled = True

# First we create a room with another user so that user dir is non-empty
# for our user
self.helper.create_room_as(self.user_id)
u2 = self.register_user("user2", "pass")
room = self.helper.create_room_as(self.user_id)
self.helper.join(room, user=u2)

# Assert user directory is not empty
request, channel = self.make_request(
"POST",
b"user_directory/search",
b'{"search_term":"user2"}',
)
self.render(request)
self.assertEquals(200, channel.code, channel.result)
self.assertTrue(len(channel.json_body["results"]) > 0)

# Disable user directory and check search returns nothing
self.config.user_directory_search_enabled = False
request, channel = self.make_request(
"POST",
b"user_directory/search",
b'{"search_term":"user2"}',
)
self.render(request)
self.assertEquals(200, channel.code, channel.result)
self.assertTrue(len(channel.json_body["results"]) == 0)

0 comments on commit 263f2c9

Please sign in to comment.