Return total number of users and profile attributes in admin users endpoint (#6881)

Signed-off-by: Manuel Stahl <manuel.stahl@awesome-technologies.de>
This commit is contained in:
Manuel Stahl 2020-04-28 19:19:36 +02:00 committed by GitHub
parent fce663889b
commit 04dd7d182d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 34 deletions

1
changelog.d/6881.misc Normal file
View file

@ -0,0 +1 @@
Return total number of users and profile attributes in admin users endpoint. Contributed by Awesome Technologies Innovationslabor GmbH.

View file

@ -72,17 +72,22 @@ It returns a JSON body like the following:
"is_guest": 0, "is_guest": 0,
"admin": 0, "admin": 0,
"user_type": null, "user_type": null,
"deactivated": 0 "deactivated": 0,
"displayname": <User One>,
"avatar_url": null
}, { }, {
"name": "<user_id2>", "name": "<user_id2>",
"password_hash": "<password_hash2>", "password_hash": "<password_hash2>",
"is_guest": 0, "is_guest": 0,
"admin": 1, "admin": 1,
"user_type": null, "user_type": null,
"deactivated": 0 "deactivated": 0,
"displayname": <User Two>,
"avatar_url": "<avatar_url>"
} }
], ],
"next_token": "100" "next_token": "100",
"total": 200
} }

View file

@ -94,10 +94,10 @@ class UsersRestServletV2(RestServlet):
guests = parse_boolean(request, "guests", default=True) guests = parse_boolean(request, "guests", default=True)
deactivated = parse_boolean(request, "deactivated", default=False) deactivated = parse_boolean(request, "deactivated", default=False)
users = await self.store.get_users_paginate( users, total = await self.store.get_users_paginate(
start, limit, user_id, guests, deactivated start, limit, user_id, guests, deactivated
) )
ret = {"users": users} ret = {"users": users, "total": total}
if len(users) >= limit: if len(users) >= limit:
ret["next_token"] = str(start + len(users)) ret["next_token"] = str(start + len(users))
@ -199,7 +199,7 @@ class UserRestServletV2(RestServlet):
user_id, threepid["medium"], threepid["address"], current_time user_id, threepid["medium"], threepid["address"], current_time
) )
if "avatar_url" in body: if "avatar_url" in body and type(body["avatar_url"]) == str:
await self.profile_handler.set_avatar_url( await self.profile_handler.set_avatar_url(
target_user, requester, body["avatar_url"], True target_user, requester, body["avatar_url"], True
) )
@ -276,7 +276,7 @@ class UserRestServletV2(RestServlet):
user_id, threepid["medium"], threepid["address"], current_time user_id, threepid["medium"], threepid["address"], current_time
) )
if "avatar_url" in body: if "avatar_url" in body and type(body["avatar_url"]) == str:
await self.profile_handler.set_avatar_url( await self.profile_handler.set_avatar_url(
user_id, requester, body["avatar_url"], True user_id, requester, body["avatar_url"], True
) )

View file

@ -503,7 +503,8 @@ class DataStore(
self, start, limit, name=None, guests=True, deactivated=False self, start, limit, name=None, guests=True, deactivated=False
): ):
"""Function to retrieve a paginated list of users from """Function to retrieve a paginated list of users from
users list. This will return a json list of users. users list. This will return a json list of users and the
total number of users matching the filter criteria.
Args: Args:
start (int): start number to begin the query from start (int): start number to begin the query from
@ -512,35 +513,44 @@ class DataStore(
guests (bool): whether to in include guest users guests (bool): whether to in include guest users
deactivated (bool): whether to include deactivated users deactivated (bool): whether to include deactivated users
Returns: Returns:
defer.Deferred: resolves to list[dict[str, Any]] defer.Deferred: resolves to list[dict[str, Any]], int
""" """
name_filter = {}
def get_users_paginate_txn(txn):
filters = []
args = []
if name: if name:
name_filter["name"] = "%" + name + "%" filters.append("name LIKE ?")
args.append("%" + name + "%")
attr_filter = {}
if not guests: if not guests:
attr_filter["is_guest"] = 0 filters.append("is_guest = 0")
if not deactivated:
attr_filter["deactivated"] = 0
return self.db.simple_select_list_paginate( if not deactivated:
desc="get_users_paginate", filters.append("deactivated = 0")
table="users",
orderby="name", where_clause = "WHERE " + " AND ".join(filters) if len(filters) > 0 else ""
start=start,
limit=limit, sql = "SELECT COUNT(*) as total_users FROM users %s" % (where_clause)
filters=name_filter, txn.execute(sql, args)
keyvalues=attr_filter, count = txn.fetchone()[0]
retcols=[
"name", args = [self.hs.config.server_name] + args + [limit, start]
"password_hash", sql = """
"is_guest", SELECT name, user_type, is_guest, admin, deactivated, displayname, avatar_url
"admin", FROM users as u
"user_type", LEFT JOIN profiles AS p ON u.name = '@' || p.user_id || ':' || ?
"deactivated", {}
], ORDER BY u.name LIMIT ? OFFSET ?
""".format(
where_clause
) )
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)
def search_users(self, term): def search_users(self, term):
"""Function to search users list for one or more users with """Function to search users list for one or more users with

View file

@ -360,6 +360,7 @@ class UsersListTestCase(unittest.HomeserverTestCase):
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
self.assertEqual(3, len(channel.json_body["users"])) self.assertEqual(3, len(channel.json_body["users"]))
self.assertEqual(3, channel.json_body["total"])
class UserRestTestCase(unittest.HomeserverTestCase): class UserRestTestCase(unittest.HomeserverTestCase):
@ -434,6 +435,7 @@ class UserRestTestCase(unittest.HomeserverTestCase):
"admin": True, "admin": True,
"displayname": "Bob's name", "displayname": "Bob's name",
"threepids": [{"medium": "email", "address": "bob@bob.bob"}], "threepids": [{"medium": "email", "address": "bob@bob.bob"}],
"avatar_url": None,
} }
) )

View file

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Awesome Technologies Innovationslabor GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from twisted.internet import defer
from synapse.types import UserID
from tests import unittest
from tests.utils import setup_test_homeserver
class DataStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self):
hs = yield setup_test_homeserver(self.addCleanup)
self.store = hs.get_datastore()
self.user = UserID.from_string("@abcde:test")
self.displayname = "Frank"
@defer.inlineCallbacks
def test_get_users_paginate(self):
yield self.store.register_user(self.user.to_string(), "pass")
yield self.store.create_profile(self.user.localpart)
yield self.store.set_profile_displayname(self.user.localpart, self.displayname)
users, total = yield self.store.get_users_paginate(
0, 10, name="bc", guests=False
)
self.assertEquals(1, total)
self.assertEquals(self.displayname, users.pop()["displayname"])