File size: 1,766 Bytes
bb56382
971fa87
 
 
 
 
 
 
 
bb56382
 
 
 
971fa87
 
 
 
 
 
bb56382
971fa87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""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())