Skip to content

Commit f125518

Browse files
committed
register-new-matrix-user: add a flag to ignore already existing users
This allows to register users in a more declarative and stateless way. Signed-off-by: Jörg Thalheim <[email protected]>
1 parent 1992230 commit f125518

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

synapse/_scripts/register_new_matrix_user.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def request_registration(
5252
user_type: Optional[str] = None,
5353
_print: Callable[[str], None] = print,
5454
exit: Callable[[int], None] = sys.exit,
55+
exists_ok: bool = False,
5556
) -> None:
5657
url = "%s/_synapse/admin/v1/register" % (server_location.rstrip("/"),)
5758

@@ -97,6 +98,10 @@ def request_registration(
9798
r = requests.post(url, json=data)
9899

99100
if r.status_code != 200:
101+
response = r.json()
102+
if exists_ok and response["errcode"] == "M_USER_IN_USE":
103+
_print("User already exists. Skipping.")
104+
return
100105
_print("ERROR! Received %d %s" % (r.status_code, r.reason))
101106
if 400 <= r.status_code < 500:
102107
try:
@@ -115,6 +120,7 @@ def register_new_user(
115120
shared_secret: str,
116121
admin: Optional[bool],
117122
user_type: Optional[str],
123+
exists_ok: bool = False,
118124
) -> None:
119125
if not user:
120126
try:
@@ -154,7 +160,13 @@ def register_new_user(
154160
admin = False
155161

156162
request_registration(
157-
user, password, server_location, shared_secret, bool(admin), user_type
163+
user,
164+
password,
165+
server_location,
166+
shared_secret,
167+
bool(admin),
168+
user_type,
169+
exists_ok=exists_ok,
158170
)
159171

160172

@@ -173,6 +185,11 @@ def main() -> None:
173185
default=None,
174186
help="Local part of the new user. Will prompt if omitted.",
175187
)
188+
parser.add_argument(
189+
"--exists-ok",
190+
action="store_true",
191+
help="Do not fail if user already exists.",
192+
)
176193
password_group = parser.add_mutually_exclusive_group()
177194
password_group.add_argument(
178195
"-p",
@@ -192,6 +209,7 @@ def main() -> None:
192209
default=None,
193210
help="User type as specified in synapse.api.constants.UserTypes",
194211
)
212+
195213
admin_group = parser.add_mutually_exclusive_group()
196214
admin_group.add_argument(
197215
"-a",
@@ -281,7 +299,7 @@ def main() -> None:
281299
if args.admin or args.no_admin:
282300
admin = args.admin
283301

284-
register_new_user(args.user, password, server_url, secret, admin, args.user_type)
302+
register_new_user(args.user, password, server_url, secret, admin, args.user_type, exists_ok=args.exists_ok)
285303

286304

287305
def _read_file(file_path: Any, config_path: str) -> str:

0 commit comments

Comments
 (0)