46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Create initial admin user with pre-generated hash"""
|
|
|
|
import asyncio
|
|
import sys
|
|
|
|
sys.path.insert(0, "/app")
|
|
|
|
from sqlalchemy import text
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.orm import sessionmaker
|
|
import bcrypt
|
|
|
|
|
|
# Generate proper bcrypt hash
|
|
ADMIN_PASSWORD_HASH = bcrypt.hashpw("admin123".encode(), bcrypt.gensalt()).decode()
|
|
|
|
|
|
async def create_admin():
|
|
DATABASE_URL = "postgresql+asyncpg://postgres:postgres@postgres:5432/planet_db"
|
|
engine = create_async_engine(DATABASE_URL, echo=False)
|
|
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
async with async_session() as session:
|
|
result = await session.execute(
|
|
text("SELECT id FROM users WHERE username = 'admin'")
|
|
)
|
|
if result.fetchone():
|
|
print("Admin user already exists")
|
|
return
|
|
|
|
await session.execute(
|
|
text("""
|
|
INSERT INTO users (username, email, password_hash, role, is_active, created_at, updated_at)
|
|
VALUES ('admin', 'admin@planet.local', :password, 'super_admin', true, NOW(), NOW())
|
|
"""),
|
|
{"password": ADMIN_PASSWORD_HASH},
|
|
)
|
|
await session.commit()
|
|
print(f"Admin user created: admin / admin123")
|
|
print(f"Hash: {ADMIN_PASSWORD_HASH}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(create_admin())
|