2025-11-01 23:46:29 +08:00
|
|
|
"""
|
|
|
|
|
Initialize Default Admin User
|
|
|
|
|
|
2025-11-03 09:52:45 +00:00
|
|
|
Creates a default admin user on first application startup.
|
2025-11-01 23:46:29 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
# Add parent directory to path
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
|
|
from db.session import AsyncSessionLocal
|
2025-11-03 09:52:45 +00:00
|
|
|
from models.user import User, UserRole
|
|
|
|
|
from core.security import get_password_hash
|
2025-11-01 23:46:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create_default_admin():
|
2025-11-03 09:52:45 +00:00
|
|
|
"""Create default admin user if not exists"""
|
2025-11-01 23:46:29 +08:00
|
|
|
|
|
|
|
|
async with AsyncSessionLocal() as session:
|
|
|
|
|
try:
|
2025-11-03 09:52:45 +00:00
|
|
|
# Check if any admin user already exists
|
|
|
|
|
result = await session.execute(
|
|
|
|
|
select(User).where(User.role == UserRole.ADMIN)
|
|
|
|
|
)
|
|
|
|
|
existing_admin = result.scalar_one_or_none()
|
2025-11-01 23:46:29 +08:00
|
|
|
|
2025-11-03 09:52:45 +00:00
|
|
|
if existing_admin:
|
|
|
|
|
logger.info(f"✅ Admin user already exists: {existing_admin.username}")
|
|
|
|
|
return
|
2025-11-01 23:46:29 +08:00
|
|
|
|
2025-11-03 09:52:45 +00:00
|
|
|
# Create default admin user with fixed credentials
|
|
|
|
|
admin_password = "Admin123!" # Default password for development
|
|
|
|
|
password_hash = get_password_hash(admin_password)
|
2025-11-01 23:46:29 +08:00
|
|
|
|
2025-11-03 09:52:45 +00:00
|
|
|
admin_user = User(
|
|
|
|
|
email="admin@xcodereview.com",
|
|
|
|
|
username="admin",
|
|
|
|
|
password_hash=password_hash,
|
|
|
|
|
full_name="Administrator",
|
|
|
|
|
role=UserRole.ADMIN,
|
|
|
|
|
is_active=True,
|
|
|
|
|
is_verified=True,
|
|
|
|
|
)
|
2025-11-01 23:46:29 +08:00
|
|
|
|
2025-11-03 09:52:45 +00:00
|
|
|
session.add(admin_user)
|
|
|
|
|
await session.commit()
|
2025-11-01 23:46:29 +08:00
|
|
|
|
2025-11-03 09:52:45 +00:00
|
|
|
# Log the credentials
|
2025-11-01 23:46:29 +08:00
|
|
|
logger.info("=" * 80)
|
2025-11-03 09:52:45 +00:00
|
|
|
logger.info("✅ DEFAULT ADMIN USER CREATED")
|
2025-11-01 23:46:29 +08:00
|
|
|
logger.info("=" * 80)
|
2025-11-03 09:52:45 +00:00
|
|
|
logger.info(f"📧 Email: admin@xcodereview.com")
|
|
|
|
|
logger.info(f"👤 Username: admin")
|
|
|
|
|
logger.info(f"🔑 Password: {admin_password}")
|
2025-11-01 23:46:29 +08:00
|
|
|
logger.info("=" * 80)
|
2025-11-03 09:52:45 +00:00
|
|
|
logger.warning("⚠️ IMPORTANT: Change this password in production!")
|
2025-11-01 23:46:29 +08:00
|
|
|
logger.info("=" * 80)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-11-03 09:52:45 +00:00
|
|
|
logger.error(f"❌ Error creating admin user: {e}")
|
2025-11-01 23:46:29 +08:00
|
|
|
await session.rollback()
|
2025-11-03 09:52:45 +00:00
|
|
|
# Don't raise - allow app to continue even if admin creation fails
|
2025-11-01 23:46:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
"""Main entry point"""
|
|
|
|
|
logger.info("Initializing default admin user...")
|
|
|
|
|
asyncio.run(create_default_admin())
|
|
|
|
|
logger.info("Admin user initialization complete")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|