Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit c6e6706

Browse files
authored
Merge pull request #136 from JOJ0/fix_threepid
User modify command threepid handling fixes
2 parents 382664d + 35781a2 commit c6e6706

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

synadm/api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,17 +603,21 @@ def user_modify(self, user_id, password, display_name, threepid,
603603
avatar_url, admin, deactivation, user_type, lock):
604604
""" Create or update information about a given user
605605
606-
Threepid should be passed as a tuple in a tuple
606+
The threepid argument must be passed as a tuple in a tuple (which is
607+
what we usually get from a Click multi-arg option)
607608
"""
608609
data = {}
609610
if password:
610611
data.update({"password": password})
611612
if display_name:
612613
data.update({"displayname": display_name})
613614
if threepid:
614-
data.update({"threepids": [
615-
{"medium": k, "address": i} for k, i in dict(threepid).items()
616-
]})
615+
if threepid == (('', ''),): # empty strings clear all threepids
616+
data.update({"threepids": []})
617+
else:
618+
data.update({"threepids": [
619+
{"medium": m, "address": a} for m, a in threepid
620+
]})
617621
if avatar_url:
618622
data.update({"avatar_url": avatar_url})
619623
if admin is not None:

synadm/cli/user.py

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,18 @@ def name_extra(self):
394394
help="Set display name. defaults to the value of user_id")
395395
@optgroup.option(
396396
"--threepid", "-t", type=str, multiple=True, nargs=2,
397-
help="""Add a third-party identifier. This can be an email address or a
398-
phone number. Threepids are used for several things: For use when
399-
logging in, as an alternative to the user id. In the case of email, as
400-
an alternative contact to help with account recovery, as well as
401-
to receive notifications of missed messages. Format: medium
402-
value (eg. --threepid email <user@example.org>). This option can also
403-
be stated multiple times, i.e. a user can have multiple threepids
404-
configured.""")
397+
help="""Set a third-party identifier (email address or phone number). Pass
398+
two arguments: `medium value` (eg. `--threepid email <user@example.org>`).
399+
This option can be passed multiple times, which allows setting multiple
400+
entries for a user. When modifying existing users, all threepids are
401+
replaced by what's passed in all given `--threepid` options. Threepids are
402+
used for several things: For use when logging in, as an alternative to the
403+
user id; in the case of email, as an alternative contact to help with
404+
account recovery, as well as to receive notifications of missed
405+
messages.""")
406+
@optgroup.option(
407+
"--clear-threepids", is_flag=True, default=None,
408+
help="Remove all threepids of an existing user.")
405409
@optgroup.option(
406410
"--avatar-url", "-v", type=str,
407411
help="""Set avatar URL. Must be a MXC URI
@@ -435,7 +439,8 @@ def name_extra(self):
435439
@click.pass_obj
436440
@click.pass_context
437441
def modify(ctx, helper, user_id, password, password_prompt, display_name,
438-
threepid, avatar_url, admin, deactivation, user_type, lock):
442+
threepid, clear_threepids, avatar_url, admin, deactivation,
443+
user_type, lock):
439444
""" Create or modify a local user. Provide matrix user ID (@user:server)
440445
as argument.
441446
"""
@@ -453,17 +458,21 @@ def modify(ctx, helper, user_id, password, password_prompt, display_name,
453458
ctx.invoke(user_details_cmd, user_id=mxid)
454459
click.echo("User account settings to be modified:")
455460
for key, value in ctx.params.items():
456-
if key in ["user_id", "password", "password_prompt"]: # skip these
461+
# skip these, they get special treatment or can't be changed
462+
if key in ["user_id", "password", "password_prompt",
463+
"clear_threepids"]:
457464
continue
458465
if key == "threepid":
459-
if value != ():
460-
for t_key, t_val in value:
461-
click.echo(f"{key}: {t_key} {t_val}")
462-
if t_key not in ["email", "msisdn"]:
463-
helper.log.warning(
464-
f"{t_key} is probably not a supported medium "
465-
"type. Threepid medium types according to the "
466-
"current matrix spec are: email, msisdn.")
466+
if value == (('', ''),) or clear_threepids:
467+
click.echo("threepid: All entries will be cleared!")
468+
continue
469+
for t_key, t_val in value:
470+
click.echo(f"{key}: {t_key} {t_val}")
471+
if t_key not in ["email", "msisdn"]:
472+
helper.log.warning(
473+
f"{t_key} is probably not a supported medium "
474+
"type. Threepid medium types according to the "
475+
"current matrix spec are: email, msisdn.")
467476
elif key == "user_type" and value == 'regular':
468477
click.echo("user_type: null")
469478
elif value not in [None, {}, []]: # only show non-empty (aka changed)
@@ -482,21 +491,27 @@ def modify(ctx, helper, user_id, password, password_prompt, display_name,
482491
password = None
483492
sure = (
484493
helper.no_confirm or
485-
click.prompt("Are you sure you want to modify user? (y/N)",
494+
click.prompt("Are you sure you want to modify/create user? (y/N)",
486495
type=bool, default=False, show_default=False)
487496
)
488497
if sure:
489498
modified = helper.api.user_modify(
490-
mxid, password, display_name, threepid,
491-
avatar_url, admin, deactivation,
492-
'null' if user_type == 'regular' else user_type, lock)
499+
mxid,
500+
password,
501+
display_name,
502+
(('', ''),) if clear_threepids else threepid,
503+
avatar_url,
504+
admin,
505+
deactivation,
506+
'null' if user_type == 'regular' else user_type, lock
507+
)
493508
if modified is None:
494-
click.echo("User could not be modified.")
509+
click.echo("User could not be modified/created.")
495510
raise SystemExit(1)
496511
if helper.output_format == "human":
497512
if modified != {}:
498513
helper.output(modified)
499-
click.echo("User successfully modified.")
514+
click.echo("User successfully modified/created.")
500515
else:
501516
click.echo("Synapse returned: {}".format(modified))
502517
else:

0 commit comments

Comments
 (0)