summary refs log tree commit diff
diff options
context:
space:
mode:
authorAmanda Graven <amanda@amandag.net>2022-04-01 10:31:30 +0200
committerGitHub <noreply@github.com>2022-04-01 09:31:30 +0100
commit4e900ece42eb0d28c2773d328b4ab459b0d3aaf3 (patch)
treed7397f894506bbdc7bbf7bd5b25996664b8d1b14
parentMove MSC2654 support behind an experimental configuration flag. (#12295) (diff)
downloadsynapse-4e900ece42eb0d28c2773d328b4ab459b0d3aaf3.tar.xz
Add set_user_admin function to the module API (#12341)
-rw-r--r--changelog.d/12341.feature1
-rw-r--r--synapse/module_api/__init__.py11
-rw-r--r--tests/module_api/test_api.py14
3 files changed, 26 insertions, 0 deletions
diff --git a/changelog.d/12341.feature b/changelog.d/12341.feature
new file mode 100644
index 0000000000..ebb96ee486
--- /dev/null
+++ b/changelog.d/12341.feature
@@ -0,0 +1 @@
+Allow setting user admin status using the module API. Contributed by Famedly.
diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py
index 3c7dcca74d..f7f95bae29 100644
--- a/synapse/module_api/__init__.py
+++ b/synapse/module_api/__init__.py
@@ -515,6 +515,17 @@ class ModuleApi:
         """
         return await self._store.is_server_admin(UserID.from_string(user_id))
 
+    async def set_user_admin(self, user_id: str, admin: bool) -> None:
+        """Sets if a user is a server admin.
+
+        Added in Synapse v1.56.0.
+
+        Args:
+            user_id: The Matrix ID of the user to set admin status for.
+            admin: True iff the user is to be a server admin, false otherwise.
+        """
+        await self._store.set_server_admin(UserID.from_string(user_id), admin)
+
     def get_qualified_user_id(self, username: str) -> str:
         """Qualify a user id, if necessary
 
diff --git a/tests/module_api/test_api.py b/tests/module_api/test_api.py
index 10dd94b549..36dfe5c36a 100644
--- a/tests/module_api/test_api.py
+++ b/tests/module_api/test_api.py
@@ -96,6 +96,20 @@ class ModuleApiTestCase(HomeserverTestCase):
         self.assertEqual(found_user.user_id.to_string(), user_id)
         self.assertIdentical(found_user.is_admin, True)
 
+    def test_can_set_admin(self):
+        user_id = self.get_success(
+            self.register_user(
+                "alice_wants_admin",
+                "1234",
+                displayname="Alice Powerhungry",
+                admin=False,
+            )
+        )
+        self.get_success(self.module_api.set_user_admin(user_id, 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))