Skip to main content

Tortoise ORM adapter for AsyncCasbin

Project description

Tortoise ORM Adapter for AsyncCasbin

GitHub Workflow Status PyPI - Downloads GitHub Buy a tree

This is an asynchronous adapter for AsyncCasbin using Tortoise ORM.

Installation

python3 -m pip install --user casbin-tortoise-adapter
# or via your favorite dependency manager, like Poetry

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

  • PostgreSQL >= 9.4 (using asyncpg)
  • SQLite (using aiosqlite)
  • MySQL/MariaDB (using asyncmy)
  • Microsoft SQL Server / Oracle (using asyncodbc)

Documentation

The only 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.)

Basic 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.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to my forest you’ll be creating employment for local families and restoring wildlife habitats.

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.2.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

casbin_tortoise_adapter-1.2.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for casbin-tortoise-adapter-1.2.0.tar.gz
Algorithm Hash digest
SHA256 08cd69c9dbd61a6679e935512037fe16ba46238d8d0df2efb195726e8a6d43b8
MD5 44960998235fe1cb2b79a93443a18ca6
BLAKE2b-256 d97e125ee4d6117aa160b7d01309fd18bcc01f9c5b8ef075e173788a42d824ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for casbin_tortoise_adapter-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 85a9c909c69698b5c5afe5d005ad2649731eeef04edaae55e849a6c5ae0e5990
MD5 fc9075f180e1c5e116807aabb3d5c870
BLAKE2b-256 c7a1c4a33d8565a122b7537c00bbd34476cd461306a1f78c10296be03ab13696

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