django-rules icon indicating copy to clipboard operation
django-rules copied to clipboard

django.contrib.sites integration

Open parruc opened this issue 4 years ago • 5 comments

It would be really cool to have, in the predicates, an optional "site" parameter to easily integrate with the django.contrib.sites framework

parruc avatar Dec 08 '20 16:12 parruc

This wouldn't work outside of a request. I can see it would be useful though — maybe even better to just expose the request to predicates, if it's available?

benwhalley avatar Dec 08 '20 16:12 benwhalley

Passing the request sounds much more useful, but I wouldn't want to make it a required argument. The request could be passed as a context argument however. For plain rules, the caller can provide that directly, and for permissions it's trivial to write a custom backend that forwards it. I'd happily accept a PR.

dfunckt avatar Dec 08 '20 17:12 dfunckt

Getting the whole request would be fantastic and I think it is the way to go. I can try to watch into it if somebody guides me into where these modifications should be made: I use rules a lot but never looked into it deeply.

parruc avatar Dec 08 '20 17:12 parruc

I've looked into it a bit and looks like when some view checks a permission the following happens: 1- rules.contrib.views.PermissionRequiredMixin.has_permission is called. Here we have the request. we than call 2- django.contrib.auth.models.User.has_perms that calls in turn all the authentication backends passing only user, permission and object. Here we don't have the request anymore 3- rules.permissions.ObjectPermissionBackend forwards all received aguments to 4- rules.ruleset.test_rule that delegates the check (forwarding all params) to 5- rules.predicates.test that creates a context and returns the result. Is this analysis correct or there is something I'm missing?

What I should do, if I got it right, is: 1- add request=NO_VALUE to the rules.predicates.test signature and in predicate context so that in my predicates I can do self.context.get("request"). 2- add the request to the has_perm call in rules.contrib.views.PermissionRequiredMixin.has_permission 3- Somehow override the django.contrib.auth.models.User.has_perms method in order to forward the request, This is the biggest obstacle I see atm cause I dont see a way around it.

Any idea/thoughts/suggestion?

Thanks for the support

parruc avatar Dec 09 '20 00:12 parruc

Regarding the third point I tried to open a ticket on the django tracker:

https://code.djangoproject.com/ticket/32255

Feel free to join the discussion

parruc avatar Dec 10 '20 08:12 parruc