Merge pull request #4895 from matrix-org/erikj/disable_user_search

Add option to disable searching in the user dir
This commit is contained in:
Erik Johnston 2019-03-20 16:47:15 +00:00 committed by GitHub
commit 263f2c9ce1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 0 deletions

1
changelog.d/4895.feature Normal file
View File

@ -0,0 +1 @@
Add option to disable searching the user directory.

View File

@ -962,6 +962,10 @@ password_config:
# User Directory configuration # 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 # 'search_all_users' defines whether to search all users visible to your HS
# when searching the user directory, rather than limiting to users visible # 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 # in public rooms. Defaults to false. If you set it True, you'll have to run
@ -969,6 +973,7 @@ password_config:
# on your database to tell it to rebuild the user_directory search indexes. # on your database to tell it to rebuild the user_directory search indexes.
# #
#user_directory: #user_directory:
# enabled: true
# search_all_users: false # search_all_users: false

View File

@ -22,9 +22,13 @@ class UserDirectoryConfig(Config):
""" """
def read_config(self, config): def read_config(self, config):
self.user_directory_search_enabled = True
self.user_directory_search_all_users = False self.user_directory_search_all_users = False
user_directory_config = config.get("user_directory", None) user_directory_config = config.get("user_directory", None)
if user_directory_config: if user_directory_config:
self.user_directory_search_enabled = (
user_directory_config.get("enabled", True)
)
self.user_directory_search_all_users = ( self.user_directory_search_all_users = (
user_directory_config.get("search_all_users", False) user_directory_config.get("search_all_users", False)
) )
@ -33,6 +37,10 @@ class UserDirectoryConfig(Config):
return """ return """
# User Directory configuration # 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 # 'search_all_users' defines whether to search all users visible to your HS
# when searching the user directory, rather than limiting to users visible # 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 # in public rooms. Defaults to false. If you set it True, you'll have to run
@ -40,5 +48,6 @@ class UserDirectoryConfig(Config):
# on your database to tell it to rebuild the user_directory search indexes. # on your database to tell it to rebuild the user_directory search indexes.
# #
#user_directory: #user_directory:
# enabled: true
# search_all_users: false # search_all_users: false
""" """

View File

@ -59,6 +59,12 @@ class UserDirectorySearchRestServlet(RestServlet):
requester = yield self.auth.get_user_by_req(request, allow_guest=False) requester = yield self.auth.get_user_by_req(request, allow_guest=False)
user_id = requester.user.to_string() 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) body = parse_json_object_from_request(request)
limit = body.get("limit", 10) limit = body.get("limit", 10)

View File

@ -16,6 +16,7 @@ from mock import Mock
from synapse.api.constants import UserTypes from synapse.api.constants import UserTypes
from synapse.rest.client.v1 import admin, login, room 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 synapse.storage.roommember import ProfileInfo
from tests import unittest from tests import unittest
@ -317,3 +318,54 @@ class UserDirectoryTestCase(unittest.HomeserverTestCase):
u4 = self.register_user("user4", "pass") u4 = self.register_user("user4", "pass")
s = self.get_success(self.handler.search_users(u1, u4, 10)) s = self.get_success(self.handler.search_users(u1, u4, 10))
self.assertEqual(len(s["results"]), 1) 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)