how to override list permission with django-rules

381 views Asked by At

I installed django-rules to my project to define rules for my actions. The list function hast no permission setting per default so you have to add them to the permission_type_map as written here but without an effect. For the other actions i am able to change the behavior if i set it to is_superuser or something else.

from django.db import models

import rules
from rules.contrib.models import RulesModel
from rules.contrib.rest_framework import AutoPermissionViewSetMixin

from base.models import BaseModel


class Company(RulesModel, BaseModel):
    name = models.CharField(max_length=100)
    active = models.BooleanField(default=True)

    permission_type_map = {
        **AutoPermissionViewSetMixin.permission_type_map,
        "list": "all",
    }

    class Meta:
        rules_permissions = {
            # TODO: rules need to be defined
            "add": rules.always_allow,
            "view": rules.always_allow,
            "delete": rules.always_allow,
            "change": rules.always_allow,
            "all": rules.is_superuser
        }

What do i miss here?

1

There are 1 answers

0
Ben Honda On

I'm new to django-rules, but from what I can see in the docs you can only extend/change permission_type_map when using or subclassing AutoPermissionViewSetMixin. Try defining the permission_type_map dict in your ViewSet class instead:

from rest_framework import viewsets
from rules.contrib.rest_framework import AutoPermissionViewSetMixin

class MyViewSet(AutoPermissionViewSetMixin, viewsets.ModelViewSet):

    permission_type_map = {
        **AutoPermissionViewSetMixin.permission_type_map,
        "list": "all",
    }