#!/usr/bin/env python3 """ Script to create an initial admin user for the CMT system. Run this after setting up the database to create the first admin user. """ import sys import os # Add the parent directory to the path so we can import from app sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sqlmodel import Session from app.core.db import engine from app.core.auth import get_password_hash from app.schemas.models import User from app.schemas.base import UserRole def create_admin_user(): """Create an initial admin user.""" username = input("Enter admin username: ").strip() if not username: print("Username cannot be empty!") return password = input("Enter admin password: ").strip() if not password: print("Password cannot be empty!") return # Hash the password hashed_password = get_password_hash(password) # Create the user admin_user = User( username=username, password_hash=hashed_password, role=UserRole.ADMIN ) try: with Session(engine) as session: # Check if user already exists from sqlmodel import select statement = select(User).where(User.username == username) existing = session.exec(statement).first() if existing: print(f"User '{username}' already exists!") return session.add(admin_user) session.commit() session.refresh(admin_user) print(f"✅ Admin user '{username}' created successfully!") print(f"User ID: {admin_user.id}") print(f"Role: {admin_user.role}") except Exception as e: print(f"❌ Error creating admin user: {e}") if __name__ == "__main__": print("=== CMT Admin User Creation ===") create_admin_user()