dfunckt / django-rules

Awesome Django authorization, without the database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with CBV and Rules

opened this issue · comments

Hi, I am running into a problem using rules with CBV. I initialized rules like this:
rules.add_perm('core.change_user', is_myuser)

Where:

    @rules.predicate
    def is_myuser(user):
        print('Checking inside from rules')
        return False

And I am calling:

    class UserUpdateView(PermissionRequiredMixin, UpdateView):
        model = User
        form_class = UserForm
        template_name = 'forms/user_form.html'
        permission_required = 'core.change_user'

However this is not working. I have checked and I correctly have:

AUTHENTICATION_BACKENDS = [
    'rules.permissions.ObjectPermissionBackend',
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
]

I am not able to figure out the problem here. Even the debug message inside my predicate "is_myuser" doesn't print

I also tried changing the permission name and using add_rule().
I am using Django==2.0.8 and rules==2.0

Other pieces of Code:

Initialize Permissions in AppConfig:

class CoreConfig(AppConfig):
    name = 'app.core'
    verbose_name = 'My App Config'
def ready(self):
    from .rules import init_rules
    import logging
    logger = logging.getLogger(__name__)
    logger.info('Initializing permissions')
    rules.add_perm('core.change_user', is_myuser)

I am getting 'Initializing permissions' in logs.

When I check for permission in get_context_data of CBV
print(f"User has permission: {self.request.user.has_perm(self.permission_required, self.get_object())}")

Output: User has permission: True

I assume this is something you managed to resolve, so I'm closing, though I do wonder what the issue was if you could post back.