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

Commit 7a1cefc

Browse files
dklimpelreivilibre
andauthored
Add admin API to get users' account data (#11664)
Co-authored-by: reivilibre <[email protected]>
1 parent 84bfe47 commit 7a1cefc

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

changelog.d/11664.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add admin API to get users' account data.

docs/admin_api/user_admin_api.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,81 @@ The following fields are returned in the JSON response body:
480480
- `joined_rooms` - An array of `room_id`.
481481
- `total` - Number of rooms.
482482

483+
## Account Data
484+
Gets information about account data for a specific `user_id`.
485+
486+
The API is:
487+
488+
```
489+
GET /_synapse/admin/v1/users/<user_id>/accountdata
490+
```
491+
492+
A response body like the following is returned:
493+
494+
```json
495+
{
496+
"account_data": {
497+
"global": {
498+
"m.secret_storage.key.LmIGHTg5W": {
499+
"algorithm": "m.secret_storage.v1.aes-hmac-sha2",
500+
"iv": "fwjNZatxg==",
501+
"mac": "eWh9kNnLWZUNOgnc="
502+
},
503+
"im.vector.hide_profile": {
504+
"hide_profile": true
505+
},
506+
"org.matrix.preview_urls": {
507+
"disable": false
508+
},
509+
"im.vector.riot.breadcrumb_rooms": {
510+
"rooms": [
511+
"!LxcBDAsDUVAfJDEo:matrix.org",
512+
"!MAhRxqasbItjOqxu:matrix.org"
513+
]
514+
},
515+
"m.accepted_terms": {
516+
"accepted": [
517+
"https://example.org/somewhere/privacy-1.2-en.html",
518+
"https://example.org/somewhere/terms-2.0-en.html"
519+
]
520+
},
521+
"im.vector.setting.breadcrumbs": {
522+
"recent_rooms": [
523+
"!MAhRxqasbItqxuEt:matrix.org",
524+
"!ZtSaPCawyWtxiImy:matrix.org"
525+
]
526+
}
527+
},
528+
"rooms": {
529+
"!GUdfZSHUJibpiVqHYd:matrix.org": {
530+
"m.fully_read": {
531+
"event_id": "$156334540fYIhZ:matrix.org"
532+
}
533+
},
534+
"!tOZwOOiqwCYQkLhV:matrix.org": {
535+
"m.fully_read": {
536+
"event_id": "$xjsIyp4_NaVl2yPvIZs_k1Jl8tsC_Sp23wjqXPno"
537+
}
538+
}
539+
}
540+
}
541+
}
542+
```
543+
544+
**Parameters**
545+
546+
The following parameters should be set in the URL:
547+
548+
- `user_id` - fully qualified: for example, `@user:server.com`.
549+
550+
**Response**
551+
552+
The following fields are returned in the JSON response body:
553+
554+
- `account_data` - A map containing the account data for the user
555+
- `global` - A map containing the global account data for the user
556+
- `rooms` - A map containing the account data per room for the user
557+
483558
## User media
484559

485560
### List media uploaded by a user

