2023-09-08 19:33:43 +00:00
|
|
|
import secrets
|
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
from fastapi import Depends, HTTPException, status
|
|
|
|
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
|
|
|
|
2023-09-08 19:53:35 +00:00
|
|
|
from ..core.config import Config, get_config
|
2023-09-08 19:33:43 +00:00
|
|
|
|
|
|
|
security = HTTPBasic()
|
|
|
|
|
|
|
|
|
|
|
|
async def user_is_admin(
|
|
|
|
credentials: HTTPBasicCredentials = Depends(security),
|
2023-09-08 19:53:35 +00:00
|
|
|
cfg: Config = Depends(get_config),
|
2023-09-08 19:33:43 +00:00
|
|
|
) -> bool:
|
2023-09-08 19:44:41 +00:00
|
|
|
"""
|
|
|
|
True iff der user "admin" ist
|
|
|
|
"""
|
2023-09-08 19:33:43 +00:00
|
|
|
|
2023-09-08 19:44:41 +00:00
|
|
|
username_correct = secrets.compare_digest(credentials.username, cfg.admin.name)
|
|
|
|
password_correct = secrets.compare_digest(credentials.password, cfg.admin.password)
|
2023-09-08 19:33:43 +00:00
|
|
|
|
|
|
|
return username_correct and password_correct
|
|
|
|
|
|
|
|
|
|
|
|
async def require_admin(
|
|
|
|
is_admin: bool = Depends(user_is_admin),
|
|
|
|
) -> None:
|
2023-09-08 19:44:41 +00:00
|
|
|
"""
|
|
|
|
HTTP 401 iff der user nicht "admin" ist
|
|
|
|
"""
|
|
|
|
|
2023-09-08 19:33:43 +00:00
|
|
|
if not is_admin:
|
|
|
|
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
|
|
|
|
|
|
|
|
|
2023-09-08 19:44:41 +00:00
|
|
|
async def user_visible_doors() -> int:
|
|
|
|
"""
|
|
|
|
Anzahl der user-sichtbaren Türchen
|
|
|
|
"""
|
|
|
|
|
2023-09-08 19:33:43 +00:00
|
|
|
today = date.today()
|
|
|
|
|
|
|
|
if today.month == 12:
|
|
|
|
return today.day
|
|
|
|
|
|
|
|
if today.month in (1, 2, 3):
|
|
|
|
return 24
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2023-09-12 13:50:02 +00:00
|
|
|
async def user_can_view_day(
|
2023-09-08 19:33:43 +00:00
|
|
|
day: int,
|
|
|
|
) -> bool:
|
2023-09-08 19:44:41 +00:00
|
|
|
"""
|
|
|
|
True iff das Türchen von Tag `day` user-sichtbar ist
|
|
|
|
"""
|
|
|
|
|
2023-09-12 16:48:55 +00:00
|
|
|
if day < 1:
|
2023-09-10 15:12:58 +00:00
|
|
|
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY)
|
|
|
|
|
2023-09-12 16:48:55 +00:00
|
|
|
return day <= await user_visible_doors()
|