Skip to content

Conversation

@gerbyzation
Copy link
Contributor

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_APPS like 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.

@hsluoyz hsluoyz mentioned this pull request May 4, 2020
@gerbyzation
Copy link
Contributor Author

gerbyzation commented May 4, 2020

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)

@hsluoyz
Copy link
Member

hsluoyz commented May 5, 2020

@techoner @nodece please review.

@divy9881
Copy link

divy9881 commented May 5, 2020

3.7 and 3.8 python builds are failing due to the fact, the unittest doesn't allow abuse in PEP 420. And, I think, the Travis config is not consistent with your location of the test directory. Try bringing the test directory to the root level. I think this may solve this build issue.

@divy9881
Copy link

divy9881 commented May 5, 2020

Frankly speaking, I have never seen this type of issue.

@hsluoyz
Copy link
Member

hsluoyz commented May 5, 2020

@divypatel9881 the Travis CI error will be discussed at: #3

@divy9881
Copy link

divy9881 commented May 5, 2020

The travis build issue is specific to this PR, as @gerbyzation has moved tests directory in the casbin_adapter directory.

@hsluoyz
Copy link
Member

hsluoyz commented May 5, 2020

@gerbyzation
Copy link
Contributor Author

@hsluoyz done

@hsluoyz
Copy link
Member

hsluoyz commented May 5, 2020

lgtm. Thanks for you all! @gerbyzation @divypatel9881

@hsluoyz hsluoyz merged commit e361a22 into pycasbin:master May 5, 2020
@github-actions
Copy link

🎉 This PR is included in version 1.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants