FJNR-inc / dry-rest-permissions

Rules based permissions for the Django Rest Framework
ISC License
75 stars 11 forks source link

[question] Where does one define the permission for the user? #9

Closed noobmaster19 closed 3 years ago

noobmaster19 commented 3 years ago

Hello , sorry if this is a noob question. I am considering using this package to implement a RBAC system for my django x react application. However , reading through the documentations , im pretty confused as to how one would specify the user's permissions. In the django's default permissions system , this would be defined within the django administrator itself. Does this package follow on the same idea?

RignonNoel commented 3 years ago

With this package you define the permissions from the models, so imagine for example that you maintain a blog project: you can create some custom permissions in your Article or Post models by adding some methods like:

class Post(models.Model):

[...]

    @authenticated_users
    def has_create_permission(request):
        # Replace the code here for your own conditions
       # In this example we will limit new Post to user that validated their emails before contributing to the Blog
        return request.user.has_validated_email()

You can based your condition on Django basic permissions or on a custom permissions system you maintain.

So do not hesitate to base your conditions on Django's Groups or Permissions

You will have the possibility to add some specific conditions like if the user have the right to edit a specific Article or Post based and if he's the author or not:

class Post(models.Model):

    author = models.ForeignKey(User, on_delete=models.CASCADE)

    @authenticated_users
    def has_object_edit_permission(self, request):
        return self.author.id == request.user.id
RignonNoel commented 3 years ago

@neowenshun If it's not clear or if you have some others question do not hesitate to ping me on this thread, i'll be happy to help you integrate this package and it allow us to understand which part of the doc should be clarify for futurs developers.

noobmaster19 commented 3 years ago

Hey ! i have created a new issue , i don't seem to be able to get the permissions to work for custom actions

RignonNoel commented 3 years ago

Perfect, we will help you on this new issue and take care to help you start with this package.

I close this ticket and let you follow on your new one.