From 8e1b3e4b8171ad81f09dce5009a1978f1283d0b0 Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Thu, 9 Mar 2023 16:39:20 -0500 Subject: [PATCH] manage.py: separated the user management logic --- sachet/server/auth/manage.py | 33 ++++++++++++++++++++++++++++++++ sachet/server/commands.py | 37 ++++++++++-------------------------- 2 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 sachet/server/auth/manage.py diff --git a/sachet/server/auth/manage.py b/sachet/server/auth/manage.py new file mode 100644 index 0000000..f9ccab1 --- /dev/null +++ b/sachet/server/auth/manage.py @@ -0,0 +1,33 @@ +from sachet.server import app, db +from sachet.server.models import User + +def create_user(admin, username, password): + user = User.query.filter_by(username=username).first() + if not user: + user = User( + username=username, + password=password, + admin=admin + ) + db.session.add(user) + db.session.commit() + else: + raise KeyError(f"User '{username}' already exists (ID: {user.id})") + +def delete_user_by_username(username): + user = User.query.filter_by(username=username).first() + + if not user: + raise KeyError(f"User {username} does not exist.") + else: + db.session.delete(user) + db.session.commit() + +def delete_user_by_id(id): + user = User.query.filter_by(id=id).first() + + if not user: + raise KeyError(f"User with ID {id} does not exist.") + else: + db.session.delete(user) + db.session.commit() diff --git a/sachet/server/commands.py b/sachet/server/commands.py index 8fc9863..70d271d 100644 --- a/sachet/server/commands.py +++ b/sachet/server/commands.py @@ -1,6 +1,7 @@ import click from sachet.server import app, db from sachet.server.models import User +from sachet.server.auth import manage from flask.cli import AppGroup @@ -11,7 +12,6 @@ def create_db(): """Create all db tables.""" db.create_all() - @db_cli.command("drop") @click.option('--yes', is_flag=True, expose_value=False, prompt="Are you sure you want to drop all tables?") def drop_db(): @@ -32,34 +32,17 @@ user_cli = AppGroup("user") help="Sets the user's password (for security, avoid setting this from the command line).") def create_user(admin, username, password): """Create a user directly in the database.""" - user = User.query.filter_by(username=username).first() - if not user: - user = User( - username=username, - password=password, - admin=admin - ) - db.session.add(user) - db.session.commit() - else: - raise Exception(f"User '{username}' already exists (ID: {user.id})") + manage.create_user(admin, username, password) @user_cli.command("delete") -@click.option("--username", "selector", help="Delete by username.", flag_value="username", default=True) -@click.option("--id", "selector", help="Delete by ID.", flag_value="ID", default=True) -@click.argument("usersel") +@click.option("--username", "selector_type", help="Delete by username.", flag_value="username", default=True) +@click.option("--id", "selector_type", help="Delete by ID.", flag_value="ID", default=True) +@click.argument("selector") @click.option('--yes', is_flag=True, expose_value=False, prompt=f"Are you sure you want to delete this user?") -def delete_user(selector, usersel): - if selector == "username": - user = User.query.filter_by(username=usersel).first() - elif selector == "ID": - user = User.query.filter_by(id=usersel).first() - - if not user: - raise KeyError(f"User ({selector} {usersel}) does not exist.") - else: - db.session.delete(user) - db.session.commit() - +def delete_user(selector_type, selector): + if selector_type == "username": + manage.delete_user_by_username(selector) + elif selector_type == "ID": + manage.delete_user_by_id(selector) app.cli.add_command(user_cli)