PyMongo Adapter for PyCasbin
PyMongo Adapter is the PyMongo adapter for 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
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)
Using an Existing MongoDB Client
If you already have a MongoDB client instance in your application, you can reuse it:
from pymongo import MongoClient
import casbin_pymongo_adapter
import casbin
# Create or use your existing MongoDB client
mongo_client = MongoClient('mongodb://localhost:27017/')
# Pass the client to the adapter
adapter = casbin_pymongo_adapter.Adapter(client=mongo_client, db_name="casbin")
e = casbin.Enforcer('path/to/model.conf', adapter, True)
Async Example
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()
Using an Existing AsyncMongoClient
from pymongo import AsyncMongoClient
from casbin_pymongo_adapter.asynchronous import Adapter
import casbin
# Create or use your existing AsyncMongoClient
mongo_client = AsyncMongoClient('mongodb://localhost:27017/')
# Pass the client to the adapter
adapter = Adapter(client=mongo_client, db_name="casbin")
e = casbin.AsyncEnforcer('path/to/model.conf', adapter)
await e.load_policy()
Getting Help
License
This project is licensed under the Apache 2.0 license.
Release files for casbin-pymongo-adapter 1.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| casbin_pymongo_adapter-1.7.0.tar.gz | 13.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| casbin_pymongo_adapter-1.7.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 27.5 kB
Release files / casbin_pymongo_adapter-1.7.0.tar.gz
| Download URL | casbin_pymongo_adapter-1.7.0.tar.gz |
|---|---|
| Size | 13.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1326ea4b9a882d935fb792cbcb84dc62a4c45d34995012ad0754bcee248c2e47
|
|
BLAKE2b-256 checksum How to use checksums |
9a3a5b2f8119da0d506d4d8abdb2bd2c12512d339419327c51d364ddcb9084b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.12
|
Release files / casbin_pymongo_adapter-1.7.0-py2.py3-none-any.whl
| Download URL | casbin_pymongo_adapter-1.7.0-py2.py3-none-any.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
a4c7c55d284d7e46bde2e44436994461fe3152063c260d8d4a85338c0b619ab8
|
|
BLAKE2b-256 checksum How to use checksums |
6c408dc1c92ffdea42251bf87703bd249a0a4dc5ed35354ed14f5ac6ef74d07d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.12
|