Skip to main content

This is an Adapter for PyCasbin that implemented using Databases connection to achieve async process

Project description

Databases Casbin Adapter

GitHub Actions Coverage Status Version PyPI - Wheel Pyversions Download License

This is an Adapter for PyCasbin that implemented using Databases connection to achieve async process

Installation

pip install casbin_databases_adapter

Simple Example

import casbin_databases_adapter
import casbin
from databases import Database
import sqlalchemy
from sqlalchemy import Table, Column, String, Integer
from sqlalchemy.sql.ddl import CreateTable
import asyncio

DATABASE_URL = "sqlite+aiosqlite:///example.db"

async def create_casbin_rule_table(db: Database):
    metadata = sqlalchemy.MetaData()
    table = Table(
        "casbin_rules",
        metadata,
        Column("id", Integer, primary_key=True),
        Column("ptype", String(255)),
        Column("v0", String(255)),
        Column("v1", String(255)),
        Column("v2", String(255)),
        Column("v3", String(255)),
        Column("v4", String(255)),
        Column("v5", String(255)),
    )
    q = CreateTable(table)
    await db.execute(query=str(q))
    return table

async def main():
    database = Database(DATABASE_URL)
    await database.connect()
    casbin_rule_table = await create_casbin_rule_table(database)
    adapter = casbin_databases_adapter.DatabasesAdapter(db=database, table=casbin_rule_table)
    
    e = casbin.Enforcer('path/to/model.conf', adapter)
    
    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

# run the main function
asyncio.run(main())

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_databases_adapter-1.4.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

casbin_databases_adapter-1.4.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: casbin_databases_adapter-1.4.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for casbin_databases_adapter-1.4.0.tar.gz
Algorithm Hash digest
SHA256 75d5fa89182ff65ea4a8faab5be15e155019a724f20dae51cf52b1dbfa408773
MD5 5652647e112157aa13fcbb520fd1093c
BLAKE2b-256 2d696adf2fa34593e51e71c71ee08f3f74c5a96b91639363872da1bfc30511eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for casbin_databases_adapter-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 956df8a1d114bac971b8c6b8e631a49dee4a44962f64fa010dff1aad38f0e737
MD5 b72239f3d6cbc965bad83ff61fc16fc7
BLAKE2b-256 3bda5e25ca8c4fa7726a9a91a94d90f8780c7f382ca9e86dd3110c7a5befd8a6

See more details on using hashes here.

Supported by

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