https://github.com/officialpycasbin/pymongo-adapter
PyMongo Adapter for PyCasbin
https://github.com/officialpycasbin/pymongo-adapter
abac acl adapter auth authorization authz casbin db mongo mongodb pycasbin pymongo python rbac
Last synced: 10 months ago
JSON representation
PyMongo Adapter for PyCasbin
- Host: GitHub
- URL: https://github.com/officialpycasbin/pymongo-adapter
- Owner: officialpycasbin
- License: apache-2.0
- Created: 2024-11-11T17:24:24.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-08-15T13:28:11.000Z (10 months ago)
- Last Synced: 2025-08-15T20:53:07.492Z (10 months ago)
- Topics: abac, acl, adapter, auth, authorization, authz, casbin, db, mongo, mongodb, pycasbin, pymongo, python, rbac
- Language: Python
- Homepage: https://github.com/casbin/pycasbin
- Size: 48.8 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
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).