feat: reset admin user from config on every app startup
This commit is contained in:
parent
05468edf9a
commit
89e060f9d2
@ -2,6 +2,8 @@ APP_ENV=development
|
|||||||
APP_PREFIX=
|
APP_PREFIX=
|
||||||
DATABASE_URL=sqlite:///./app.db
|
DATABASE_URL=sqlite:///./app.db
|
||||||
SECRET_KEY=changeme-replace-in-production
|
SECRET_KEY=changeme-replace-in-production
|
||||||
|
ADMIN_USERNAME=admin
|
||||||
|
ADMIN_PASSWORD=change_me
|
||||||
|
|
||||||
# Produktion (MariaDB + LDAP):
|
# Produktion (MariaDB + LDAP):
|
||||||
# APP_ENV=production
|
# APP_ENV=production
|
||||||
|
|||||||
@ -14,6 +14,8 @@ class Settings(BaseSettings):
|
|||||||
LDAP_SERVER: str = "gso1.ads1.fh-nuernberg.de"
|
LDAP_SERVER: str = "gso1.ads1.fh-nuernberg.de"
|
||||||
LDAP_DOMAIN: str = "ADS1"
|
LDAP_DOMAIN: str = "ADS1"
|
||||||
LDAP_SEARCH_BASE: str = "OU=users,OU=EFI,OU=Faculties,DC=ADS1,DC=fh-nuernberg,DC=de"
|
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
|
@lru_cache
|
||||||
|
|||||||
27
app/main.py
27
app/main.py
@ -1,3 +1,5 @@
|
|||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from fastapi import Depends, FastAPI, Request, WebSocket
|
from fastapi import Depends, FastAPI, Request, WebSocket
|
||||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
@ -10,9 +12,34 @@ from app.modules.auth.router import router as auth_router
|
|||||||
|
|
||||||
settings = get_settings()
|
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(
|
app = FastAPI(
|
||||||
title="University Process Hub",
|
title="University Process Hub",
|
||||||
root_path=settings.APP_PREFIX,
|
root_path=settings.APP_PREFIX,
|
||||||
|
lifespan=lifespan,
|
||||||
)
|
)
|
||||||
|
|
||||||
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user