Skip to content

Commit c99203d

Browse files
Mic92anoadragon453
andauthored
register-new-matrix-user: add a flag to ignore already existing users (#17304)
Co-authored-by: Andrew Morgan <[email protected]>
1 parent 9104a9f commit c99203d

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

changelog.d/17304.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`register_new_matrix_user` now supports a --exists-ok flag to allow registration of users that already exist in the database.
2+
This is useful for scripts that bootstrap user accounts with initial passwords.

debian/changelog

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
matrix-synapse-py3 (1.109.0+nmu1) UNRELEASED; urgency=medium
22

3-
* `register_new_matrix_user` now supports a --password-file flag.
3+
* `register_new_matrix_user` now supports a --password-file and a --exists-ok flag.
44

55
-- Synapse Packaging team <[email protected]> Tue, 18 Jun 2024 13:29:36 +0100
66

debian/register_new_matrix_user.ronn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ A sample YAML file accepted by `register_new_matrix_user` is described below:
4848
Shared secret as defined in server config file. This is an optional
4949
parameter as it can be also supplied via the YAML file.
5050

51+
* `--exists-ok`:
52+
Do not fail if the user already exists. The user account will be not updated in this case.
53+
5154
* `server_url`:
5255
URL of the home server. Defaults to 'https://localhost:8448'.
5356

synapse/_scripts/register_new_matrix_user.py

Lines changed: 28 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,15 @@ 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(
303+
args.user,
304+
password,
305+
server_url,
306+
secret,
307+
admin,
308+
args.user_type,
309+
exists_ok=args.exists_ok,
310+
)
285311

286312

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

0 commit comments

Comments
 (0)