brevity
This commit is contained in:
parent
c5e388aed4
commit
bf8cb86cee
4 changed files with 24 additions and 20 deletions
|
@ -4,7 +4,8 @@ import uvicorn
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
from .config import Config, Settings
|
from .config import Config, Settings
|
||||||
from .db import Connection, schemas
|
from .db import Connection
|
||||||
|
from .db.schemas import User
|
||||||
from .routers import admin, user
|
from .routers import admin, user
|
||||||
|
|
||||||
settings = Settings.get()
|
settings = Settings.get()
|
||||||
|
@ -40,8 +41,8 @@ async def on_startup() -> None:
|
||||||
|
|
||||||
# some testing
|
# some testing
|
||||||
with Connection.use() as db:
|
with Connection.use() as db:
|
||||||
print(schemas.User.from_db(db, "admin"))
|
print(User.from_db(db, "admin"))
|
||||||
print(schemas.User.from_db(db, "nonexistent"))
|
print(User.from_db(db, "nonexistent"))
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
|
|
@ -3,7 +3,8 @@ from fastapi.security import OAuth2PasswordBearer
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from ..config import Config
|
from ..config import Config
|
||||||
from ..db import Connection, schemas
|
from ..db import Connection
|
||||||
|
from ..db.schemas import User
|
||||||
|
|
||||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="user/auth")
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="user/auth")
|
||||||
|
|
||||||
|
@ -44,6 +45,6 @@ async def get_current_user(
|
||||||
return None
|
return None
|
||||||
|
|
||||||
username = await current_config.jwt.decode_token(token)
|
username = await current_config.jwt.decode_token(token)
|
||||||
user = schemas.User.from_db(db, username)
|
user = User.from_db(db, username)
|
||||||
|
|
||||||
return user
|
return user
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
|
||||||
from ..config import Config
|
from ..config import Config
|
||||||
from ..db import Connection, schemas
|
from ..db import Connection
|
||||||
|
from ..db.schemas import User, UserCapability, UserCreate
|
||||||
from . import _deps
|
from . import _deps
|
||||||
|
|
||||||
router = APIRouter(prefix="/admin")
|
router = APIRouter(prefix="/admin")
|
||||||
|
@ -16,7 +17,7 @@ router = APIRouter(prefix="/admin")
|
||||||
)
|
)
|
||||||
async def install(
|
async def install(
|
||||||
config: Config,
|
config: Config,
|
||||||
admin_user: schemas.UserCreate,
|
admin_user: UserCreate,
|
||||||
current_config: Config | None = Depends(Config.load),
|
current_config: Config | None = Depends(Config.load),
|
||||||
):
|
):
|
||||||
if current_config is not None:
|
if current_config is not None:
|
||||||
|
@ -26,7 +27,7 @@ async def install(
|
||||||
Connection.connect(await config.db.db_engine)
|
Connection.connect(await config.db.db_engine)
|
||||||
|
|
||||||
with Connection.use() as db:
|
with Connection.use() as db:
|
||||||
admin_user = schemas.User.create(
|
admin_user = User.create(
|
||||||
db=db,
|
db=db,
|
||||||
user=admin_user,
|
user=admin_user,
|
||||||
crypt_context=await config.crypto.crypt_context,
|
crypt_context=await config.crypto.crypt_context,
|
||||||
|
@ -34,7 +35,7 @@ async def install(
|
||||||
|
|
||||||
admin_user.add_capabilities(
|
admin_user.add_capabilities(
|
||||||
db=db,
|
db=db,
|
||||||
capabilities=[schemas.UserCapability.admin],
|
capabilities=[UserCapability.admin],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,13 +51,13 @@ async def install(
|
||||||
async def set_config(
|
async def set_config(
|
||||||
new_config: Config,
|
new_config: Config,
|
||||||
current_config: Config | None = Depends(Config.load),
|
current_config: Config | None = Depends(Config.load),
|
||||||
current_user: schemas.User | None = Depends(_deps.get_current_user),
|
current_user: User | None = Depends(_deps.get_current_user),
|
||||||
):
|
):
|
||||||
if current_config is None:
|
if current_config is None:
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
if (current_user is None
|
if (current_user is None
|
||||||
or schemas.UserCapability.admin not in current_user.capabilities):
|
or UserCapability.admin not in current_user.capabilities):
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
await new_config.save()
|
await new_config.save()
|
||||||
|
|
|
@ -4,7 +4,8 @@ from pydantic import BaseModel
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from ..config import Config
|
from ..config import Config
|
||||||
from ..db import Connection, schemas
|
from ..db import Connection
|
||||||
|
from ..db.schemas import User, UserCapability, UserCreate
|
||||||
from . import _deps
|
from . import _deps
|
||||||
|
|
||||||
router = APIRouter(prefix="/user")
|
router = APIRouter(prefix="/user")
|
||||||
|
@ -24,7 +25,7 @@ async def login(
|
||||||
if current_config is None:
|
if current_config is None:
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
user = schemas.User.login(
|
user = User.login(
|
||||||
db=db,
|
db=db,
|
||||||
name=form_data.username,
|
name=form_data.username,
|
||||||
password=form_data.password,
|
password=form_data.password,
|
||||||
|
@ -42,9 +43,9 @@ async def login(
|
||||||
return {"access_token": access_token, "token_type": "bearer"}
|
return {"access_token": access_token, "token_type": "bearer"}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/current", response_model=schemas.User)
|
@router.get("/current", response_model=User)
|
||||||
async def get_current_user(
|
async def get_current_user(
|
||||||
current_user: schemas.User | None = Depends(_deps.get_current_user),
|
current_user: User | None = Depends(_deps.get_current_user),
|
||||||
):
|
):
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
@ -58,22 +59,22 @@ async def get_current_user(
|
||||||
status.HTTP_403_FORBIDDEN: _deps.Responses.needs_admin,
|
status.HTTP_403_FORBIDDEN: _deps.Responses.needs_admin,
|
||||||
status.HTTP_409_CONFLICT: _deps.Responses.entry_exists,
|
status.HTTP_409_CONFLICT: _deps.Responses.entry_exists,
|
||||||
},
|
},
|
||||||
response_model=schemas.User,
|
response_model=User,
|
||||||
)
|
)
|
||||||
async def add_user(
|
async def add_user(
|
||||||
user: schemas.UserCreate,
|
user: UserCreate,
|
||||||
current_config: Config | None = Depends(Config.load),
|
current_config: Config | None = Depends(Config.load),
|
||||||
current_user: schemas.User | None = Depends(_deps.get_current_user),
|
current_user: User | None = Depends(_deps.get_current_user),
|
||||||
db: Session | None = Depends(Connection.get),
|
db: Session | None = Depends(Connection.get),
|
||||||
):
|
):
|
||||||
if current_config is None:
|
if current_config is None:
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
if (current_user is None
|
if (current_user is None
|
||||||
or schemas.UserCapability.admin not in current_user.capabilities):
|
or UserCapability.admin not in current_user.capabilities):
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
new_user = schemas.User.create(
|
new_user = User.create(
|
||||||
db=db,
|
db=db,
|
||||||
user=user,
|
user=user,
|
||||||
crypt_context=await current_config.crypto.crypt_context,
|
crypt_context=await current_config.crypto.crypt_context,
|
||||||
|
|
Loading…
Reference in a new issue