Skip to main content

Django's ORM adapter for PyCasbin

Project description

Django ORM Adapter for PyCasbin

GitHub Action Coverage Status Version PyPI - Wheel Pyversions Download Discord

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

# 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

# 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

License

This project is licensed under the Apache 2.0 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

casbin_django_orm_adapter-1.4.0.tar.gz (370.1 kB view details)

Uploaded Source

Built Distribution

casbin_django_orm_adapter-1.4.0-py3-none-any.whl (404.0 kB view details)

Uploaded Python 3

File details

Details for the file casbin_django_orm_adapter-1.4.0.tar.gz.

File metadata

File hashes

Hashes for casbin_django_orm_adapter-1.4.0.tar.gz
Algorithm Hash digest
SHA256 3323f7696c469e52b67cf390297e5e5dbdb076fafe0722ceb8110257907e3214
MD5 260198af71f3b9784fb2c844339125ca
BLAKE2b-256 38c98ac317d358f273ba9d1f62106d0a8d75cbfacca86f4209496a45de5f1134

See more details on using hashes here.

File details

Details for the file casbin_django_orm_adapter-1.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for casbin_django_orm_adapter-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 336f0d5460319b53e657f875022456549686405c505e77ce76bf8fdc19516039
MD5 89c952edfa5a937d4d50095379b72f54
BLAKE2b-256 0918dce41e07ad5e5080d451566b61ceb1a7858ddc70e9a4edbf1b52d6c4162d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page