46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
"""Create a superuser account."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import sys
|
|
|
|
from app.core.constants import Role
|
|
from app.core.security import hash_password
|
|
from app.db.mariadb import AsyncSessionLocal, async_engine
|
|
from app.models.mariadb.user import User, UserProfile
|
|
|
|
|
|
async def create_superuser(email: str, password: str, full_name: str = "Super Admin") -> None:
|
|
async with AsyncSessionLocal() as session:
|
|
user = User(
|
|
email=email,
|
|
hashed_password=hash_password(password),
|
|
role=Role.SUPERADMIN,
|
|
is_active=True,
|
|
is_verified=True,
|
|
)
|
|
session.add(user)
|
|
await session.flush()
|
|
|
|
profile = UserProfile(
|
|
user_id=user.id,
|
|
full_name=full_name,
|
|
)
|
|
session.add(profile)
|
|
await session.commit()
|
|
|
|
await async_engine.dispose()
|
|
print(f"Superuser '{email}' created successfully!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 3:
|
|
print("Usage: python -m scripts.create_superuser <email> <password> [full_name]")
|
|
sys.exit(1)
|
|
|
|
email = sys.argv[1]
|
|
password = sys.argv[2]
|
|
name = sys.argv[3] if len(sys.argv) > 3 else "Super Admin"
|
|
|
|
asyncio.run(create_superuser(email, password, name))
|