95 lines
3.0 KiB
Diff
95 lines
3.0 KiB
Diff
|
From 1789416df425d22693b0055a6688d8686e0ee4a1 Mon Sep 17 00:00:00 2001
|
||
|
From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= <joerg@thalheim.io>
|
||
|
Date: Thu, 13 Jun 2024 14:38:19 +0200
|
||
|
Subject: [PATCH 2/2] register-new-matrix-user: add a flag to ignore already
|
||
|
existing users
|
||
|
MIME-Version: 1.0
|
||
|
Content-Type: text/plain; charset=UTF-8
|
||
|
Content-Transfer-Encoding: 8bit
|
||
|
|
||
|
This allows to register users in a more declarative and stateless way.
|
||
|
|
||
|
Signed-off-by: Jörg Thalheim <joerg@thalheim.io>
|
||
|
---
|
||
|
synapse/_scripts/register_new_matrix_user.py | 22 ++++++++++++++++++--
|
||
|
1 file changed, 20 insertions(+), 2 deletions(-)
|
||
|
|
||
|
diff --git a/synapse/_scripts/register_new_matrix_user.py b/synapse/_scripts/register_new_matrix_user.py
|
||
|
index 972b35e2d..233e7267d 100644
|
||
|
--- a/synapse/_scripts/register_new_matrix_user.py
|
||
|
+++ b/synapse/_scripts/register_new_matrix_user.py
|
||
|
@@ -52,6 +52,7 @@ def request_registration(
|
||
|
user_type: Optional[str] = None,
|
||
|
_print: Callable[[str], None] = print,
|
||
|
exit: Callable[[int], None] = sys.exit,
|
||
|
+ exists_ok: bool = False,
|
||
|
) -> None:
|
||
|
url = "%s/_synapse/admin/v1/register" % (server_location.rstrip("/"),)
|
||
|
|
||
|
@@ -97,6 +98,10 @@ def request_registration(
|
||
|
r = requests.post(url, json=data)
|
||
|
|
||
|
if r.status_code != 200:
|
||
|
+ response = r.json()
|
||
|
+ if exists_ok and response["errcode"] == "M_USER_IN_USE":
|
||
|
+ _print("User already exists. Skipping.")
|
||
|
+ return
|
||
|
_print("ERROR! Received %d %s" % (r.status_code, r.reason))
|
||
|
if 400 <= r.status_code < 500:
|
||
|
try:
|
||
|
@@ -115,6 +120,7 @@ def register_new_user(
|
||
|
shared_secret: str,
|
||
|
admin: Optional[bool],
|
||
|
user_type: Optional[str],
|
||
|
+ exists_ok: bool = False,
|
||
|
) -> None:
|
||
|
if not user:
|
||
|
try:
|
||
|
@@ -154,7 +160,13 @@ def register_new_user(
|
||
|
admin = False
|
||
|
|
||
|
request_registration(
|
||
|
- user, password, server_location, shared_secret, bool(admin), user_type
|
||
|
+ user,
|
||
|
+ password,
|
||
|
+ server_location,
|
||
|
+ shared_secret,
|
||
|
+ bool(admin),
|
||
|
+ user_type,
|
||
|
+ exists_ok=exists_ok,
|
||
|
)
|
||
|
|
||
|
|
||
|
@@ -173,6 +185,11 @@ def main() -> None:
|
||
|
default=None,
|
||
|
help="Local part of the new user. Will prompt if omitted.",
|
||
|
)
|
||
|
+ parser.add_argument(
|
||
|
+ "--exists-ok",
|
||
|
+ action="store_true",
|
||
|
+ help="Do not fail if user already exists.",
|
||
|
+ )
|
||
|
password_group = parser.add_mutually_exclusive_group()
|
||
|
password_group.add_argument(
|
||
|
"-p",
|
||
|
@@ -192,6 +209,7 @@ def main() -> None:
|
||
|
default=None,
|
||
|
help="User type as specified in synapse.api.constants.UserTypes",
|
||
|
)
|
||
|
+
|
||
|
admin_group = parser.add_mutually_exclusive_group()
|
||
|
admin_group.add_argument(
|
||
|
"-a",
|
||
|
@@ -281,7 +299,7 @@ def main() -> None:
|
||
|
if args.admin or args.no_admin:
|
||
|
admin = args.admin
|
||
|
|
||
|
- register_new_user(args.user, password, server_url, secret, admin, args.user_type)
|
||
|
+ register_new_user(args.user, password, server_url, secret, admin, args.user_type, exists_ok=args.exists_ok)
|
||
|
|
||
|
|
||
|
def _read_file(file_path: Any, config_path: str) -> str:
|
||
|
--
|
||
|
2.44.1
|
||
|
|