Files
yakpanel-core/YakPanel-server/backend/scripts/seed_admin.py

44 lines
1.4 KiB
Python
Raw Normal View History

2026-04-07 02:04:22 +05:30
"""Seed admin user for YakPanel"""
import asyncio
import sys
import os
# Run from backend directory
backend_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.chdir(backend_dir)
sys.path.insert(0, backend_dir)
from sqlalchemy import select
from app.core.database import AsyncSessionLocal, init_db
from app.core.security import get_password_hash
from app.models.user import User
2026-04-07 03:57:44 +05:30
async def seed(*, reset_password: bool = False):
2026-04-07 02:04:22 +05:30
await init_db()
async with AsyncSessionLocal() as db:
result = await db.execute(select(User).where(User.username == "admin"))
2026-04-07 03:57:44 +05:30
existing = result.scalar_one_or_none()
if existing:
if reset_password:
existing.password = get_password_hash("admin")
existing.is_active = True
await db.commit()
print("Admin password reset: username=admin, password=admin")
else:
print("Admin user already exists (use --reset-password to force admin/admin)")
2026-04-07 02:04:22 +05:30
return
admin = User(
username="admin",
password=get_password_hash("admin"),
is_superuser=True,
)
db.add(admin)
await db.commit()
print("Admin user created: username=admin, password=admin")
if __name__ == "__main__":
2026-04-07 03:57:44 +05:30
reset = "--reset-password" in sys.argv
asyncio.run(seed(reset_password=reset))