SQLAlchemy Adapter for PyCasbin
====
[](https://github.com/officialpycasbin/sqlalchemy-adapter/actions/workflows/build.yml)
[](https://coveralls.io/github/officialpycasbin/sqlalchemy-adapter)
[](https://pypi.org/project/sqlalchemy-adapter/)
[](https://pypi.org/project/sqlalchemy-adapter/)
[](https://pypi.org/project/sqlalchemy-adapter/)
[](https://pypi.org/project/sqlalchemy-adapter/)
[](https://pypi.org/project/sqlalchemy-adapter/)
SQLAlchemy Adapter is the [SQLAlchemy](https://www.sqlalchemy.org) adapter for [PyCasbin](https://github.com/casbin/pycasbin). With this library, Casbin can load policy from SQLAlchemy supported database or save policy to it.
Based on [Officially Supported Databases](http://www.sqlalchemy.org/), The current supported databases are:
- PostgreSQL
- MySQL
- SQLite
- Oracle
- Microsoft SQL Server
- Firebird
- Sybase
## Installation
```
pip install sqlalchemy_adapter
```
## Simple Example
You can save and load policy to database.
```python
import sqlalchemy_adapter
import casbin
adapter = sqlalchemy_adapter.Adapter('sqlite:///test.db')
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
```
By default, policies are stored in the `casbin_rule` table.
You can custom the table where the policy is stored by using the `table_name` parameter.
```python
import sqlalchemy_adapter
import casbin
custom_table_name = "<custom_table_name>"
# create adapter with custom table name.
adapter = sqlalchemy_adapter.Adapter('sqlite:///test.db', table_name=custom_table_name)
e = casbin.Enforcer('path/to/model.conf', adapter)
```
### 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/officialpycasbin/sqlalchemy-adapter",
"name": "sqlalchemy-adapter",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.3",
"maintainer_email": null,
"keywords": "pycasbin, casbin, SQLAlchemy, casbin-adapter, rbac, access control, abac, acl, permission",
"author": "Casbin",
"author_email": "admin@casbin.org",
"download_url": "https://files.pythonhosted.org/packages/c8/d9/ec30050fb0e00391ab47aa7beb8862dbca1161f755daf1889a9593229852/sqlalchemy_adapter-1.7.0.tar.gz",
"platform": null,
"description": "SQLAlchemy Adapter for PyCasbin \n====\n\n[](https://github.com/officialpycasbin/sqlalchemy-adapter/actions/workflows/build.yml)\n[](https://coveralls.io/github/officialpycasbin/sqlalchemy-adapter)\n[](https://pypi.org/project/sqlalchemy-adapter/)\n[](https://pypi.org/project/sqlalchemy-adapter/)\n[](https://pypi.org/project/sqlalchemy-adapter/)\n[](https://pypi.org/project/sqlalchemy-adapter/)\n[](https://pypi.org/project/sqlalchemy-adapter/)\n\nSQLAlchemy Adapter is the [SQLAlchemy](https://www.sqlalchemy.org) adapter for [PyCasbin](https://github.com/casbin/pycasbin). With this library, Casbin can load policy from SQLAlchemy supported database or save policy to it.\n\nBased on [Officially Supported Databases](http://www.sqlalchemy.org/), The current supported databases are:\n\n- PostgreSQL\n- MySQL\n- SQLite\n- Oracle\n- Microsoft SQL Server\n- Firebird\n- Sybase\n\n## Installation\n\n```\npip install sqlalchemy_adapter\n```\n\n## Simple Example\n\nYou can save and load policy to database.\n\n```python\nimport sqlalchemy_adapter\nimport casbin\n\nadapter = sqlalchemy_adapter.Adapter('sqlite:///test.db')\n\ne = casbin.Enforcer('path/to/model.conf', adapter)\n\nsub = \"alice\" # the user that wants to access a resource.\nobj = \"data1\" # the resource that is going to be accessed.\nact = \"read\" # the operation that the user performs on the resource.\n\nif e.enforce(sub, obj, act):\n # permit alice to read data1\n pass\nelse:\n # deny the request, show an error\n pass\n```\n\nBy default, policies are stored in the `casbin_rule` table.\nYou can custom the table where the policy is stored by using the `table_name` parameter.\n\n```python\n\nimport sqlalchemy_adapter\nimport casbin\n\ncustom_table_name = \"<custom_table_name>\"\n\n# create adapter with custom table name.\nadapter = sqlalchemy_adapter.Adapter('sqlite:///test.db', table_name=custom_table_name)\n\ne = casbin.Enforcer('path/to/model.conf', adapter)\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": "SQLAlchemy Adapter for PyCasbin",
"version": "1.7.0",
"project_urls": {
"Homepage": "https://github.com/officialpycasbin/sqlalchemy-adapter"
},
"split_keywords": [
"pycasbin",
" casbin",
" sqlalchemy",
" casbin-adapter",
" rbac",
" access control",
" abac",
" acl",
" permission"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "3d62cdf8128f0a3b14b6a72238a2bf8f401cab28c41478fb4e68c4dcd947b9b9",
"md5": "86ec7e6aa3cfc443605e939062e85afe",
"sha256": "34fd5315d0ff95947a0ecf1debbfb4cefce9df2f5e5cd1be2b44a2c3df407630"
},
"downloads": -1,
"filename": "sqlalchemy_adapter-1.7.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "86ec7e6aa3cfc443605e939062e85afe",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.3",
"size": 9395,
"upload_time": "2025-08-29T12:28:19",
"upload_time_iso_8601": "2025-08-29T12:28:19.586063Z",
"url": "https://files.pythonhosted.org/packages/3d/62/cdf8128f0a3b14b6a72238a2bf8f401cab28c41478fb4e68c4dcd947b9b9/sqlalchemy_adapter-1.7.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "c8d9ec30050fb0e00391ab47aa7beb8862dbca1161f755daf1889a9593229852",
"md5": "e7b7b5bc2f7cd5d4aa0de5d25f9c816e",
"sha256": "cb29c00cbb998c1e542f2da8229df64bd12cf5448b732ec0b0054a27a9426396"
},
"downloads": -1,
"filename": "sqlalchemy_adapter-1.7.0.tar.gz",
"has_sig": false,
"md5_digest": "e7b7b5bc2f7cd5d4aa0de5d25f9c816e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.3",
"size": 11543,
"upload_time": "2025-08-29T12:28:20",
"upload_time_iso_8601": "2025-08-29T12:28:20.318195Z",
"url": "https://files.pythonhosted.org/packages/c8/d9/ec30050fb0e00391ab47aa7beb8862dbca1161f755daf1889a9593229852/sqlalchemy_adapter-1.7.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-29 12:28:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "officialpycasbin",
"github_project": "sqlalchemy-adapter",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [
{
"name": "pycasbin",
"specs": [
[
">=",
"2.0.0"
]
]
},
{
"name": "SQLAlchemy",
"specs": [
[
">=",
"1.2.18"
]
]
}
],
"lcname": "sqlalchemy-adapter"
}