From e6ef757bbe0b4a25308d34b2339451c342adb01d Mon Sep 17 00:00:00 2001 From: liberty-rising Date: Tue, 26 Dec 2023 18:15:56 +0100 Subject: [PATCH] create admin on startup in prod --- backend/envs/prod/__init__.py | 0 .../initialization/setup_prod_environment.py | 27 +++++++++++++++++++ backend/startup.py | 10 +++++-- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 backend/envs/prod/__init__.py create mode 100644 backend/envs/prod/initialization/setup_prod_environment.py diff --git a/backend/envs/prod/__init__.py b/backend/envs/prod/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/envs/prod/initialization/setup_prod_environment.py b/backend/envs/prod/initialization/setup_prod_environment.py new file mode 100644 index 0000000..b1eba04 --- /dev/null +++ b/backend/envs/prod/initialization/setup_prod_environment.py @@ -0,0 +1,27 @@ +from databases.database_manager import DatabaseManager +from databases.user_manager import UserManager +from models.user import User +from security import get_password_hash +from utils.utils import get_app_logger + +logger = get_app_logger(__name__) + + +def create_admin_user(): + """Creates an admin user if it doesn't already exist.""" + admin_user = User( + username="admin", + hashed_password=get_password_hash("admin"), + email="admin@docshow.ai", + organization_id=1, + role="admin", + ) + + with DatabaseManager() as session: + user_manager = UserManager(session) + existing_user = user_manager.get_user_by_username(admin_user.username) + if not existing_user: + user_manager.create_user(admin_user) + logger.debug("Admin user created.") + else: + logger.debug("Admin user already exists.") diff --git a/backend/startup.py b/backend/startup.py index 74e6d46..1621a17 100644 --- a/backend/startup.py +++ b/backend/startup.py @@ -9,11 +9,14 @@ >>> run_startup_routines() """ from envs.dev.initialization.setup_dev_environment import ( - create_admin_user, + create_admin_user as dev_create_admin_user, create_sample_dashboard, create_sample_organization, create_sample_dataprofile, ) +from envs.prod.initialization.setup_prod_environment import ( + create_admin_user as prod_create_admin_user, +) from envs.dev.utils import seed_db from settings import APP_ENV, JWT_SECRET_KEY from utils.utils import get_app_logger @@ -27,11 +30,14 @@ def run_startup_routines(): if APP_ENV == "development": create_sample_organization() - create_admin_user() + dev_create_admin_user() seed_db() create_sample_dashboard() create_sample_dataprofile() + if APP_ENV == "prod": + prod_create_admin_user() + def check_jwt_secret_key(): if APP_ENV != "development" and JWT_SECRET_KEY == "mysecretkey":