Skip to main content

Tortoise ORM adapter for AsyncCasbin

Project description

Tortiose 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.0.post1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file casbin-tortoise-adapter-1.0.0.post1.tar.gz.

File metadata

File hashes

Hashes for casbin-tortoise-adapter-1.0.0.post1.tar.gz
Algorithm Hash digest
SHA256 26fb833813eda9ce4c216a08cbbbdfe73f9b6e7055c559466b7e976a4028b857
MD5 690f7606f7ae370efc5841e1e5a9ccf6
BLAKE2b-256 7e6a634ff8abd989acd784ee5ab763c8a0d13fcfbae3ba9969295536a8846aa5

See more details on using hashes here.

File details

Details for the file casbin_tortoise_adapter-1.0.0.post1-py3-none-any.whl.

File metadata

File hashes

Hashes for casbin_tortoise_adapter-1.0.0.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 75a2db11b6249aa4d765625dd61ac96a475fd822a2e0480afa1479a61ac3287a
MD5 25f640476d1a2bbe77e4cffb0638b078
BLAKE2b-256 7a82464f30586b173e24c367761ef5a5a33050acf6ad73cf7e7ccbcd1e50516a

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