72 lines
2.0 KiB
Python
72 lines
2.0 KiB
Python
"""
|
|
Bootstrap script to create the first admin user
|
|
This script does NOT require authentication (use only for initial setup)
|
|
|
|
Usage:
|
|
python scripts/create_first_admin.py
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
project_root = Path(__file__).parent.parent
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
from src.database.session import db_manager
|
|
from src.database.repositories import UserRepository
|
|
from src.auth.service import AuthService
|
|
|
|
|
|
def create_first_admin():
|
|
"""Create the first admin user without requiring authentication"""
|
|
print("Creating first admin user...")
|
|
print("=" * 60)
|
|
|
|
username = input("Enter admin username: ")
|
|
password = input("Enter admin password: ")
|
|
confirm_password = input("Confirm admin password: ")
|
|
|
|
if password != confirm_password:
|
|
print("Error: Passwords do not match")
|
|
sys.exit(1)
|
|
|
|
if len(password) < 8:
|
|
print("Warning: Password should be at least 8 characters")
|
|
response = input("Continue anyway? (yes/no): ")
|
|
if response.lower() != "yes":
|
|
sys.exit(1)
|
|
|
|
session = db_manager.get_session()
|
|
try:
|
|
user_repo = UserRepository(session)
|
|
|
|
if user_repo.exists(username):
|
|
print(f"Error: User '{username}' already exists")
|
|
sys.exit(1)
|
|
|
|
auth_service = AuthService(user_repo)
|
|
user = auth_service.create_user_with_hashed_password(
|
|
username=username,
|
|
password=password,
|
|
role="Admin"
|
|
)
|
|
|
|
print("=" * 60)
|
|
print(f"Success! Admin user '{user.username}' created.")
|
|
print("=" * 60)
|
|
print("\nYou can now use this account with CLI commands:")
|
|
print(f" --admin-user {username}")
|
|
print(f" --admin-password [your-password]")
|
|
|
|
except Exception as e:
|
|
print(f"Error creating admin user: {e}")
|
|
sys.exit(1)
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
create_first_admin()
|
|
|
|
|