From bed819095599b9681ccd8009c566e2e8ea586f2d Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Thu, 30 Apr 2020 18:42:41 +0200 Subject: [PATCH] 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)