Motor adapter for PyCasbin.
With this library, Casbin can load policy from MongoDB or save policy to it.
Motor support a coroutine-based API for non-blocking access to MongoDB.
So that, this adapter allows you to use pycasbin's AsyncEnforcer.
load_policy()
loads all policies from storage.
import casbin_motor_adapter
import casbin
adapter = casbin_motor_adapter.Adapter('mongodb://localhost:27017/', "dbname")
e = casbin.AsyncEnforcer('path/to/model.conf', adapter, True)
await e.load_policy()
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
load_filtered_policy()
loads filtered policies from storage. This is useful for performance optimization.
Policy Subset Loading, https://casbin.org/docs/policy-subset-loading
Additionally, load_filtered_policy()
supports the MongoDB native queries for filtering conditions.
import casbin_motor_adapter
import casbin
adapter = casbin_motor_adapter.Adapter('mongodb://localhost:27017/', "dbname")
e = casbin.AsyncEnforcer('path/to/model.conf', adapter, True)
# define filter conditions
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
await e.load_filtered_policy(filter)
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
This adapter was inspired by pymongo-adapter.