api / scripts /create_superadmin.py
hanbinsite's picture
feat: implement full API - all admin/user routes, RBAC, sync service, collector, MySQL config, superadmin hanbin credentials
9d66536
Raw History Blame
1.38 kB
import asyncio
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from sqlalchemy import select
from app.database import async_session
from app.models.admin import Admin
from app.models.role import Role, AdminRole
from app.utils.auth import hash_password
async def create_superadmin():
username = "hanbin"
password = "Han147258!"
async with async_session() as db:
existing = await db.execute(select(Admin).where(Admin.username == username))
if existing.scalar_one_or_none():
print(f"Admin '{username}' already exists, skipping.")
return
super_admin_role = await db.execute(
select(Role).where(Role.code == "super_admin")
)
role = super_admin_role.scalar_one_or_none()
if not role:
print("super_admin role not found. Run seed_roles.py first.")
return
admin = Admin(
username=username,
password_hash=hash_password(password),
nickname="韩彬",
is_active=True,
)
db.add(admin)
await db.flush()
db.add(AdminRole(admin_id=admin.id, role_id=role.id))
await db.commit()
print(f"Super admin '{username}' created with password '{password}'.")
if __name__ == "__main__":
asyncio.run(create_superadmin())