manage.py: separated the user management logic

This commit is contained in:
dogeystamp 2023-03-09 16:39:20 -05:00
parent 00314f155a
commit 8e1b3e4b81
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
2 changed files with 43 additions and 27 deletions

View File

@ -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()

View File

@ -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)