Django ORM Adapter for PyCasbin
Django ORM Adapter is the Django's ORM adapter for PyCasbin. With this library, Casbin can load policy from Django ORM supported database or save policy to it.
Based on Officially Supported Databases, The current supported databases are:
- PostgreSQL
- MariaDB
- MySQL
- Oracle
- SQLite
- IBM DB2
- Microsoft SQL Server
- Firebird
- ODBC
Installation
pip install casbin-django-orm-adapter
Add casbin_adapter.apps.CasbinAdapterConfig
to your INSTALLED_APPS
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
...
'casbin_adapter.apps.CasbinAdapterConfig',
...
]
CASBIN_MODEL = os.path.join(BASE_DIR, 'casbin.conf')
To run schema migration, execute python manage.py migrate casbin_adapter
Simple Example
from casbin_adapter.enforcer import enforcer
def hello(request):
sub = "alice"
obj = "data1"
act = "read"
if e.enforce(sub, obj, act):
pass
else:
pass
Configuration
CASBIN_MODEL
A string containing the file location of your casbin model.
CASBIN_ADAPTER
A string containing the adapter import path. Default to the django adapter shipped with this package: casbin_adapter.adapter.Adapter
CASBIN_ADAPTER_ARGS
A tuple of arguments to be passed into the constructor of the adapter specified
in CASBIN_ADAPTER
. Refer to adapters to see available arguments.
E.g. if you wish to use the file adapter
set the adapter to casbin.persist.adapters.FileAdapter
and use
CASBIN_ADAPTER_ARGS = ('path/to/policy_file.csv',)
CASBIN_DB_ALIAS
The database the adapter uses. Default to "default".
CASBIN_WATCHER
Watcher instance to be set as the watcher on the enforcer instance.
CASBIN_ROLE_MANAGER
Role manager instance to be set as the role manager on the enforcer instance.
Getting Help
License
This project is licensed under the Apache 2.0 license.