@@ -13,7 +13,7 @@ PyCasbin
1313
1414<a href =" https://casdoor.org/ " ><img src =" https://user-images.githubusercontent.com/3787410/147868267-6ac74908-5654-4f9c-ac79-8852af9ff925.png " alt =" casdoor " style =" width : 50% ; height : 50% " /></a >
1515
16- ** News** : Async version PyCasbin can be found at: https://pypi.org/project/asynccasbin/
16+ ** News** : Async is now supported by Pycasbin >= 1.23.0!
1717
1818** News** : still worry about how to write the correct Casbin policy? `` Casbin online editor `` is coming to help! Try it at: http://casbin.org/editor/
1919
@@ -44,6 +44,7 @@ production-ready | production-ready | beta-test | production-ready
4444- [ Policy management] ( #policy-management )
4545- [ Policy persistence] ( #policy-persistence )
4646- [ Role manager] ( #role-manager )
47+ - [ Async Enforcer] ( #async-enforcer )
4748- [ Benchmarks] ( #benchmarks )
4849- [ Examples] ( #examples )
4950- [ Middlewares] ( #middlewares )
@@ -211,6 +212,63 @@ https://casbin.org/docs/adapters
211212
212213https://casbin.org/docs/role-managers
213214
215+ ## Async Enforcer
216+
217+ If your code use ` async ` / ` await ` and is heavily dependent on I/O operations, you can adopt Async Enforcer!
218+
219+ 1 . Create an async engine and new a Casbin AsyncEnforcer with a model file and an async Pycasbin adapter:
220+
221+ ``` python
222+ import asyncio
223+ import os
224+
225+ import casbin
226+ from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
227+ from sqlalchemy.orm import sessionmaker
228+
229+ from casbin_async_sqlalchemy_adapter import Adapter, CasbinRule
230+
231+
232+ async def get_enforcer ():
233+ engine = create_async_engine(" sqlite+aiosqlite://" , future = True )
234+ adapter = Adapter(engine)
235+ await adapter.create_table()
236+
237+ async_session = sessionmaker(engine, expire_on_commit = False , class_ = AsyncSession)
238+ async with async_session() as s:
239+ s.add(CasbinRule(ptype = " p" , v0 = " alice" , v1 = " data1" , v2 = " read" ))
240+ s.add(CasbinRule(ptype = " p" , v0 = " bob" , v1 = " data2" , v2 = " write" ))
241+ s.add(CasbinRule(ptype = " p" , v0 = " data2_admin" , v1 = " data2" , v2 = " read" ))
242+ s.add(CasbinRule(ptype = " p" , v0 = " data2_admin" , v1 = " data2" , v2 = " write" ))
243+ s.add(CasbinRule(ptype = " g" , v0 = " alice" , v1 = " data2_admin" ))
244+ await s.commit()
245+
246+ e = casbin.AsyncEnforcer(" path/to/model.conf" , adapter)
247+ await e.load_policy()
248+ return e
249+ ```
250+
251+ Note: you can see all supported adapters in [ Adapters | Casbin] ( https://casbin.org/docs/adapters ) .
252+
253+ 2 . Add an enforcement hook into your code right before the access happens:
254+
255+ ``` python
256+ async def main ():
257+ e = await get_enforcer()
258+ if e.enforce(" alice" , " data1" , " read" ):
259+ print (" alice can read data1" )
260+ else :
261+ print (" alice can not read data1" )
262+ ```
263+
264+ 3 . Run the code:
265+
266+ ``` python
267+ asyncio.run(main())
268+ ```
269+
270+ 4 . Please refer to the `` tests `` files for more usage.
271+
214272## Benchmarks
215273
216274https://casbin.org/docs/benchmark
0 commit comments