video-accessibility/backend/app/core/seed.py
Vadym Samoilenko cf761c4bb6 feat: add linguist role and user management navigation
- Add LINGUIST role to UserRole enum (backend + frontend)
- Grant linguists access to QC Review, Final Review, review notes, and VTT editing
- Add MongoDB migration to update schema validator with linguist role
- Add admin seed: vadymsamoilenko@oliver.agency is promoted to admin on startup
- Add User Management sidebar link for admin users
- Fix Login.tsx role type cast to use UserRole instead of hardcoded union

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-16 11:46:33 +01:00

51 lines
1.7 KiB
Python

"""Seed utilities for initial data setup."""
import os
import re
from datetime import datetime
from bson import ObjectId
from .security import get_password_hash
DEFAULT_ADMIN_EMAIL = "vadymsamoilenko@oliver.agency"
async def seed_default_admin(db) -> None:
"""Ensure the default admin user exists and has the admin role.
Looks up vadymsamoilenko@oliver.agency (case-insensitive).
- If found with a non-admin role: promotes to admin.
- If not found: creates a local-auth admin account.
Password is read from DEFAULT_ADMIN_PASSWORD env var (fallback: ChangeMe123!).
"""
email_pattern = re.compile(f"^{re.escape(DEFAULT_ADMIN_EMAIL)}$", re.IGNORECASE)
existing = await db.users.find_one({"email": email_pattern})
if existing:
if existing.get("role") != "admin":
await db.users.update_one(
{"_id": existing["_id"]},
{"$set": {"role": "admin", "updated_at": datetime.utcnow()}},
)
print(f"✅ Promoted {DEFAULT_ADMIN_EMAIL} to admin role")
else:
print(f"✅ Default admin {DEFAULT_ADMIN_EMAIL} already exists")
return
password = os.environ.get("DEFAULT_ADMIN_PASSWORD", "ChangeMe123!")
user_doc = {
"_id": str(ObjectId()),
"email": DEFAULT_ADMIN_EMAIL,
"hashed_password": get_password_hash(password),
"full_name": "Vadym Samoilenko",
"role": "admin",
"auth_provider": "local",
"is_active": True,
"created_at": datetime.utcnow(),
"updated_at": datetime.utcnow(),
}
await db.users.insert_one(user_doc)
print(f"✅ Created default admin: {DEFAULT_ADMIN_EMAIL}")