Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating to DRF's DjangoObjectPermission instead of AutoPermissionViewSetMixin #128

Open
hirokiky opened this issue Aug 25, 2020 · 1 comment

Comments

@hirokiky
Copy link

My suggestion is to use DRF's DjangoObjectPermission class instead of django-rules' AutoPermissionViewSetMixin.

DjangoRestFramework provides DjangoObjectPermission to check permission for objects on ViewSets.

Asis:

class ArticleViewSet(AutoPermissionViewSetMixin, ModelViewSet):
    ...

ToBe:

class ArticleViewSet(ModelViewSet):
    permission_classes = [DjangoObjectPermission]

For me, applying this permission class for ViewSets is better than injecting AutoPermissionViewSetMixin.

Because...

  • The mixin requires us inheriting them rather than using them, and it will cause deep-coupling
  • We don't need to maintain the mixin, because DRF provides the same thing

I didn't read all of lines of AutoPermissionViewSetMixin and DjangoObjectPermission, so now I can't guarantee that it's fully compatible.
But at least, we should refer about DRF's general way on rules' document.

@Routhinator
Copy link

DjangoObjectPermission does not work if you want Owner level permissions in read views, as mentioned in the very documentation you linked:

Note: If you need object level view permissions for GET, HEAD and OPTIONS requests and are using django-guardian for your object-level permissions backend, you'll want to consider using the DjangoObjectPermissionsFilter class provided by the djangorestframework-guardian package. It ensures that list endpoints only return results including objects for which the user has appropriate view permissions.

Though the note mentions django-guardian, it applies to this as well.

Django does not natively support Owner level permissions for read operations. As soon as you need that, you need AutoPermissionViewSetMixin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants