chore(release): 1.4.0 [skip ci] # [1.4.0](https://github.com/officialpycasbin/pymongo-adapter/compare/v1.3.0...v1.4.0) (2025-08-09) ### Features * support sync & async load_filtered_policy ([#5](https://github.com/officialpycasbin/pymongo-adapter/issues/5)) ([875263c](https://github.com/officialpycasbin/pymongo-adapter/commit/875263cac37413d7645981b3c3959420e349e3f2))
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.
pip install casbin_pymongo_adapter
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)
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()
This project is licensed under the Apache 2.0 license.