From f249563b2c7442934d74c84e83efa4f44514388c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 22 Jul 2019 13:57:09 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20At=20least=20use=20configured=20?= =?UTF-8?q?user=20manager=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Making the plugin hook work would be a ton of overhead due to the components dictionary that needs to be supplied to it for user manager initialization, however at the very least we should support configuration of the used user manager class. Both features are rarely used if at all, but we should be aware of this. Also changed some wording, fixed to unicode and removed redundancy from the sub commands (e.g. just "add" instead of "add_user") and flags. --- src/octoprint/cli/user.py | 71 ++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/src/octoprint/cli/user.py b/src/octoprint/cli/user.py index 32ea02482..07026c553 100644 --- a/src/octoprint/cli/user.py +++ b/src/octoprint/cli/user.py @@ -1,5 +1,5 @@ # coding=utf-8 -from __future__ import absolute_import, division, print_function +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' __copyright__ = "Copyright (C) 2019 The OctoPrint Project - Released under terms of the AGPLv3 License" @@ -11,6 +11,9 @@ import logging from octoprint import init_settings from octoprint.cli import get_ctx_obj_option from octoprint.users import FilebasedUserManager, UnknownUser +from octoprint.util import get_class + +click.disable_unicode_literals_warning = True #~~ "octoprint user" commands @@ -23,55 +26,69 @@ def user_commands(): @user_commands.group(name="user") @click.pass_context def user(ctx): - """User account management""" + """ + User management. + + Note that this currently only supports managing user accounts stored in the configured user manager, not any + user managers added through plugins and the "octoprint.users.factory" hook. + """ try: logging.basicConfig(level=logging.DEBUG if get_ctx_obj_option(ctx, "verbosity", 0) > 0 else logging.WARN) - settingz = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) - ctx.obj.users = FilebasedUserManager(settingz=settingz) + settings = init_settings(get_ctx_obj_option(ctx, "basedir", None), get_ctx_obj_option(ctx, "configfile", None)) + + name = settings.get(["accessControl", "userManager"]) + try: + clazz = get_class(name) + manager = clazz(settings=settings) + except Exception: + click.echo("Could not instantiate user manager {}, falling back to FilebasedUserManager!".format(name), err=True) + manager = FilebasedUserManager(settings=settings) + finally: + manager.enabled = settings.getBoolean(["accessControl", "enabled"]) + + ctx.obj.user_manager = manager + except Exception: - click.echo("Could not instantiate user manager") + click.echo("Could not instantiate user manager", err=True) return -@user.command(name="add_user") +@user.command(name="add") @click.argument("username", type=click.STRING, required=True) @click.password_option("--password", "password", help="Password for user") -@click.option("--is-admin", "is_admin", type=click.BOOL, default=False, - help="Sets user role as admin") +@click.option("--admin", "is_admin", type=click.BOOL, default=False, + help="Sets admin role on user") @click.pass_context def add_user_command(ctx, username, password, is_admin): - """Adds new user account""" - ctx.obj.users.addUser( - username, - password, - roles=("admin" if is_admin else "user"), - active=True - ) + """Add a new user""" + ctx.obj.user_manager.addUser(username, + password, + roles=("admin" if is_admin else "user"), + active=True) -@user.command(name="remove_user") +@user.command(name="remove") @click.argument("username", type=click.STRING) @click.pass_context def remove_user_command(ctx, username): - """Removes user account""" - confirm = click.prompt( - "This is will irreversibly destroy the user account! Enter 'yes' to confirm", - type=click.STRING) + """Remove a user""" + confirm = click.prompt("This is will irreversibly destroy the user account! Enter 'yes' to confirm", + type=click.STRING) - if confirm == 'yes': - ctx.obj.users.removeUser(username) + if confirm.lower() == "yes": + ctx.obj.user_manager.removeUser(username) else: - click.echo("User NOT removed") + click.echo("User not removed") -@user.command(name="change_password") +@user.command(name="password") @click.argument("username", type=click.STRING) @click.password_option("--password", "password", help="New password for user") @click.pass_context def change_password_command(ctx, username, password): - """Changes an existing user password""" + """Changes an existing user's password""" try: - ctx.obj.users.changeUserPassword(username, password) + ctx.obj.user_manager.changeUserPassword(username, password) except UnknownUser: - click.echo("User does not exist!") + click.echo("This user does not exist!", err=True)