synapse/rest/admin/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
from synapse.rest.admin.statistics import UserMediaStatisticsRestServlet
7070
from synapse.rest.admin.username_available import UsernameAvailableRestServlet
7171
from synapse.rest.admin.users import (
72+
AccountDataRestServlet,
7273
AccountValidityRenewServlet,
7374
DeactivateAccountRestServlet,
7475
PushersRestServlet,
@@ -255,6 +256,7 @@ def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
255256
UserMediaStatisticsRestServlet(hs).register(http_server)
256257
EventReportDetailRestServlet(hs).register(http_server)
257258
EventReportsRestServlet(hs).register(http_server)
259+
AccountDataRestServlet(hs).register(http_server)
258260
PushersRestServlet(hs).register(http_server)
259261
MakeRoomAdminRestServlet(hs).register(http_server)
260262
ShadowBanRestServlet(hs).register(http_server)

synapse/rest/admin/users.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,3 +1121,33 @@ async def on_DELETE(
11211121
await self.store.delete_ratelimit_for_user(user_id)
11221122

11231123
return HTTPStatus.OK, {}
1124+
1125+
1126+
class AccountDataRestServlet(RestServlet):
1127+
"""Retrieve the given user's account data"""
1128+
1129+
PATTERNS = admin_patterns("/users/(?P<user_id>[^/]*)/accountdata")
1130+
1131+
def __init__(self, hs: "HomeServer"):
1132+
self._auth = hs.get_auth()
1133+
self._store = hs.get_datastore()
1134+
self._is_mine_id = hs.is_mine_id
1135+
1136+
async def on_GET(
1137+
self, request: SynapseRequest, user_id: str
1138+
) -> Tuple[int, JsonDict]:
1139+
await assert_requester_is_admin(self._auth, request)
1140+
1141+
if not self._is_mine_id(user_id):
1142+
raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only look up local users")
1143+
1144+
if not await self._store.get_user_by_id(user_id):
1145+
raise NotFoundError("User not found")
1146+
1147+
global_data, by_room_data = await self._store.get_account_data_for_user(user_id)
1148+
return HTTPStatus.OK, {
1149+
"account_data": {
1150+
"global": global_data,
1151+
"rooms": by_room_data,
1152+
},
1153+
}

tests/rest/admin/test_user.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3883,3 +3883,93 @@ def test_success(self):
38833883
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
38843884
self.assertNotIn("messages_per_second", channel.json_body)
38853885
self.assertNotIn("burst_count", channel.json_body)
3886+
3887+
3888+
class AccountDataTestCase(unittest.HomeserverTestCase):
3889+
3890+
servlets = [
3891+
synapse.rest.admin.register_servlets,
3892+
login.register_servlets,
3893+
]
3894+
3895+
def prepare(self, reactor, clock, hs) -> None:
3896+
self.store = hs.get_datastore()
3897+
3898+
self.admin_user = self.register_user("admin", "pass", admin=True)
3899+
self.admin_user_tok = self.login("admin", "pass")
3900+
3901+
self.other_user = self.register_user("user", "pass")
3902+
self.url = f"/_synapse/admin/v1/users/{self.other_user}/accountdata"
3903+
3904+
def test_no_auth(self) -> None:
3905+
"""Try to get information of a user without authentication."""
3906+
channel = self.make_request("GET", self.url, {})
3907+
3908+
self.assertEqual(HTTPStatus.UNAUTHORIZED, channel.code, msg=channel.json_body)
3909+
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
3910+
3911+
def test_requester_is_no_admin(self) -> None:
3912+
"""If the user is not a server admin, an error is returned."""
3913+
other_user_token = self.login("user", "pass")
3914+
3915+
channel = self.make_request(
3916+
"GET",
3917+
self.url,
3918+
access_token=other_user_token,
3919+
)
3920+
3921+
self.assertEqual(HTTPStatus.FORBIDDEN, channel.code, msg=channel.json_body)
3922+
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
3923+
3924+
def test_user_does_not_exist(self) -> None:
3925+
"""Tests that a lookup for a user that does not exist returns a 404"""
3926+
url = "/_synapse/admin/v1/users/@unknown_person:test/override_ratelimit"
3927+
3928+
channel = self.make_request(
3929+
"GET",
3930+
url,
3931+
access_token=self.admin_user_tok,
3932+
)
3933+
3934+
self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
3935+
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
3936+
3937+
def test_user_is_not_local(self) -> None:
3938+
"""Tests that a lookup for a user that is not a local returns a 400"""
3939+
url = "/_synapse/admin/v1/users/@unknown_person:unknown_domain/accountdata"
3940+
3941+
channel = self.make_request(
3942+
"GET",
3943+
url,
3944+
access_token=self.admin_user_tok,
3945+
)
3946+
3947+
self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
3948+
self.assertEqual("Can only look up local users", channel.json_body["error"])
3949+
3950+
def test_success(self) -> None:
3951+
"""Request account data should succeed for an admin."""
3952+
3953+
# add account data
3954+
self.get_success(
3955+
self.store.add_account_data_for_user(self.other_user, "m.global", {"a": 1})
3956+
)
3957+
self.get_success(
3958+
self.store.add_account_data_to_room(
3959+
self.other_user, "test_room", "m.per_room", {"b": 2}
3960+
)
3961+
)
3962+
3963+
channel = self.make_request(
3964+
"GET",
3965+
self.url,
3966+
access_token=self.admin_user_tok,
3967+
)
3968+
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
3969+
self.assertEqual(
3970+
{"a": 1}, channel.json_body["account_data"]["global"]["m.global"]
3971+
)
3972+
self.assertEqual(
3973+
{"b": 2},
3974+
channel.json_body["account_data"]["rooms"]["test_room"]["m.per_room"],
3975+
)

0 commit comments

Comments
 (0)