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

Allow registering admin users using the module API #12250

Merged
merged 3 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/12250.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow registering admin users using the module API. Contributed by Famedly.
4 changes: 4 additions & 0 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,15 +611,18 @@ def register_user(
localpart: str,
displayname: Optional[str] = None,
emails: Optional[List[str]] = None,
admin: bool = False,
) -> "defer.Deferred[str]":
"""Registers a new user with given localpart and optional displayname, emails.

Added in Synapse v1.2.0.
Changed in Synapse v1.56.0: add 'admin' argument to register the user as admin.

Args:
localpart: The localpart of the new user.
displayname: The displayname of the new user.
emails: Emails to bind to the new user.
admin: True if the user should be registered as a server admin.

Raises:
SynapseError if there is an error performing the registration. Check the
Expand All @@ -633,6 +636,7 @@ def register_user(
localpart=localpart,
default_display_name=displayname,
bind_emails=emails or [],
admin=admin,
)
)

Expand Down
10 changes: 10 additions & 0 deletions tests/module_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ def test_can_register_user(self):
displayname = self.get_success(self.store.get_profile_displayname("bob"))
self.assertEqual(displayname, "Bobberino")

def test_can_register_admin_user(self):
user_id = self.get_success(
self.register_user(
"bob_module_admin", "1234", displayname="Bobberino Admin", admin=True
)
)
found_user = self.get_success(self.module_api.get_userinfo_by_id(user_id))
self.assertEqual(found_user.user_id.to_string(), user_id)
self.assertIdentical(found_user.is_admin, True)

def test_get_userinfo_by_id(self):
user_id = self.register_user("alice", "1234")
found_user = self.get_success(self.module_api.get_userinfo_by_id(user_id))
Expand Down