Skip to main content

Tortoise ORM adapter for AsyncCasbin

Project description

Tortoise ORM Adapter for AsyncCasbin

GitHub Workflow Status GitHub

This is an asynchronous adapter for AsyncCasbin using Tortoise ORM.

Installation

pip install casbin-tortoise-adapter
# or via your favorite dependency manager

The current supported databases are limited by Tortoise ORM, and include:

  • SQLite
  • PostgreSQL >= 9.4 (using asyncpg)
  • MySQL/MariaDB (using aiomysql)

Documentation

The only possible configurable is the underlying Model used by TortoiseAdapter. While simple, it should be plenty to cover most use cases that one could come across. You can change the model by passing the modelclass: CasbinRule keyword argument to the adapter and updating the model in your Tortoise ORM init configuration.

The modelclass value must inherit from casbin_tortoise_adapter.CasbinRule to ensure that all the expected fields are present. A TypeError will throw if this is not the case.

A custom Model, combined with advanced configuration like show in the Tortoise ORM "Two Databases" example, allow you to change where your authorization rules are stored (database, model name, etc.)

Base Example

from casbin import Enforcer
from tortoise import Tortoise

from casbin_tortoise_adapter import CasbinRule, TortoiseAdapter

async def main()
    # connect to db and generate schemas
    await Tortoise.init(
        db_url="postgres://postgres:password@test-db:5432/my_app",
        modules={"models": ["casbin_tortoise_adapter"]},
    )
    await Tortoise.generate_schemas()

    adapter = casbin_tortoise_adapter.TortoiseAdapter()
    e = casbin.Enforcer('path/to/model.conf', adapter, True)

    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 data1
        pass
    else:
        # deny the request, show an error
        pass

License

This project, like other adapters, 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-tortoise-adapter-1.0.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

casbin_tortoise_adapter-1.0.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file casbin-tortoise-adapter-1.0.1.tar.gz.

File metadata

  • Download URL: casbin-tortoise-adapter-1.0.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.2 Windows/10

File hashes

Hashes for casbin-tortoise-adapter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0ebf89c5ef4d36c1458044fbdd84184bbd5a6d02deffcb7175793ae43c96a363
MD5 695e9cdeaece93c459a4207d2b71f13c
BLAKE2b-256 c689333d4819ab1d74b0fc6a50d5b529e7f0e87a7f546541191f05552720bb2f

See more details on using hashes here.

File details

Details for the file casbin_tortoise_adapter-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for casbin_tortoise_adapter-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 865a50bf0f383e7e8b4fc7aab56224a5c646ea6fd8ab5087d97c9488588615ce
MD5 e96966f8710b066f519a41ea2e14f173
BLAKE2b-256 3f31e482489e9da0d498973d96ff90fd07316b9f63cee4585de2fa0e0fc71c96

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