Skip to content

User object permission #2271

@scarm01

Description

@scarm01

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions