|
| 1 | +Metadata-Version: 2.1 |
| 2 | +Name: casbin-databases-adapter |
| 3 | +Version: 1.5.0 |
| 4 | +Summary: This is an Adapter for PyCasbin that implemented using Databases connection to achieve async process |
| 5 | +Home-page: https://github.com/officialpycasbin/casbin-databases-adapter |
| 6 | +Author: Isa Setiawan Abdurrazaq |
| 7 | +Author-email: isasetiawan@protonmail.com |
| 8 | +License: Apache 2.0 |
| 9 | +Keywords: casbin,asynccasbin,async,databases,casbin-adapter,rbac,access control,abac,acl,permission |
| 10 | +Classifier: Programming Language :: Python :: 3.9 |
| 11 | +Classifier: Programming Language :: Python :: 3.10 |
| 12 | +Classifier: Programming Language :: Python :: 3.11 |
| 13 | +Classifier: Programming Language :: Python :: 3.12 |
| 14 | +Classifier: License :: OSI Approved :: Apache Software License |
| 15 | +Classifier: Operating System :: OS Independent |
| 16 | +Requires-Python: >=3.3 |
| 17 | +Description-Content-Type: text/markdown |
| 18 | +License-File: LICENSE |
| 19 | + |
| 20 | +# Databases Casbin Adapter |
| 21 | + |
| 22 | +[](https://github.com/officialpycasbin/casbin-databases-adapter/actions/workflows/build.yml) |
| 23 | +[](https://coveralls.io/github/officialpycasbin/casbin-databases-adapter?branch=master) |
| 24 | +[](https://pypi.org/project/casbin_databases_adapter/) |
| 25 | +[](https://pypi.org/project/casbin_databases_adapter/) |
| 26 | +[](https://pypi.org/project/casbin_databases_adapter/) |
| 27 | +[](https://pypi.org/project/casbin_databases_adapter/) |
| 28 | +[](https://pypi.org/project/casbin_databases_adapter/) |
| 29 | + |
| 30 | +This is an Adapter for [PyCasbin](https://github.com/casbin/pycasbin) that implemented using [Databases](https://www.encode.io/databases) connection to achieve async process |
| 31 | + |
| 32 | +## Installation |
| 33 | + |
| 34 | +``` |
| 35 | +pip install casbin_databases_adapter |
| 36 | +``` |
| 37 | + |
| 38 | +## Simple Example |
| 39 | + |
| 40 | +```python |
| 41 | +import casbin_databases_adapter |
| 42 | +import casbin |
| 43 | +from databases import Database |
| 44 | +import sqlalchemy |
| 45 | +from sqlalchemy import Table, Column, String, Integer |
| 46 | +from sqlalchemy.sql.ddl import CreateTable |
| 47 | +import asyncio |
| 48 | + |
| 49 | +DATABASE_URL = "sqlite+aiosqlite:///example.db" |
| 50 | + |
| 51 | +async def create_casbin_rule_table(db: Database): |
| 52 | + metadata = sqlalchemy.MetaData() |
| 53 | + table = Table( |
| 54 | + "casbin_rules", |
| 55 | + metadata, |
| 56 | + Column("id", Integer, primary_key=True), |
| 57 | + Column("ptype", String(255)), |
| 58 | + Column("v0", String(255)), |
| 59 | + Column("v1", String(255)), |
| 60 | + Column("v2", String(255)), |
| 61 | + Column("v3", String(255)), |
| 62 | + Column("v4", String(255)), |
| 63 | + Column("v5", String(255)), |
| 64 | + ) |
| 65 | + q = CreateTable(table) |
| 66 | + await db.execute(query=str(q)) |
| 67 | + return table |
| 68 | + |
| 69 | +async def main(): |
| 70 | + database = Database(DATABASE_URL) |
| 71 | + await database.connect() |
| 72 | + casbin_rule_table = await create_casbin_rule_table(database) |
| 73 | + adapter = casbin_databases_adapter.DatabasesAdapter(db=database, table=casbin_rule_table) |
| 74 | + |
| 75 | + e = casbin.Enforcer('path/to/model.conf', adapter) |
| 76 | + |
| 77 | + sub = "alice" # the user that wants to access a resource. |
| 78 | + obj = "data1" # the resource that is going to be accessed. |
| 79 | + act = "read" # the operation that the user performs on the resource. |
| 80 | + |
| 81 | + if e.enforce(sub, obj, act): |
| 82 | + # permit alice to read data1 |
| 83 | + pass |
| 84 | + else: |
| 85 | + # deny the request, show an error |
| 86 | + pass |
| 87 | + |
| 88 | +# run the main function |
| 89 | +asyncio.run(main()) |
| 90 | +``` |
| 91 | + |
| 92 | +### Getting Help |
| 93 | + |
| 94 | +- [PyCasbin](https://github.com/casbin/pycasbin) |
| 95 | + |
| 96 | +### License |
| 97 | + |
| 98 | +This project is licensed under the [Apache 2.0 license](LICENSE). |
0 commit comments