Skip to main content

Async SQLModel Adapter for PyCasbin

Repo

GitHub Workflow Status Codecov Code Climate maintainability Dependabot

GitHub

GitHub tag (latest SemVer) GitHub tag (latest by date) GitHub last commit

PyPI

PyPI - Version PyPI - Python Versions PyPI - Python Wheel PyPI - Format PyPI - Status PyPI - Downloads PyPI - License

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.AsyncEnforcer("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.

Metadata

Release files for async-casbin-sqlmodel-adapter 0.1.5

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

Source distribution (sdist)

Source distribution for async-casbin-sqlmodel-adapter 0.1.5
File Size Uploaded Provenance Actions
async_casbin_sqlmodel_adapter-0.1.5.tar.gz 9.4 kB Without provenance Details for async_casbin_sqlmodel_adapter-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for async-casbin-sqlmodel-adapter 0.1.5
File Interpreter ABI Platform Actions
async_casbin_sqlmodel_adapter-0.1.5-py3-none-any.whl Python 3 none any Details for async_casbin_sqlmodel_adapter-0.1.5-py3-none-any.whl

Total release size: 23.3 kB

Release files / async_casbin_sqlmodel_adapter-0.1.5.tar.gz

Download URL async_casbin_sqlmodel_adapter-0.1.5.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b9eb0e201a55c81926250bff64d29dc410c89f393d1481e4ae5f3cf4e86be6b0
BLAKE2b-256 checksum
How to use checksums
8e127fa2f41c7054eefcce8efb2992dcefb142691ecbe10c02e650cd8a35f58b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Darwin/23.3.0

Release files / async_casbin_sqlmodel_adapter-0.1.5-py3-none-any.whl

Download URL async_casbin_sqlmodel_adapter-0.1.5-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eecf3bb9aeaa393bc4d5861b80d99a8010f54ec369f794bad9a27478a6235c71
BLAKE2b-256 checksum
How to use checksums
3bce4c0d86a3b457ec30a2b6cedfef78070236fb9347cf4506707318b2f66e78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Darwin/23.3.0

Security for async-casbin-sqlmodel-adapter 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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