update account admin luôn tồn tại

This commit is contained in:
2026-04-02 11:04:40 +07:00
parent b35b201b77
commit 51c599e040
3 changed files with 38 additions and 0 deletions
+30
View File
@@ -1,15 +1,45 @@
from contextlib import asynccontextmanager
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .routers import admin, annotations, auth, backup, pdfs, ws
from .database import SessionLocal
from .models import User, UserRole, UserStatus
from .security import hash_password
def _seed_admin() -> None:
"""Create the default admin account if it does not exist yet."""
username = os.getenv("ADMIN_USERNAME", "admin")
password = os.getenv("ADMIN_PASSWORD", "Admin@12345")
email = os.getenv("ADMIN_EMAIL", "admin@lms.local")
db = SessionLocal()
try:
exists = db.query(User).filter(User.username == username).first()
if not exists:
db.add(User(
username = username,
email = email,
password_hash = hash_password(password),
role = UserRole.admin,
status = UserStatus.approved,
))
db.commit()
print(f"[seed] Admin account '{username}' created.")
else:
print(f"[seed] Admin account '{username}' already exists — skipped.")
finally:
db.close()
@asynccontextmanager
async def lifespan(app: FastAPI):
# Tables are managed by Alembic migrations.
# Run: alembic upgrade head (done by docker-compose entrypoint)
_seed_admin()
yield