Skip to content

Commit

Permalink
create admin on startup in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
liberty-rising committed Dec 26, 2023
1 parent 04cbe7c commit e6ef757
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Empty file added backend/envs/prod/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions backend/envs/prod/initialization/setup_prod_environment.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
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.")
10 changes: 8 additions & 2 deletions backend/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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":
Expand Down

0 comments on commit e6ef757

Please sign in to comment.