async-casbin-sqlmodel-adapter


Nameasync-casbin-sqlmodel-adapter JSON
Version 0.1.5 PyPI version JSON
download
home_pagehttps://github.com/shepilov-vladislav/sqlmodel-casbin-adapter
SummaryAsync SQLModel Adapter for PyCasbin
upload_time2024-02-04 22:01:08
maintainer
docs_urlNone
authorVladislav Shepilov
requires_python>=3.10,<4.0
licenseApache-2.0
keywords pycasbin asynccasbin sqlmodel
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Async SQLModel Adapter for PyCasbin
====

## Repo
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/shepilov-vladislav/async-casbin-sqlmodel-adapter/pytest.yml?branch=main&logo=github&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter)
[![Codecov](https://img.shields.io/codecov/c/github/shepilov-vladislav/async-casbin-sqlmodel-adapter?branch=main&logo=github&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter)
[![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/shepilov-vladislav/async-casbin-sqlmodel-adapter?branch=main&logo=code%20climate&style=for-the-badge)](https://codeclimate.com/github/shepilov-vladislav/async-casbin-sqlmodel-adapter/maintainability)
[![Dependabot](https://img.shields.io/badge/dependabot-Active-brightgreen?branch=main&logo=dependabot&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter)


## GitHub

[![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/shepilov-vladislav/async-casbin-sqlmodel-adapter?label=latest%20stable&sort=semver&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter/releases)
[![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/shepilov-vladislav/async-casbin-sqlmodel-adapter?label=latest%20unstable&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter/releases)
[![GitHub last commit](https://img.shields.io/github/last-commit/shepilov-vladislav/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter/commits/master)

## PyPI

[![PyPI - Version](https://img.shields.io/pypi/v/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)
[![PyPI - Python Versions](https://img.shields.io/pypi/pyversions/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)
[![PyPI - Python Wheel](https://img.shields.io/pypi/wheel/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)
[![PyPI - Format](https://img.shields.io/pypi/format/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)
[![PyPI - Status](https://img.shields.io/pypi/status/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)
[![PyPI - Downloads](https://img.shields.io/pypi/dd/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)
[![PyPI - License](https://img.shields.io/pypi/l/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)

Async SQLModel Adapter is the [SQLModel](https://github.com/tiangolo/sqlmodel) adapter for [PyCasbin](https://github.com/casbin/pycasbin). With this library, Casbin can load policy from SQLModel supported database or save policy to it.

Based on [Officially Supported Databases](https://github.com/tiangolo/sqlmodel), The current supported databases are:

- PostgreSQL
- MySQL
- SQLite

## Installation

```
pip install async_casbin_sqlmodel_adapter
```

or

```
poetry add async-casbin-sqlmodel-adapter
```

## Simple Example

```python
# 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

- [PyCasbin](https://github.com/casbin/pycasbin)

### License

This project is licensed under the [Apache 2.0 license](LICENSE).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/shepilov-vladislav/sqlmodel-casbin-adapter",
    "name": "async-casbin-sqlmodel-adapter",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "pycasbin,asynccasbin,sqlmodel",
    "author": "Vladislav Shepilov",
    "author_email": "shepilov.v@protonmail.com",
    "download_url": "https://files.pythonhosted.org/packages/8e/12/7fa2f41c7054eefcce8efb2992dcefb142691ecbe10c02e650cd8a35f58b/async_casbin_sqlmodel_adapter-0.1.5.tar.gz",
    "platform": null,
    "description": "Async SQLModel Adapter for PyCasbin\n====\n\n## Repo\n[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/shepilov-vladislav/async-casbin-sqlmodel-adapter/pytest.yml?branch=main&logo=github&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter)\n[![Codecov](https://img.shields.io/codecov/c/github/shepilov-vladislav/async-casbin-sqlmodel-adapter?branch=main&logo=github&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter)\n[![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/shepilov-vladislav/async-casbin-sqlmodel-adapter?branch=main&logo=code%20climate&style=for-the-badge)](https://codeclimate.com/github/shepilov-vladislav/async-casbin-sqlmodel-adapter/maintainability)\n[![Dependabot](https://img.shields.io/badge/dependabot-Active-brightgreen?branch=main&logo=dependabot&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter)\n\n\n## GitHub\n\n[![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/shepilov-vladislav/async-casbin-sqlmodel-adapter?label=latest%20stable&sort=semver&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter/releases)\n[![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/shepilov-vladislav/async-casbin-sqlmodel-adapter?label=latest%20unstable&style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter/releases)\n[![GitHub last commit](https://img.shields.io/github/last-commit/shepilov-vladislav/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://github.com/shepilov-vladislav/async-casbin-sqlmodel-adapter/commits/master)\n\n## PyPI\n\n[![PyPI - Version](https://img.shields.io/pypi/v/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)\n[![PyPI - Python Versions](https://img.shields.io/pypi/pyversions/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)\n[![PyPI - Python Wheel](https://img.shields.io/pypi/wheel/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)\n[![PyPI - Format](https://img.shields.io/pypi/format/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)\n[![PyPI - Status](https://img.shields.io/pypi/status/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)\n[![PyPI - Downloads](https://img.shields.io/pypi/dd/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)\n[![PyPI - License](https://img.shields.io/pypi/l/async-casbin-sqlmodel-adapter?style=for-the-badge)](https://pypi.org/project/async-casbin-sqlmodel-adapter)\n\nAsync SQLModel Adapter is the [SQLModel](https://github.com/tiangolo/sqlmodel) adapter for [PyCasbin](https://github.com/casbin/pycasbin). With this library, Casbin can load policy from SQLModel supported database or save policy to it.\n\nBased on [Officially Supported Databases](https://github.com/tiangolo/sqlmodel), The current supported databases are:\n\n- PostgreSQL\n- MySQL\n- SQLite\n\n## Installation\n\n```\npip install async_casbin_sqlmodel_adapter\n```\n\nor\n\n```\npoetry add async-casbin-sqlmodel-adapter\n```\n\n## Simple Example\n\n```python\n# Stdlib:\nimport asyncio\n\n# Thirdparty:\nimport casbin\nfrom async_casbin_sqlmodel_adapter import Adapter\nfrom sqlalchemy.ext.asyncio import create_async_engine\nfrom sqlmodel import Field, SQLModel\n\nengine = create_async_engine(\"sqlite+aiosqlite:///\")\n\n\nclass CasbinRule(SQLModel, table=True):  # type: ignore\n    \"\"\"\n    CasbinRule class for SQLModel-based Casbin adapter.\n    \"\"\"\n\n    __tablename__ = \"casbin_rule\"\n\n    id: int = Field(primary_key=True)\n    ptype: str = Field(max_length=255)\n    v0: str = Field(max_length=255)\n    v1: str = Field(max_length=255)\n    v2: str | None = Field(max_length=255, default=None)\n    v3: str | None = Field(max_length=255, default=None)\n    v4: str | None = Field(max_length=255, default=None)\n    v5: str | None = Field(max_length=255, default=None)\n\n    def __str__(self) -> str:\n        arr = [self.ptype]\n        # pylint: disable=invalid-name\n        for v in (self.v0, self.v1, self.v2, self.v3, self.v4, self.v5):\n            if v is None:\n                break\n            arr.append(v)\n        return \", \".join(arr)\n\n    def __repr__(self) -> str:\n        return f'<CasbinRule {self.id}: \"{str(self)}\">'\n\n\nasync def main():\n    async with engine.begin() as conn:\n        await conn.run_sync(SQLModel.metadata.create_all)\n\n    adapter = Adapter(engine)\n\n    e = casbin.AsyncEnforcer(\"path/to/model.conf\", adapter, True)\n\n    sub = \"alice\"  # the user that wants to access a resource.\n    obj = \"data1\"  # the resource that is going to be accessed.\n    act = \"read\"  # the operation that the user performs on the resource.\n\n    if e.enforce(sub, obj, act):\n        # permit alice to read data1async_casbin_sqlmodel_adapter\n        pass\n    else:\n        # deny the request, show an error\n        pass\n\n\nasyncio.run(main())\n```\n\n\n### Getting Help\n\n- [PyCasbin](https://github.com/casbin/pycasbin)\n\n### License\n\nThis project is licensed under the [Apache 2.0 license](LICENSE).\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Async SQLModel Adapter for PyCasbin",
    "version": "0.1.5",
    "project_urls": {
        "Homepage": "https://github.com/shepilov-vladislav/sqlmodel-casbin-adapter",
        "Repository": "https://github.com/shepilov-vladislav/sqlmodel-casbin-adapter"
    },
    "split_keywords": [
        "pycasbin",
        "asynccasbin",
        "sqlmodel"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3bce4c0d86a3b457ec30a2b6cedfef78070236fb9347cf4506707318b2f66e78",
                "md5": "564cfae4b39e969676a768414f341a36",
                "sha256": "eecf3bb9aeaa393bc4d5861b80d99a8010f54ec369f794bad9a27478a6235c71"
            },
            "downloads": -1,
            "filename": "async_casbin_sqlmodel_adapter-0.1.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "564cfae4b39e969676a768414f341a36",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 13836,
            "upload_time": "2024-02-04T22:01:07",
            "upload_time_iso_8601": "2024-02-04T22:01:07.081198Z",
            "url": "https://files.pythonhosted.org/packages/3b/ce/4c0d86a3b457ec30a2b6cedfef78070236fb9347cf4506707318b2f66e78/async_casbin_sqlmodel_adapter-0.1.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8e127fa2f41c7054eefcce8efb2992dcefb142691ecbe10c02e650cd8a35f58b",
                "md5": "20b33f659cf8530fe6cb408bc8f6c0db",
                "sha256": "b9eb0e201a55c81926250bff64d29dc410c89f393d1481e4ae5f3cf4e86be6b0"
            },
            "downloads": -1,
            "filename": "async_casbin_sqlmodel_adapter-0.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "20b33f659cf8530fe6cb408bc8f6c0db",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 9449,
            "upload_time": "2024-02-04T22:01:08",
            "upload_time_iso_8601": "2024-02-04T22:01:08.495300Z",
            "url": "https://files.pythonhosted.org/packages/8e/12/7fa2f41c7054eefcce8efb2992dcefb142691ecbe10c02e650cd8a35f58b/async_casbin_sqlmodel_adapter-0.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-04 22:01:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "shepilov-vladislav",
    "github_project": "sqlmodel-casbin-adapter",
    "github_not_found": true,
    "lcname": "async-casbin-sqlmodel-adapter"
}
        
Elapsed time: 4.56131s