From d9a18790d337aa35fc681d060de73b9e25fc540d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Thu, 11 Jul 2019 11:55:12 +0800 Subject: [PATCH] Define a password from the CLI on user creation Add a `-p/--password` option to `createuser` commands. --- CHANGELOG.md | 1 + tutor/commands/k8s.py | 11 +++++++++-- tutor/commands/local.py | 11 +++++++++-- tutor/scripts.py | 15 +++++++++------ tutor/templates/hooks/lms/createuser | 2 -- 5 files changed, 28 insertions(+), 12 deletions(-) delete mode 100644 tutor/templates/hooks/lms/createuser diff --git a/CHANGELOG.md b/CHANGELOG.md index 4171ae6..4085785 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Note: Breaking changes between versions are indicated by "💥". ## Latest +- [Feature] Modify ``createuser`` commands to define a password from the command line - [Improvement] Better yaml value parsing from command line - [Feature] Add `dev exec` command - [Bugfix] Fix incorrect notes settings definition diff --git a/tutor/commands/k8s.py b/tutor/commands/k8s.py index b58abef..c789478 100644 --- a/tutor/commands/k8s.py +++ b/tutor/commands/k8s.py @@ -115,13 +115,20 @@ def init(root): @opts.root @click.option("--superuser", is_flag=True, help="Make superuser") @click.option("--staff", is_flag=True, help="Make staff user") +@click.option( + "-p", + "--password", + help="Specify password from the command line. If undefined, you will be prompted to input a password", +) @click.argument("name") @click.argument("email") -def createuser(root, superuser, staff, name, email): +def createuser(root, superuser, staff, password, name, email): config = tutor_config.load(root) runner = K8sScriptRunner(root, config) runner.check_service_is_activated("lms") - command = scripts.create_user_command(superuser, staff, name, email) + command = scripts.create_user_command( + superuser, staff, name, email, password=password + ) kubectl_exec(config, "lms", command, attach=True) diff --git a/tutor/commands/local.py b/tutor/commands/local.py index c8e6288..f6808bb 100644 --- a/tutor/commands/local.py +++ b/tutor/commands/local.py @@ -270,13 +270,20 @@ def logs(root, follow, tail, service): @opts.root @click.option("--superuser", is_flag=True, help="Make superuser") @click.option("--staff", is_flag=True, help="Make staff user") +@click.option( + "-p", + "--password", + help="Specify password from the command line. If undefined, you will be prompted to input a password", +) @click.argument("name") @click.argument("email") -def createuser(root, superuser, staff, name, email): +def createuser(root, superuser, staff, password, name, email): config = tutor_config.load(root) runner = ScriptRunner(root, config) runner.check_service_is_activated("lms") - command = scripts.create_user_command(superuser, staff, name, email) + command = scripts.create_user_command( + superuser, staff, name, email, password=password + ) runner.exec("lms", command) diff --git a/tutor/scripts.py b/tutor/scripts.py index c689a5f..a8d07a5 100644 --- a/tutor/scripts.py +++ b/tutor/scripts.py @@ -58,17 +58,20 @@ def initialise(runner): fmt.echo_info("All services initialised.") -def create_user_command(superuser, staff, username, email): +def create_user_command(superuser, staff, username, email, password=None): opts = "" if superuser: opts += " --superuser" if staff: opts += " --staff" - command = ( - "./manage.py lms --settings=tutor.production manage_user {opts} {username} {email}\n" - "./manage.py lms --settings=tutor.production changepassword {username}" - ).format(opts=opts, username=username, email=email) - return command + command = "./manage.py lms --settings=tutor.production manage_user {opts} {username} {email}\n" + if password: + command += "./manage.py lms --settings=tutor.production shell -c \"from django.contrib.auth import get_user_model; u = get_user_model().objects.get(username='{username}'); u.set_password('{password}'); u.save()\"" + else: + command += ( + "./manage.py lms --settings=tutor.production changepassword {username}" + ) + return command.format(opts=opts, username=username, email=email, password=password) def import_demo_course(runner): diff --git a/tutor/templates/hooks/lms/createuser b/tutor/templates/hooks/lms/createuser deleted file mode 100644 index b9842f4..0000000 --- a/tutor/templates/hooks/lms/createuser +++ /dev/null @@ -1,2 +0,0 @@ -./manage.py lms --settings=tutor.production manage_user {{ OPTS }} {{ USERNAME }} {{ EMAIL }} -./manage.py lms --settings=tutor.production changepassword {{ USERNAME }}