https://github.com/pycasbin/django-orm-adapter
Django ORM Adapter for PyCasbin
https://github.com/pycasbin/django-orm-adapter
access-control adapter authorization casbin django django-orm orm pycasbin storage-driver
Last synced: 11 months ago
JSON representation
Django ORM Adapter for PyCasbin
- Host: GitHub
- URL: https://github.com/pycasbin/django-orm-adapter
- Owner: pycasbin
- License: apache-2.0
- Created: 2020-01-27T06:46:14.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-28T14:46:44.000Z (over 1 year ago)
- Last Synced: 2025-04-09T19:16:57.693Z (11 months ago)
- Topics: access-control, adapter, authorization, casbin, django, django-orm, orm, pycasbin, storage-driver
- Language: Python
- Homepage: https://github.com/casbin/pycasbin
- Size: 60.5 KB
- Stars: 30
- Watchers: 2
- Forks: 19
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Django ORM Adapter for PyCasbin
[](https://github.com/pycasbin/django-orm-adapter/actions)
[](https://coveralls.io/github/pycasbin/django-orm-adapter)
[](https://pypi.org/project/casbin-django-orm-adapter/)
[](https://pypi.org/project/casbin-django-orm-adapter/)
[](https://pypi.org/project/casbin-django-orm-adapter/)
[](https://pypi.org/project/casbin-django-orm-adapter/)
[](https://discord.gg/S5UjpzGZjN)
Django ORM Adapter is the [Django](https://www.djangoproject.com/)'s [ORM](https://docs.djangoproject.com/en/3.0/ref/databases/) adapter for [PyCasbin](https://github.com/pycasbin/django-orm-adapter). With this library, Casbin can load policy from Django ORM supported database or save policy to it.
Based on [Officially Supported Databases](https://docs.djangoproject.com/en/3.0/ref/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`
```python
# settings.py
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
```python
# views.py
from casbin_adapter.enforcer import enforcer
def hello(request):
sub = "alice" # the user that wants to access a resource.
obj = "data1" # the resource that is going to be accessed.
act = "read" # the operation that the user performs on the resource.
if e.enforce(sub, obj, act):
# permit alice to read data1casbin_django_orm_adapter
pass
else:
# deny the request, show an error
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
- [PyCasbin](https://github.com/casbin/pycasbin)
### License
This project is licensed under the [Apache 2.0 license](LICENSE).