From bed819095599b9681ccd8009c566e2e8ea586f2d Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Thu, 30 Apr 2020 18:42:41 +0200 Subject: [PATCH 1/6] Search in columns 'name' and 'displayname' in the admin users endpoint Signed-off-by: Manuel Stahl --- changelog.d/7377.misc | 1 + docs/admin_api/user_admin_api.rst | 2 +- synapse/storage/data_stores/main/__init__.py | 28 +++++++++++--------- 3 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 changelog.d/7377.misc diff --git a/changelog.d/7377.misc b/changelog.d/7377.misc new file mode 100644 index 000000000000..67e2da0dcb9e --- /dev/null +++ b/changelog.d/7377.misc @@ -0,0 +1 @@ +Search in columns 'name' and 'displayname' in the admin users endpoint. Contributed by Awesome Technologies Innovationslabor GmbH. diff --git a/docs/admin_api/user_admin_api.rst b/docs/admin_api/user_admin_api.rst index 859d7f99e7c8..d9009835fa95 100644 --- a/docs/admin_api/user_admin_api.rst +++ b/docs/admin_api/user_admin_api.rst @@ -71,7 +71,7 @@ The parameter ``limit`` is optional but is used for pagination, denoting the maximum number of items to return in this call. Defaults to ``100``. The parameter ``user_id`` is optional and filters to only users with user IDs -that contain this value. +or display name that contain this value. The parameter ``guests`` is optional and if ``false`` will **exclude** guest users. Defaults to ``true`` to include guest users. diff --git a/synapse/storage/data_stores/main/__init__.py b/synapse/storage/data_stores/main/__init__.py index ceba10882c7d..d7580927ad8d 100644 --- a/synapse/storage/data_stores/main/__init__.py +++ b/synapse/storage/data_stores/main/__init__.py @@ -511,7 +511,7 @@ def get_users_paginate( Args: start (int): start number to begin the query from limit (int): number of rows to retrieve - name (string): filter for user names + name (string): search for user_id or display name guests (bool): whether to in include guest users deactivated (bool): whether to include deactivated users Returns: @@ -520,11 +520,11 @@ def get_users_paginate( def get_users_paginate_txn(txn): filters = [] - args = [] + args = [self.hs.config.server_name] if name: - filters.append("name LIKE ?") - args.append("%" + name + "%") + filters.append("(name LIKE ? OR displayname LIKE ?)") + args.extend(["%" + name + "%:%", "%" + name + "%"]) if not guests: filters.append("is_guest = 0") @@ -534,22 +534,26 @@ def get_users_paginate_txn(txn): where_clause = "WHERE " + " AND ".join(filters) if len(filters) > 0 else "" - sql = "SELECT COUNT(*) as total_users FROM users %s" % (where_clause) - txn.execute(sql, args) - count = txn.fetchone()[0] - - args = [self.hs.config.server_name] + args + [limit, start] - sql = """ - SELECT name, user_type, is_guest, admin, deactivated, displayname, avatar_url + sql_base = """ FROM users as u LEFT JOIN profiles AS p ON u.name = '@' || p.user_id || ':' || ? {} - ORDER BY u.name LIMIT ? OFFSET ? """.format( where_clause ) + sql = "SELECT COUNT(*) as total_users " + sql_base + txn.execute(sql, args) + count = txn.fetchone()[0] + + sql = ( + "SELECT name, user_type, is_guest, admin, deactivated, displayname, avatar_url " + + sql_base + + " ORDER BY u.name LIMIT ? OFFSET ?" + ) + args += [limit, start] txn.execute(sql, args) users = self.db.cursor_to_dict(txn) + return users, count return self.db.runInteraction("get_users_paginate_txn", get_users_paginate_txn) From 397033932339266b6dfba4eac40a1bbdaada2da1 Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Wed, 10 Jun 2020 12:49:08 +0200 Subject: [PATCH 2/6] Rename filter parameter from 'user_id' to 'name' Signed-off-by: Manuel Stahl --- docs/admin_api/user_admin_api.rst | 4 ++-- synapse/rest/admin/users.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/admin_api/user_admin_api.rst b/docs/admin_api/user_admin_api.rst index d9009835fa95..a36ae73deecf 100644 --- a/docs/admin_api/user_admin_api.rst +++ b/docs/admin_api/user_admin_api.rst @@ -70,8 +70,8 @@ from a previous call. The parameter ``limit`` is optional but is used for pagination, denoting the maximum number of items to return in this call. Defaults to ``100``. -The parameter ``user_id`` is optional and filters to only users with user IDs -or display name that contain this value. +The parameter ``name`` is optional and can be used to list only users with the +local part of the user ID or display name that contain this value. The parameter ``guests`` is optional and if ``false`` will **exclude** guest users. Defaults to ``true`` to include guest users. diff --git a/synapse/rest/admin/users.py b/synapse/rest/admin/users.py index 593ce011e888..37bb42ef8526 100644 --- a/synapse/rest/admin/users.py +++ b/synapse/rest/admin/users.py @@ -74,7 +74,7 @@ class UsersRestServletV2(RestServlet): The parameters `from` and `limit` are required only for pagination. By default, a `limit` of 100 is used. - The parameter `user_id` can be used to filter by user id. + The parameter `name` can be used to filter by user id or display name. The parameter `guests` can be used to exclude guest users. The parameter `deactivated` can be used to include deactivated users. """ @@ -90,12 +90,12 @@ async def on_GET(self, request): start = parse_integer(request, "from", default=0) limit = parse_integer(request, "limit", default=100) - user_id = parse_string(request, "user_id", default=None) + name = parse_string(request, "name", default=None) guests = parse_boolean(request, "guests", default=True) deactivated = parse_boolean(request, "deactivated", default=False) users, total = await self.store.get_users_paginate( - start, limit, user_id, guests, deactivated + start, limit, name, guests, deactivated ) ret = {"users": users, "total": total} if len(users) >= limit: From 06004a553b70a3d1f014e587a14173cdbd32e90c Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Mon, 15 Jun 2020 09:47:06 +0200 Subject: [PATCH 3/6] Support legacy parameter 'user_id' --- synapse/rest/admin/users.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/synapse/rest/admin/users.py b/synapse/rest/admin/users.py index 37bb42ef8526..253272abcc34 100644 --- a/synapse/rest/admin/users.py +++ b/synapse/rest/admin/users.py @@ -90,7 +90,8 @@ async def on_GET(self, request): start = parse_integer(request, "from", default=0) limit = parse_integer(request, "limit", default=100) - name = parse_string(request, "name", default=None) + user_id = parse_string(request, "user_id", default=None) + name = parse_string(request, "name", default=user_id) guests = parse_boolean(request, "guests", default=True) deactivated = parse_boolean(request, "deactivated", default=False) From ae5477cf80a93f43512a49099c2497e821c1afd5 Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Mon, 24 Aug 2020 19:22:52 +0200 Subject: [PATCH 4/6] Keep functionality of legacy parameter 'user_id' --- docs/admin_api/user_admin_api.rst | 2 ++ synapse/rest/admin/users.py | 5 +++-- synapse/storage/databases/main/__init__.py | 8 ++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/admin_api/user_admin_api.rst b/docs/admin_api/user_admin_api.rst index 8bb4b8afc8e3..99948ec06199 100644 --- a/docs/admin_api/user_admin_api.rst +++ b/docs/admin_api/user_admin_api.rst @@ -119,6 +119,8 @@ from a previous call. The parameter ``limit`` is optional but is used for pagination, denoting the maximum number of items to return in this call. Defaults to ``100``. +The parameter ``user_id`` is optional and can be used to filter by user id. + The parameter ``name`` is optional and can be used to list only users with the local part of the user ID or display name that contain this value. diff --git a/synapse/rest/admin/users.py b/synapse/rest/admin/users.py index 65c611063fd4..f3e77da850c8 100644 --- a/synapse/rest/admin/users.py +++ b/synapse/rest/admin/users.py @@ -72,6 +72,7 @@ class UsersRestServletV2(RestServlet): The parameters `from` and `limit` are required only for pagination. By default, a `limit` of 100 is used. + The parameter `user_id` can be used to filter by user id. The parameter `name` can be used to filter by user id or display name. The parameter `guests` can be used to exclude guest users. The parameter `deactivated` can be used to include deactivated users. @@ -89,12 +90,12 @@ async def on_GET(self, request): start = parse_integer(request, "from", default=0) limit = parse_integer(request, "limit", default=100) user_id = parse_string(request, "user_id", default=None) - name = parse_string(request, "name", default=user_id) + name = parse_string(request, "name", default=None) guests = parse_boolean(request, "guests", default=True) deactivated = parse_boolean(request, "deactivated", default=False) users, total = await self.store.get_users_paginate( - start, limit, name, guests, deactivated + start, limit, user_id, name, guests, deactivated ) ret = {"users": users, "total": total} if len(users) >= limit: diff --git a/synapse/storage/databases/main/__init__.py b/synapse/storage/databases/main/__init__.py index a6eecfaa4780..005e453995c0 100644 --- a/synapse/storage/databases/main/__init__.py +++ b/synapse/storage/databases/main/__init__.py @@ -498,7 +498,7 @@ def get_users(self): ) def get_users_paginate( - self, start, limit, name=None, guests=True, deactivated=False + self, start, limit, user_id=None, name=None, guests=True, deactivated=False ): """Function to retrieve a paginated list of users from users list. This will return a json list of users and the @@ -507,7 +507,8 @@ def get_users_paginate( Args: start (int): start number to begin the query from limit (int): number of rows to retrieve - name (string): search for user_id or display name + user_id (string): search for user_id + name (string): search for local part of user_id or display name guests (bool): whether to in include guest users deactivated (bool): whether to include deactivated users Returns: @@ -521,6 +522,9 @@ def get_users_paginate_txn(txn): if name: filters.append("(name LIKE ? OR displayname LIKE ?)") args.extend(["%" + name + "%:%", "%" + name + "%"]) + elif user_id: + filters.append("name LIKE ?") + args.extend(["%" + user_id + "%"]) if not guests: filters.append("is_guest = 0") From 42bcd0b8e6d5cddf64810d9e042a516059cef610 Mon Sep 17 00:00:00 2001 From: Manuel Stahl <37705355+awesome-manuel@users.noreply.github.com> Date: Tue, 25 Aug 2020 14:06:31 +0200 Subject: [PATCH 5/6] Use suggested SQL pattern Co-authored-by: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> --- synapse/storage/databases/main/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/databases/main/__init__.py b/synapse/storage/databases/main/__init__.py index 005e453995c0..e0c45a8f05a3 100644 --- a/synapse/storage/databases/main/__init__.py +++ b/synapse/storage/databases/main/__init__.py @@ -521,7 +521,7 @@ def get_users_paginate_txn(txn): if name: filters.append("(name LIKE ? OR displayname LIKE ?)") - args.extend(["%" + name + "%:%", "%" + name + "%"]) + args.extend(["%@" + name + "%:%", "%" + name + "%"]) elif user_id: filters.append("name LIKE ?") args.extend(["%" + user_id + "%"]) From 7ea27cd1fbcd2b8d8136f9771bffc7bd6faa43b7 Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Tue, 25 Aug 2020 14:25:44 +0200 Subject: [PATCH 6/6] Fix SQL pattern --- synapse/storage/databases/main/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/databases/main/__init__.py b/synapse/storage/databases/main/__init__.py index e0c45a8f05a3..0ed726fee027 100644 --- a/synapse/storage/databases/main/__init__.py +++ b/synapse/storage/databases/main/__init__.py @@ -521,7 +521,7 @@ def get_users_paginate_txn(txn): if name: filters.append("(name LIKE ? OR displayname LIKE ?)") - args.extend(["%@" + name + "%:%", "%" + name + "%"]) + args.extend(["@%" + name + "%:%", "%" + name + "%"]) elif user_id: filters.append("name LIKE ?") args.extend(["%" + user_id + "%"])