2022-03-18 18:22:17 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
|
2022-03-18 22:43:02 +00:00
|
|
|
from ..config import Config
|
2022-03-18 23:45:09 +00:00
|
|
|
from ..db import Connection, schemas
|
2022-03-19 04:07:19 +00:00
|
|
|
from . import _deps
|
2022-03-18 18:22:17 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/admin")
|
|
|
|
|
|
|
|
|
|
|
|
@router.put(
|
2022-03-19 17:11:52 +00:00
|
|
|
"/install",
|
2022-03-18 18:22:17 +00:00
|
|
|
responses={
|
2022-03-19 17:11:52 +00:00
|
|
|
status.HTTP_200_OK: _deps.Responses.ok,
|
|
|
|
status.HTTP_400_BAD_REQUEST: _deps.Responses.installed,
|
2022-03-18 18:22:17 +00:00
|
|
|
},
|
|
|
|
)
|
2022-03-19 17:11:52 +00:00
|
|
|
async def install(
|
|
|
|
config: Config,
|
|
|
|
user: schemas.UserCreate,
|
2022-03-19 02:38:32 +00:00
|
|
|
current_config: Config | None = Depends(Config.load),
|
2022-03-18 18:22:17 +00:00
|
|
|
):
|
|
|
|
if current_config is not None:
|
2022-03-19 17:11:52 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
2022-03-18 18:22:17 +00:00
|
|
|
|
2022-03-19 17:11:52 +00:00
|
|
|
await config.save()
|
|
|
|
Connection.connect(await config.db.db_engine)
|
2022-03-18 18:22:17 +00:00
|
|
|
|
2022-03-19 17:11:52 +00:00
|
|
|
async for db in Connection.get():
|
|
|
|
user.capabilities.append("admin")
|
2022-03-18 18:22:17 +00:00
|
|
|
|
2022-03-19 17:11:52 +00:00
|
|
|
schemas.User.create(
|
|
|
|
db=db,
|
|
|
|
user=user,
|
|
|
|
crypt_context=await config.crypto.crypt_context,
|
|
|
|
)
|
2022-03-18 18:22:17 +00:00
|
|
|
|
2022-03-19 17:11:52 +00:00
|
|
|
|
|
|
|
@router.put(
|
|
|
|
"/config",
|
2022-03-18 18:22:17 +00:00
|
|
|
responses={
|
2022-03-19 17:11:52 +00:00
|
|
|
status.HTTP_200_OK: _deps.Responses.ok,
|
|
|
|
status.HTTP_400_BAD_REQUEST: _deps.Responses.not_installed,
|
|
|
|
status.HTTP_401_UNAUTHORIZED: _deps.Responses.needs_user,
|
|
|
|
status.HTTP_403_FORBIDDEN: _deps.Responses.needs_admin,
|
2022-03-18 18:22:17 +00:00
|
|
|
},
|
|
|
|
)
|
2022-03-19 17:11:52 +00:00
|
|
|
async def set_config(
|
|
|
|
new_config: Config,
|
2022-03-19 02:38:32 +00:00
|
|
|
current_config: Config | None = Depends(Config.load),
|
2022-03-19 17:11:52 +00:00
|
|
|
current_user: schemas.User | None = Depends(_deps.get_current_user),
|
2022-03-18 18:22:17 +00:00
|
|
|
):
|
|
|
|
if current_config is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
2022-03-19 17:11:52 +00:00
|
|
|
if current_user is None or "admin" not in current_user.capabilities:
|
|
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
2022-03-19 03:31:41 +00:00
|
|
|
|
2022-03-19 17:11:52 +00:00
|
|
|
await new_config.save()
|
|
|
|
Connection.connect(await new_config.db.db_engine)
|