-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Closed
Labels
Description
I need to implement object permission in my application. I can handle this in each endpoint but it will lead to a lot of duplicated code.
For example:
@router.get(...)
def read_item(..., item_id: int, ...):
item = service.get_item(id=item_id)
if item.owner_id != current_user.id:
raise NotOwnerException
...
This is a simple scenario.
Is there a way to handle object permission without having to repeat code, maybe using dependencies injection?
I will need in some way to pass item_id to the dependency but I think that it's not possibile.
Has anyone found a viable solution?
I could also create a function like this:
def check_permission(item: Item, current_user: User)
...
and call this function in each endpoint, but it doesn't seem a very elegant solution.
What do you think?