bot_host / create_admin.py
ItsBounvy's picture
Upload 24 files
bb56382 verified
Raw History Blame Contribute Delete
1.77 kB
"""CLI helper to create an admin user for the hosting panel."""
from __future__ import annotations
import asyncio
import getpass
import sys
from sqlalchemy import select
from auth import hash_password, generate_invite_code
from config import PANEL_CONFIG
from database import init_db, async_session
from models import User, UserRole
async def create_admin() -> None:
await init_db()
print("=" * 50)
print(" MMaxAI Hosting Panel — Create Admin User")
print("=" * 50)
username = input("Username: ").strip()
if not username:
print("Username is required.")
sys.exit(1)
email = input("Email (optional): ").strip() or None
password = getpass.getpass("Password: ")
confirm = getpass.getpass("Confirm password: ")
if password != confirm:
print("Passwords do not match.")
sys.exit(1)
if len(password) < PANEL_CONFIG.password_min_length:
print(f"Password must be at least {PANEL_CONFIG.password_min_length} characters.")
sys.exit(1)
async with async_session() as session:
result = await session.execute(select(User).where(User.username == username))
if result.scalar_one_or_none():
print(f"User '{username}' already exists.")
sys.exit(1)
user = User(
username=username,
email=email,
password_hash=hash_password(password),
role=UserRole.ADMIN,
is_active=True,
invite_code=generate_invite_code(),
)
session.add(user)
await session.commit()
print(f"\nAdmin user '{username}' created successfully.")
print(f"Invite code: {user.invite_code}")
if __name__ == "__main__":
asyncio.run(create_admin())