repaired admin routes
This commit is contained in:
parent
12432286bf
commit
ae8894f5cc
2 changed files with 37 additions and 13 deletions
|
@ -44,7 +44,7 @@ async def on_startup() -> None:
|
||||||
# check if configured
|
# check if configured
|
||||||
if (current_config := await Config.load()) is not None:
|
if (current_config := await Config.load()) is not None:
|
||||||
# connect to database
|
# connect to database
|
||||||
Connection.connect("sqlite:///tmp/v2.db")
|
Connection.connect("sqlite:///tmp/vpn.db")
|
||||||
|
|
||||||
# # some testing
|
# # some testing
|
||||||
# with Connection.use() as db:
|
# with Connection.use() as db:
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlmodel import select
|
||||||
|
|
||||||
from ..config import Config
|
from ..config import Config
|
||||||
from ..db import Capability, Connection, User, UserCreate
|
from ..db import Capability, Connection, User, UserCreate
|
||||||
|
@ -13,34 +14,57 @@ router = APIRouter(prefix="/admin", tags=["admin"])
|
||||||
|
|
||||||
|
|
||||||
@router.put(
|
@router.put(
|
||||||
"/install",
|
"/install/config",
|
||||||
responses={
|
responses={
|
||||||
status.HTTP_200_OK: Responses.OK,
|
status.HTTP_200_OK: Responses.OK,
|
||||||
status.HTTP_400_BAD_REQUEST: Responses.INSTALLED,
|
status.HTTP_400_BAD_REQUEST: Responses.INSTALLED,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
async def install(
|
async def initial_configure(
|
||||||
config: Config,
|
config: Config,
|
||||||
# admin_user: UserCreate,
|
|
||||||
current_config: Config | None = Depends(Config.load),
|
current_config: Config | None = Depends(Config.load),
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
PUT ./install: Install `kiwi-vpn`.
|
PUT ./install/config: Configure `kiwi-vpn`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# fail if already installed
|
# fail if already configured
|
||||||
if current_config is not None:
|
if current_config is not None:
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# create config file, connect to database
|
# create config file, connect to database
|
||||||
await config.save()
|
await config.save()
|
||||||
Connection.connect("sqlite:///tmp/v2.db")
|
Connection.connect("sqlite:///tmp/vpn.db")
|
||||||
|
|
||||||
# # create an administrative user
|
|
||||||
# new_user = User.create(**admin_user)
|
@router.put(
|
||||||
# assert new_user is not None
|
"/install/admin",
|
||||||
# new_user.set_capabilities([Capability.login, Capability.admin])
|
responses={
|
||||||
# new_user.update()
|
status.HTTP_200_OK: Responses.OK,
|
||||||
|
status.HTTP_400_BAD_REQUEST: Responses.NOT_INSTALLED,
|
||||||
|
status.HTTP_409_CONFLICT: Responses.ENTRY_EXISTS,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
async def create_initial_admin(
|
||||||
|
admin_user: UserCreate,
|
||||||
|
current_config: Config | None = Depends(Config.load),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
PUT ./install/admin: Create the first administrative user.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# fail if not configured
|
||||||
|
if current_config is None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
with Connection.session as db:
|
||||||
|
if db.exec(select(User)).first() is not None:
|
||||||
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
||||||
|
|
||||||
|
# create an administrative user
|
||||||
|
new_user = User.create(**admin_user.dict())
|
||||||
|
new_user.set_capabilities([Capability.login, Capability.admin])
|
||||||
|
new_user.update()
|
||||||
|
|
||||||
|
|
||||||
@router.put(
|
@router.put(
|
||||||
|
@ -67,4 +91,4 @@ async def set_config(
|
||||||
|
|
||||||
# update config file, reconnect to database
|
# update config file, reconnect to database
|
||||||
await new_config.save()
|
await new_config.save()
|
||||||
Connection.connect("sqlite:///tmp/v2.db")
|
Connection.connect("sqlite:///tmp/vpn.db")
|
||||||
|
|
Loading…
Reference in a new issue