-
Notifications
You must be signed in to change notification settings - Fork 18
Improve adapter into working django app #2
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
Conversation
|
Thought I'd also share a snippet on how I'm currently using it. There is definitely some room to improve the ergonomics. # authentication/logic.py
from casbin import Enforcer
from django.conf import settings
from casbin_adapter.adapter import Adapter
if settings.TESTING:
adapter = settings.BASE_DIR + '/authorization/tests/empty.csv'
else:
adapter = Adapter()
_enforcer = None
def enforcer():
global _enforcer
if _enforcer is None:
_enforcer = Enforcer(settings.BASE_DIR + '/authorization/casbin.conf', adapter, settings.DEBUG)
return _enforcer
def is_allowed(*, subject: str, resource: str, action: str) -> bool:
return enforcer().enforce(subject, resource, action) |
|
@techoner @nodece please review. |
|
|
|
Frankly speaking, I have never seen this type of issue. |
|
@divypatel9881 the Travis CI error will be discussed at: #3 |
|
The travis build issue is specific to this PR, as @gerbyzation has moved tests directory in the casbin_adapter directory. |
|
OK. @gerbyzation can you fix the CI error? https://travis-ci.org/github/pycasbin/django-orm-adapter/builds/682985313 |
|
@hsluoyz done |
|
lgtm. Thanks for you all! @gerbyzation @divypatel9881 |
|
🎉 This PR is included in version 1.0.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
in response to #1
I've done some work on the adapter to get it working with Django and restructured it a bit to be a valid django app. You can now add it to
INSTALLED_APPSlike any other django package.One issue is that the initialisation of the enforcer attempts to load all policies from database. The way django works this causes problems, as this would also execute before migrations can be applied, causing the code to query non-existing tables and crashing.
At the moment I'm avoiding this by using a singleton that initialises the enforcer on the first call. I haven't found anything like a lifecycle hook in django that could be used for this (
AppConfig.ready()should not have database queries as it still runs before management commands). I've just pinged off a message in the django IRC to see if there might be a better approach. Either way this should be documented or ideally a solution offered.