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

Search in columns 'name' and 'displayname' in the admin users endpoint #7377

Merged
merged 8 commits into from
Aug 25, 2020
1 change: 1 addition & 0 deletions changelog.d/7377.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Search in columns 'name' and 'displayname' in the admin users endpoint. Contributed by Awesome Technologies Innovationslabor GmbH.
4 changes: 2 additions & 2 deletions docs/admin_api/user_admin_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand Down
5 changes: 3 additions & 2 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
awesome-manuel marked this conversation as resolved.
Show resolved Hide resolved
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.
"""
Expand All @@ -91,11 +91,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still doesn't address the concern I mentioned. Now user_id will return results from both displayname and name columns. This breaks backwards compatibility.

What I'm asking for is:

  • If user_id is provided, results are filtered according to the name column only.
  • If name is provided, results are filtered according to the name and displayname columns.
  • If both user_id and name are provided, results are filtered according to the name column only.

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:
Expand Down
28 changes: 16 additions & 12 deletions synapse/storage/data_stores/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand All @@ -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)
Expand Down