PyMongo Adapter for PyCasbin
====
[](https://github.com/officialpycasbin/pymongo-adapter/actions/workflows/main.yml)
[](https://coveralls.io/github/officialpycasbin/pymongo-adapter)
[](https://pypi.org/project/casbin_pymongo_adapter/)
[](https://pypi.org/project/casbin_pymongo_adapter/)
[](https://pypi.org/project/casbin_pymongo_adapter/)
[](https://pypi.org/project/casbin_pymongo_adapter/)
[](https://pypi.org/project/casbin_pymongo_adapter/)
PyMongo Adapter is the [PyMongo](https://pypi.org/project/pymongo/) adapter for [PyCasbin](https://github.com/casbin/pycasbin). With this library, Casbin can load policy from MongoDB or save policy to it.
This adapter supports both synchronous and asynchronous PyMongo APIs.
## Installation
```
pip install casbin_pymongo_adapter
```
## Simple Example
```python
import casbin_pymongo_adapter
import casbin
adapter = casbin_pymongo_adapter.Adapter('mongodb://localhost:27017/', "dbname")
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 data1casbin_sqlalchemy_adapter
pass
else:
# deny the request, show an error
pass
# define filter conditions
from casbin_pymongo_adapter import Filter
filter = Filter()
filter.ptype = ["p"]
filter.v0 = ["alice"]
# support MongoDB native query
filter.raw_query = {
"ptype": "p",
"v0": {
"$in": ["alice"]
}
}
# In this case, load only policies with sub value alice
e.load_filtered_policy(filter)
```
## Async Example
```python
from casbin_pymongo_adapter.asynchronous import Adapter
import casbin
adapter = Adapter('mongodb://localhost:27017/', "dbname")
e = casbin.AsyncEnforcer('path/to/model.conf', adapter)
# Note: AsyncEnforcer does not automatically load policies.
# You need to call load_policy() manually.
await e.load_policy()
```
### 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/pymongo-adapter",
"name": "casbin-pymongo-adapter",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "pycasbin, casbin, pymongo, casbin-adapter, rbac, access control, abac, acl, permission",
"author": "Casbin",
"author_email": "admin@casbin.org",
"download_url": "https://files.pythonhosted.org/packages/0a/6b/e7bbfc5a07d075775ec0069b2b32c0aaed9c96229181e63fbbbdc770358c/casbin_pymongo_adapter-1.6.0.tar.gz",
"platform": null,
"description": "PyMongo Adapter for PyCasbin\n====\n\n[](https://github.com/officialpycasbin/pymongo-adapter/actions/workflows/main.yml)\n[](https://coveralls.io/github/officialpycasbin/pymongo-adapter)\n[](https://pypi.org/project/casbin_pymongo_adapter/)\n[](https://pypi.org/project/casbin_pymongo_adapter/)\n[](https://pypi.org/project/casbin_pymongo_adapter/)\n[](https://pypi.org/project/casbin_pymongo_adapter/)\n[](https://pypi.org/project/casbin_pymongo_adapter/)\n\nPyMongo Adapter is the [PyMongo](https://pypi.org/project/pymongo/) adapter for [PyCasbin](https://github.com/casbin/pycasbin). With this library, Casbin can load policy from MongoDB or save policy to it.\n\nThis adapter supports both synchronous and asynchronous PyMongo APIs.\n\n## Installation\n\n```\npip install casbin_pymongo_adapter\n```\n\n## Simple Example\n\n```python\nimport casbin_pymongo_adapter\nimport casbin\n\nadapter = casbin_pymongo_adapter.Adapter('mongodb://localhost:27017/', \"dbname\")\n\ne = casbin.Enforcer('path/to/model.conf', adapter, True)\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 data1casbin_sqlalchemy_adapter\n pass\nelse:\n # deny the request, show an error\n pass\n\n# define filter conditions\nfrom casbin_pymongo_adapter import Filter\n\nfilter = Filter()\nfilter.ptype = [\"p\"]\nfilter.v0 = [\"alice\"]\n\n# support MongoDB native query\nfilter.raw_query = {\n \"ptype\": \"p\",\n \"v0\": {\n \"$in\": [\"alice\"]\n }\n}\n\n# In this case, load only policies with sub value alice\ne.load_filtered_policy(filter)\n```\n\n## Async Example\n\n```python\nfrom casbin_pymongo_adapter.asynchronous import Adapter\nimport casbin\n\nadapter = Adapter('mongodb://localhost:27017/', \"dbname\")\ne = casbin.AsyncEnforcer('path/to/model.conf', adapter)\n\n# Note: AsyncEnforcer does not automatically load policies.\n# You need to call load_policy() manually.\nawait e.load_policy()\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": "PyMongo Adapter for PyCasbin",
"version": "1.6.0",
"project_urls": {
"Homepage": "https://github.com/officialpycasbin/pymongo-adapter"
},
"split_keywords": [
"pycasbin",
" casbin",
" pymongo",
" casbin-adapter",
" rbac",
" access control",
" abac",
" acl",
" permission"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "7144eb634768ce43ba9638f301d217d9dfd847f71222aaec853d8b95356b1b2b",
"md5": "9e6fe802a0d8ea425431c5d9e35f380d",
"sha256": "c06dcd2bc7c2be6af22a4b1ef3160cf4d12ba043609a93ca479ba6752c27161c"
},
"downloads": -1,
"filename": "casbin_pymongo_adapter-1.6.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "9e6fe802a0d8ea425431c5d9e35f380d",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.6",
"size": 13395,
"upload_time": "2025-08-15T13:28:12",
"upload_time_iso_8601": "2025-08-15T13:28:12.174225Z",
"url": "https://files.pythonhosted.org/packages/71/44/eb634768ce43ba9638f301d217d9dfd847f71222aaec853d8b95356b1b2b/casbin_pymongo_adapter-1.6.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "0a6be7bbfc5a07d075775ec0069b2b32c0aaed9c96229181e63fbbbdc770358c",
"md5": "3b858a3060e4e94da4bfb8eb35008fb6",
"sha256": "89ecc52a30c81551f8b5ecc24a962b694acf33d4fc9782db3d586f90530963a4"
},
"downloads": -1,
"filename": "casbin_pymongo_adapter-1.6.0.tar.gz",
"has_sig": false,
"md5_digest": "3b858a3060e4e94da4bfb8eb35008fb6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 12336,
"upload_time": "2025-08-15T13:28:13",
"upload_time_iso_8601": "2025-08-15T13:28:13.306231Z",
"url": "https://files.pythonhosted.org/packages/0a/6b/e7bbfc5a07d075775ec0069b2b32c0aaed9c96229181e63fbbbdc770358c/casbin_pymongo_adapter-1.6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-15 13:28:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "officialpycasbin",
"github_project": "pymongo-adapter",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [
{
"name": "pycasbin",
"specs": [
[
">=",
"2.0.0"
]
]
},
{
"name": "pymongo",
"specs": [
[
">=",
"4.13.0"
]
]
}
],
"lcname": "casbin-pymongo-adapter"
}