This is an Adapter for PyCasbin that implemented using Databases connection to achieve async process
Project description
Databases Casbin Adapter
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
Built Distribution
File details
Details for the file casbin_databases_adapter-1.3.0.tar.gz
.
File metadata
- Download URL: casbin_databases_adapter-1.3.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37ace2ee91af5df3ffbde51f55d2f044d0479ea19828f30ab7648d5739d42c24 |
|
MD5 | 2d50abb6b74e3073750be5458fa10523 |
|
BLAKE2b-256 | 3a21d6ee80f18fbe63a67d0e121635e66d2e64c811353d432e5aab514eeed5f5 |
Provenance
File details
Details for the file casbin_databases_adapter-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: casbin_databases_adapter-1.3.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e12bd4552c57989388106777d6407278e62e9e29ad5976cd62640a37e73922c |
|
MD5 | 1ab167a8ed4f4e07169609519a35787e |
|
BLAKE2b-256 | 4d05ed94599dbec3bc61b591907782df37475cc1d98cbae01862c0a3730b1414 |