diff --git a/.env.example b/.env.example index ed1c0b4..a41fe9a 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,8 @@ APP_ENV=development APP_PREFIX= DATABASE_URL=sqlite:///./app.db SECRET_KEY=changeme-replace-in-production +ADMIN_USERNAME=admin +ADMIN_PASSWORD=change_me # Produktion (MariaDB + LDAP): # APP_ENV=production diff --git a/app/core/config.py b/app/core/config.py index f1bde1f..bea7503 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -14,6 +14,8 @@ class Settings(BaseSettings): LDAP_SERVER: str = "gso1.ads1.fh-nuernberg.de" LDAP_DOMAIN: str = "ADS1" LDAP_SEARCH_BASE: str = "OU=users,OU=EFI,OU=Faculties,DC=ADS1,DC=fh-nuernberg,DC=de" + ADMIN_USERNAME: str = "admin" + ADMIN_PASSWORD: str = "change_me" @lru_cache diff --git a/app/main.py b/app/main.py index 9d56335..d0984a3 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,5 @@ +from contextlib import asynccontextmanager + from fastapi import Depends, FastAPI, Request, WebSocket from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.staticfiles import StaticFiles @@ -10,9 +12,34 @@ from app.modules.auth.router import router as auth_router settings = get_settings() + +def _reset_admin() -> None: + from app.core.database import Base, SessionLocal, engine + from app.modules.auth.models import User # noqa: F401 — registers table + from app.modules.auth.service import get_user, hash_password + + Base.metadata.create_all(bind=engine) + with SessionLocal() as db: + user = get_user(db, settings.ADMIN_USERNAME) + if user is None: + user = User(username=settings.ADMIN_USERNAME, full_name="Administrator") + db.add(user) + user.pw_hash = hash_password(settings.ADMIN_PASSWORD) + user.is_admin = True + user.is_active = True + db.commit() + + +@asynccontextmanager +async def lifespan(app: FastAPI): + _reset_admin() + yield + + app = FastAPI( title="University Process Hub", root_path=settings.APP_PREFIX, + lifespan=lifespan, ) app.mount("/static", StaticFiles(directory="app/static"), name="static")