Registrace, kontrola prihlaseni uzivatele ve sprave letadel

This commit is contained in:
Michal Kváček
2023-08-15 10:31:06 +02:00
parent 97295c17c0
commit 33e0a35f4b
5 changed files with 24 additions and 16 deletions
+6 -6
View File
@@ -1,13 +1,13 @@
from fastapi_jwt.jwt import JwtAccess, JwtRefresh from fastapi_jwt.jwt import JwtAccess, JwtRefresh
class BaseEndpoint: class BaseEndpoint:
def __init__(self, *args, **kwargs): def __init__(self, db):
self.db = kwargs.get("db") self.db = db
super().__init__(*args, **kwargs)
class AuthEndpoint: class AuthEndpoint:
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.access_security: JwtAccess = kwargs.get("access_token") self.access_security: JwtAccess = kwargs.pop("access_token")
self.refresh_security: JwtRefresh = kwargs.get("refresh_token") self.refresh_security: JwtRefresh = kwargs.pop("refresh_token")
super().__init__() super().__init__(*args, **kwargs)
+1 -2
View File
@@ -1,6 +1,5 @@
from fastapi import HTTPException from fastapi import HTTPException
from fastapi_jwt import JwtAuthorizationCredentials from fastapi_jwt import JwtAuthorizationCredentials
from passlib.hash import bcrypt from passlib.hash import bcrypt
from sqlalchemy import select from sqlalchemy import select
from starlette.responses import Response from starlette.responses import Response
@@ -14,7 +13,7 @@ class LoginInput(BaseModel):
password: str password: str
class LoginEndpoint(BaseEndpoint, AuthEndpoint): class LoginEndpoint(AuthEndpoint, BaseEndpoint):
async def on_post(self, user_data: LoginInput, resp: Response) -> dict: async def on_post(self, user_data: LoginInput, resp: Response) -> dict:
query = select(User).filter_by(email=user_data.email) query = select(User).filter_by(email=user_data.email)
logged_user = (await self.db.scalars(query)).first() logged_user = (await self.db.scalars(query)).first()
+6 -6
View File
@@ -32,13 +32,13 @@ class RegistrationEndpoint(BaseEndpoint):
if existing_user: if existing_user:
raise HTTPException(status_code=422, detail="User already exists") raise HTTPException(status_code=422, detail="User already exists")
model = User( model = await User.create(self.db, {
name=user_data.name, "name": user_data.name,
email=user_data.email, "email": user_data.email,
password_hashed=bcrypt.hash(user_data.password) "password_hashed": bcrypt.hash(user_data.password),
) "description": ""
})
self.db.add(model)
await self.db.commit() await self.db.commit()
return model.as_dict() return model.as_dict()
+6
View File
@@ -3,6 +3,7 @@ import strawberry
from strawberry.file_uploads import Upload from strawberry.file_uploads import Upload
from sqlalchemy import select from sqlalchemy import select
from database import models from database import models
from decorators.endpoints import authenticated_user_only
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type, strawberry_sqlalchemy_input from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type, strawberry_sqlalchemy_input
from upload_utils import handle_file_upload, delete_file, get_public_url from upload_utils import handle_file_upload, delete_file, get_public_url
from ..dataloaders.flight import flights_by_aircraft_dataloader from ..dataloaders.flight import flights_by_aircraft_dataloader
@@ -37,6 +38,7 @@ def get_base_query(user_id: int):
class AircraftQueries: class AircraftQueries:
@strawberry.field @strawberry.field
@authenticated_user_only
async def aircrafts(root, info) -> List[Aircraft]: async def aircrafts(root, info) -> List[Aircraft]:
query = ( query = (
get_base_query(info.context.user_id) get_base_query(info.context.user_id)
@@ -46,6 +48,7 @@ class AircraftQueries:
return (await info.context.db.scalars(query)).all() return (await info.context.db.scalars(query)).all()
@strawberry.field @strawberry.field
@authenticated_user_only
async def aircraft(root, info, id: int) -> Aircraft: async def aircraft(root, info, id: int) -> Aircraft:
query = ( query = (
get_base_query(info.context.user_id) get_base_query(info.context.user_id)
@@ -61,6 +64,7 @@ class CreateAircraftMutation:
photo: Optional[Upload] photo: Optional[Upload]
@strawberry.mutation @strawberry.mutation
@authenticated_user_only
async def create_aircraft(root, info, input: CreateAircraftInput) -> Aircraft: async def create_aircraft(root, info, input: CreateAircraftInput) -> Aircraft:
# TODO: kontrola organizace # TODO: kontrola organizace
@@ -84,6 +88,7 @@ class EditAircraftMutation:
photo: Optional[Upload] photo: Optional[Upload]
@strawberry.mutation @strawberry.mutation
@authenticated_user_only
async def edit_aircraft(root, info, id: int, input: EditAircraftInput) -> Aircraft: async def edit_aircraft(root, info, id: int, input: EditAircraftInput) -> Aircraft:
# TODO: kontrola organizace # TODO: kontrola organizace
# TODO: kontrola opravneni na akci # TODO: kontrola opravneni na akci
@@ -103,6 +108,7 @@ class EditAircraftMutation:
class DeleteAircraftMutation: class DeleteAircraftMutation:
@strawberry.mutation @strawberry.mutation
@authenticated_user_only
async def delete_aircraft(self, info, id: int) -> Aircraft: async def delete_aircraft(self, info, id: int) -> Aircraft:
# TODO: kontrola opravneni na akci # TODO: kontrola opravneni na akci
+5 -2
View File
@@ -110,11 +110,14 @@ class App:
@self.api_router.post("/logout") @self.api_router.post("/logout")
async def login(resp: Response): async def login(resp: Response):
return await LogoutEndpoint(access_token=self.access_security, refresh_token=self.refresh_security).on_post(resp) return await LogoutEndpoint(
access_token=self.access_security,
refresh_token=self.refresh_security
).on_post(resp)
@self.api_router.post("/registration", status_code=201) @self.api_router.post("/registration", status_code=201)
async def registration(user: RegistrationInput, db: AsyncSession = Depends(db_session)): async def registration(user: RegistrationInput, db: AsyncSession = Depends(db_session)):
return await RegistrationEndpoint(db).on_post(user) return await RegistrationEndpoint(db=db).on_post(user)
# musi byt na konci # musi byt na konci
app.include_router(self.api_router) app.include_router(self.api_router)