get_current_config dependable
This commit is contained in:
parent
d98d234cc1
commit
1f4a9994a6
3 changed files with 22 additions and 23 deletions
|
@ -49,18 +49,28 @@ class Responses:
|
|||
}
|
||||
|
||||
|
||||
async def get_current_config(
|
||||
current_config: Config | None = Depends(Config.load),
|
||||
) -> Config:
|
||||
"""
|
||||
Get the current configuration if it exists.
|
||||
"""
|
||||
|
||||
# fail if not configured
|
||||
if current_config is None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
return current_config
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
token: str = Depends(oauth2_scheme),
|
||||
current_config: Config | None = Depends(Config.load),
|
||||
current_config: Config = Depends(get_current_config),
|
||||
) -> User:
|
||||
"""
|
||||
Get the currently logged-in user if it exists.
|
||||
"""
|
||||
|
||||
# can't connect to an unconfigured database
|
||||
if current_config is None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
username = await current_config.jwt.decode_token(token)
|
||||
|
||||
# fail if not requested by a user
|
||||
|
@ -73,22 +83,18 @@ async def get_current_user(
|
|||
|
||||
async def get_user_by_name(
|
||||
user_name: str,
|
||||
current_config: Config | None = Depends(Config.load),
|
||||
_: Config = Depends(get_current_config),
|
||||
) -> User | None:
|
||||
"""
|
||||
Get a user by name.
|
||||
"""
|
||||
|
||||
# can't connect to an unconfigured database
|
||||
if current_config is None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
return User.get(user_name)
|
||||
|
||||
|
||||
async def get_device_by_id(
|
||||
device_id: int,
|
||||
current_config: Config | None = Depends(Config.load),
|
||||
current_config: Config = Depends(get_current_config),
|
||||
) -> Device | None:
|
||||
|
||||
# can't connect to an unconfigured database
|
||||
|
|
|
@ -7,7 +7,7 @@ from sqlmodel import select
|
|||
|
||||
from ..config import Config
|
||||
from ..db import Connection, TagValue, User, UserCreate
|
||||
from ._common import Responses, get_current_user
|
||||
from ._common import Responses, get_current_config, get_current_user
|
||||
|
||||
router = APIRouter(prefix="/admin", tags=["admin"])
|
||||
|
||||
|
@ -46,16 +46,12 @@ async def initial_configure(
|
|||
)
|
||||
async def create_initial_admin(
|
||||
admin_user: UserCreate,
|
||||
current_config: Config | None = Depends(Config.load),
|
||||
current_config: Config = Depends(get_current_config),
|
||||
):
|
||||
"""
|
||||
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)
|
||||
|
||||
# fail if any user exists
|
||||
with Connection.session as db:
|
||||
if db.exec(select(User).limit(1)).first() is not None:
|
||||
|
|
|
@ -8,7 +8,8 @@ from pydantic import BaseModel
|
|||
|
||||
from ..config import Config
|
||||
from ..db import TagValue, User, UserCreate, UserRead
|
||||
from ._common import Responses, get_current_user, get_user_by_name
|
||||
from ._common import (Responses, get_current_config, get_current_user,
|
||||
get_user_by_name)
|
||||
|
||||
router = APIRouter(prefix="/user", tags=["user"])
|
||||
|
||||
|
@ -25,16 +26,12 @@ class Token(BaseModel):
|
|||
@router.post("/authenticate", response_model=Token)
|
||||
async def login(
|
||||
form_data: OAuth2PasswordRequestForm = Depends(),
|
||||
current_config: Config | None = Depends(Config.load),
|
||||
current_config: Config = Depends(get_current_config),
|
||||
):
|
||||
"""
|
||||
POST ./authenticate: Authenticate a user. Issues a bearer token.
|
||||
"""
|
||||
|
||||
# fail if not installed
|
||||
if current_config is None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# try logging in
|
||||
if (user := User.authenticate(
|
||||
name=form_data.username,
|
||||
|
|
Loading…
Reference in a new issue