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

Commit bed8190

Browse files
Search in columns 'name' and 'displayname' in the admin users endpoint
Signed-off-by: Manuel Stahl <[email protected]>
1 parent 032e5a2 commit bed8190

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

changelog.d/7377.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Search in columns 'name' and 'displayname' in the admin users endpoint. Contributed by Awesome Technologies Innovationslabor GmbH.

docs/admin_api/user_admin_api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ The parameter ``limit`` is optional but is used for pagination, denoting the
7171
maximum number of items to return in this call. Defaults to ``100``.
7272

7373
The parameter ``user_id`` is optional and filters to only users with user IDs
74-
that contain this value.
74+
or display name that contain this value.
7575

7676
The parameter ``guests`` is optional and if ``false`` will **exclude** guest users.
7777
Defaults to ``true`` to include guest users.

synapse/storage/data_stores/main/__init__.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def get_users_paginate(
511511
Args:
512512
start (int): start number to begin the query from
513513
limit (int): number of rows to retrieve
514-
name (string): filter for user names
514+
name (string): search for user_id or display name
515515
guests (bool): whether to in include guest users
516516
deactivated (bool): whether to include deactivated users
517517
Returns:
@@ -520,11 +520,11 @@ def get_users_paginate(
520520

521521
def get_users_paginate_txn(txn):
522522
filters = []
523-
args = []
523+
args = [self.hs.config.server_name]
524524

525525
if name:
526-
filters.append("name LIKE ?")
527-
args.append("%" + name + "%")
526+
filters.append("(name LIKE ? OR displayname LIKE ?)")
527+
args.extend(["%" + name + "%:%", "%" + name + "%"])
528528

529529
if not guests:
530530
filters.append("is_guest = 0")
@@ -534,22 +534,26 @@ def get_users_paginate_txn(txn):
534534

535535
where_clause = "WHERE " + " AND ".join(filters) if len(filters) > 0 else ""
536536

537-
sql = "SELECT COUNT(*) as total_users FROM users %s" % (where_clause)
538-
txn.execute(sql, args)
539-
count = txn.fetchone()[0]
540-
541-
args = [self.hs.config.server_name] + args + [limit, start]
542-
sql = """
543-
SELECT name, user_type, is_guest, admin, deactivated, displayname, avatar_url
537+
sql_base = """
544538
FROM users as u
545539
LEFT JOIN profiles AS p ON u.name = '@' || p.user_id || ':' || ?
546540
{}
547-
ORDER BY u.name LIMIT ? OFFSET ?
548541
""".format(
549542
where_clause
550543
)
544+
sql = "SELECT COUNT(*) as total_users " + sql_base
545+
txn.execute(sql, args)
546+
count = txn.fetchone()[0]
547+
548+
sql = (
549+
"SELECT name, user_type, is_guest, admin, deactivated, displayname, avatar_url "
550+
+ sql_base
551+
+ " ORDER BY u.name LIMIT ? OFFSET ?"
552+
)
553+
args += [limit, start]
551554
txn.execute(sql, args)
552555
users = self.db.cursor_to_dict(txn)
556+
553557
return users, count
554558

555559
return self.db.runInteraction("get_users_paginate_txn", get_users_paginate_txn)

0 commit comments

Comments
 (0)