From 41e9ca178b1db3d89164ae7d56d749212d5de8d9 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Wed, 10 May 2023 21:10:52 +0200 Subject: add a fietsctl user modify command Currently, there is no way to make a user an admin after creating them - so this changes that! --- fietsboek/scripts/fietsctl.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/fietsboek/scripts/fietsctl.py b/fietsboek/scripts/fietsctl.py index caca0f9..9bf8abe 100644 --- a/fietsboek/scripts/fietsctl.py +++ b/fietsboek/scripts/fietsctl.py @@ -16,6 +16,15 @@ EXIT_OKAY = 0 EXIT_FAILURE = 1 +def human_bool(value: bool) -> str: + """Formats a boolean for CLI output. + + :param value: The value to format. + :return: The string representing the bool. + """ + return "yes" if value else "no" + + def setup(config_path: str) -> AppEnvironment: """Sets up the logging and app environment for the scripts. @@ -201,6 +210,45 @@ def cms_user_passwd( click.echo(f"Changed password of {user.name} ({user.email})") +@cmd_user.command("modify") +@config_option +@click.option("--admin/--no-admin", help="make the user an admin", default=None) +@click.option("--verified/--no-verified", help="set the user verification status", default=None) +@optgroup.group("User selection", cls=RequiredMutuallyExclusiveOptionGroup) +@optgroup.option("--id", "-i", "id_", help="database ID of the user", type=int) +@optgroup.option("--email", "-e", help="email of the user") +@click.pass_context +def cms_user_modify( + ctx: click.Context, + config: str, + admin: Optional[bool], + verified: Optional[bool], + id_: Optional[int], + email: Optional[str], +): + """Modify a user.""" + env = setup(config) + if id_ is not None: + query = select(models.User).filter_by(id=id_) + else: + query = models.User.query_by_email(email) + with env["request"].tm: + dbsession = env["request"].dbsession + user = dbsession.execute(query).scalar_one_or_none() + if user is None: + click.echo("Error: No such user found.", err=True) + ctx.exit(EXIT_FAILURE) + + if admin is not None: + user.is_admin = admin + if verified is not None: + user.is_verified = verified + + click.echo(f"{user.name} - {user.email}") + click.echo(f"Is admin: {human_bool(user.is_admin)}") + click.echo(f"Is verified: {human_bool(user.is_verified)}") + + @cmd_user.command("hittekaart") @config_option @click.option( -- cgit v1.2.3