drf- prbac permissions checking #7848
Unanswered
aryaniyaps
asked this question in
Question & Answer
Replies: 1 comment 1 reply
-
@tomchristie can prbac permissions-checking be documented? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Relevant S.O. post is here.
So I am building an api with architecture inspired by discord. I have objects called
Boxes
and these boxes have their own set ofRoles
.The role object looks like this.
I am using the bitfield extension provided by disqus here.
Roles have a set of predefined permissions which can be allowed or denied for a specific role, like
Now, users can be a part of the box, through a model called member, and they are assigned roles. The final set of permissions the user has is determined by a method.
Now let's say that I have a view.
I want to make sure that only users who have the MANAGE_ROLES permission (determined by the get_permissions method) can use this view.
I know that I have to make a custom permission, but have found integrating the api for my use case very hard.
Furthermore, I want to only check for the permission if the request's method is POST, because the user is creating something here.
Can someone please help get me started?
Here is what I've come up with so far.
First, we need to get the associated box object for every view.
For example, in this view here,
we can get the box object by referencing the box id. But the way we get the box
differs for each view. Some views may not have the
box_id
in the url itself.So each view has a property called box as follows.
Then here is the abstract base permission class which checks if the member has the required permission.
Now I can subclass this BasePermissionClass as follows.
And also, in cases where additional checking is required (members cannot give or take away roles from owners of the box)
we can do this.
Finally, adding to the class the required permissions,
What I wanted to ask was, Is this best practice, or can the way I am checking permissions be improved?
I came up with this on my own, so I don't know if I am right or wrong. Should the
box
attribute be present on every view,or is there any alternative to it?
Please let me know!
Thanks a lot!
Beta Was this translation helpful? Give feedback.
All reactions