69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
from secrets import token_hex
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from .. import config
|
|
|
|
from ..db import crud, schemas, connection
|
|
|
|
router = APIRouter(prefix="/admin")
|
|
|
|
|
|
@router.put(
|
|
"/config",
|
|
responses={
|
|
status.HTTP_200_OK: {
|
|
"content": None,
|
|
},
|
|
status.HTTP_403_FORBIDDEN: {
|
|
"description": "Must be admin",
|
|
"content": None,
|
|
},
|
|
},
|
|
)
|
|
async def set_config(
|
|
new_config: config.BaseConfig,
|
|
current_config: config.BaseConfig | None = Depends(config.get),
|
|
):
|
|
if current_config is not None:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
if new_config.jwt.secret is None:
|
|
new_config.jwt.secret = token_hex(32)
|
|
|
|
connection.reconnect(new_config.db_engine)
|
|
|
|
config.set(new_config)
|
|
|
|
|
|
@router.post(
|
|
"/user",
|
|
responses={
|
|
status.HTTP_200_OK: {
|
|
"content": None,
|
|
},
|
|
status.HTTP_400_BAD_REQUEST: {
|
|
"description": "Database doesn't exist",
|
|
"content": None,
|
|
},
|
|
},
|
|
)
|
|
async def add_user(
|
|
user_name: str,
|
|
user_password: str,
|
|
current_config: config.BaseConfig | None = Depends(config.get),
|
|
db: Session | None = Depends(connection.get),
|
|
):
|
|
if current_config is None:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
|
|
|
crud.create_user(
|
|
db=db,
|
|
user=schemas.UserCreate(
|
|
name=user_name,
|
|
password=user_password,
|
|
),
|
|
crypt_context=current_config.crypt_context,
|
|
)
|
|
crud.add_user_capability(db, user_name=user_name, capability="admin")
|