Async SQLModel Adapter for PyCasbin
Project description
Async SQLModel Adapter for PyCasbin
Repo
GitHub
PyPI
Async SQLModel Adapter is the SQLModel adapter for PyCasbin. With this library, Casbin can load policy from SQLModel supported database or save policy to it.
Based on Officially Supported Databases, The current supported databases are:
- PostgreSQL
- MySQL
- SQLite
Installation
pip install async_casbin_sqlmodel_adapter
or
poetry add async-casbin-sqlmodel-adapter
Simple Example
# Stdlib:
import asyncio
# Thirdparty:
import casbin
from async_casbin_sqlmodel_adapter import Adapter
from sqlalchemy.ext.asyncio import create_async_engine
from sqlmodel import Field, SQLModel
engine = create_async_engine("sqlite+aiosqlite:///")
class CasbinRule(SQLModel, table=True): # type: ignore
"""
CasbinRule class for SQLModel-based Casbin adapter.
"""
__tablename__ = "casbin_rule"
id: int = Field(primary_key=True)
ptype: str = Field(max_length=255)
v0: str = Field(max_length=255)
v1: str = Field(max_length=255)
v2: str | None = Field(max_length=255, default=None)
v3: str | None = Field(max_length=255, default=None)
v4: str | None = Field(max_length=255, default=None)
v5: str | None = Field(max_length=255, default=None)
def __str__(self) -> str:
arr = [self.ptype]
# pylint: disable=invalid-name
for v in (self.v0, self.v1, self.v2, self.v3, self.v4, self.v5):
if v is None:
break
arr.append(v)
return ", ".join(arr)
def __repr__(self) -> str:
return f'<CasbinRule {self.id}: "{str(self)}">'
async def main():
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
adapter = Adapter(engine)
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 data1async_casbin_sqlmodel_adapter
pass
else:
# deny the request, show an error
pass
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
Close
Hashes for async-casbin-sqlmodel-adapter-0.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa46b737687d9202989086b28d31b46ffcf79cc6368c6371f04641c43b84c79b |
|
MD5 | 3b70dc922d46255c10b3f4debc56817e |
|
BLAKE2b-256 | dee537c43de5c193b3427218bd54af575ff21e5d4906ea67a80aebc22ae07f6b |
Close
Hashes for async_casbin_sqlmodel_adapter-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 559f06dc320f6ea5aeb1a4666560bccb7fc7bb8ec50e7aa5b9af812eb1a576c2 |
|
MD5 | 5b624b879bdfa413939a68c801e434f4 |
|
BLAKE2b-256 | 32efd2b02afcbd17c8bcf83493de9b4e85d24f648089b5c80f262e27cc631076 |