@@ -16,6 +16,39 @@ With this library, Casbin can load policy from MongoDB or save policy to it.
1616Motor support a coroutine-based API for non-blocking access to MongoDB.
1717So that, this adapter allows you to use pycasbin's [ AsyncEnforcer] ( https://github.com/casbin/pycasbin/blob/master/casbin/async_enforcer.py ) .
1818
19+ ### ` load_policy() `
20+
21+ ` load_policy() ` loads all policies from storage.
22+
23+ ``` py
24+ import casbin_motor_adapter
25+ import casbin
26+
27+ adapter = casbin_motor_adapter.Adapter(' mongodb://localhost:27017/' , " dbname" )
28+
29+ e = casbin.AsyncEnforcer(' path/to/model.conf' , adapter, True )
30+ await e.load_policy()
31+
32+ sub = " alice" # the user that wants to access a resource.
33+ obj = " data1" # the resource that is going to be accessed.
34+ act = " read" # the operation that the user performs on the resource.
35+
36+ if e.enforce(sub, obj, act):
37+ # permit alice to read data1
38+ pass
39+ else :
40+ # deny the request, show an error
41+ pass
42+ ```
43+
44+ ### ` load_filtered_policy() `
45+
46+ ` load_filtered_policy() ` loads filtered policies from storage. This is useful for performance optimization.
47+
48+ > Policy Subset Loading, https://casbin.org/docs/policy-subset-loading
49+
50+ Additionally, ` load_filtered_policy() ` supports the MongoDB native queries for filtering conditions.
51+
1952``` py
2053import casbin_motor_adapter
2154import casbin
@@ -24,6 +57,22 @@ adapter = casbin_motor_adapter.Adapter('mongodb://localhost:27017/', "dbname")
2457
2558e = casbin.AsyncEnforcer(' path/to/model.conf' , adapter, True )
2659
60+ # define filter conditions
61+ filter = Filter()
62+ filter .ptype = [" p" ]
63+ filter .v0 = [" alice" ]
64+
65+ # support MongoDB native query
66+ filter .raw_query = {
67+ " ptype" : " p" ,
68+ " v0" : {
69+ " $in" : [" alice" ]
70+ }
71+ }
72+
73+ # In this case, load only policies with sub value alice
74+ await e.load_filtered_policy(filter )
75+
2776sub = " alice" # the user that wants to access a resource.
2877obj = " data1" # the resource that is going to be accessed.
2978act = " read" # the operation that the user performs on the resource.
@@ -34,6 +83,7 @@ if e.enforce(sub, obj, act):
3483else :
3584 # deny the request, show an error
3685 pass
86+
3787```
3888
3989## Acknowledgments
0 commit comments