Skip to main content

Databases Casbin Adapter

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

Metadata

Release files for casbin-databases-adapter 1.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for casbin-databases-adapter 1.6.0
File Size Uploaded
casbin_databases_adapter-1.6.0.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for casbin-databases-adapter 1.6.0
File Interpreter ABI Platform
casbin_databases_adapter-1.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.5 kB

Release files / casbin_databases_adapter-1.6.0.tar.gz

Download URL casbin_databases_adapter-1.6.0.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1534a9eedcef63fec76b1fe876537a67d5fd99bb311d0a51873d0dc9648ce68c
BLAKE2b-256 checksum
How to use checksums
98c364a2658069a79801f4f9d64780fb2f93e0e4bf902d487f82099c2253ae55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.19

Release files / casbin_databases_adapter-1.6.0-py3-none-any.whl

Download URL casbin_databases_adapter-1.6.0-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e68add21be28aba94510bc96c9776b38068c011b871353ea967f3316e68bc9f
BLAKE2b-256 checksum
How to use checksums
c5ef487c0894460598efbeee29c36a478d6512fc0846ec08663260f670a454fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.19

Release history Release notifications | RSS feed

This release

1.6.0 This release

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page