SQLAlchemy Adapter for PyCasbin
SQLAlchemy Adapter is the SQLAlchemy adapter for PyCasbin. With this library, Casbin can load policy from SQLAlchemy supported database or save policy to it.
Based on Officially Supported Databases, The current supported databases are:
- PostgreSQL
- MySQL
- SQLite
- Oracle
- Microsoft SQL Server
- Firebird
- Sybase
Installation
pip install sqlalchemy_adapter
Simple Example
You can save and load policy to database.
import sqlalchemy_adapter
import casbin
adapter = sqlalchemy_adapter.Adapter('sqlite:///test.db')
e = casbin.Enforcer('path/to/model.conf', adapter)
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
By default, policies are stored in the casbin_rule table.
You can custom the table where the policy is stored by using the table_name parameter.
import sqlalchemy_adapter
import casbin
custom_table_name = "<custom_table_name>"
# create adapter with custom table name.
adapter = sqlalchemy_adapter.Adapter('sqlite:///test.db', table_name=custom_table_name)
e = casbin.Enforcer('path/to/model.conf', adapter)
Prevent Automatic Table Creation
By default, the adapter automatically creates the necessary database tables during initialization. If you want to use the adapter only as an intermediary without automatically creating tables, you can set the create_table parameter to False:
import sqlalchemy_adapter
import casbin
# Create adapter without automatically creating tables
adapter = sqlalchemy_adapter.Adapter('sqlite:///test.db', create_table=False)
e = casbin.Enforcer('path/to/model.conf', adapter)
This is useful when:
- Tables are already created by your database migration system
- You want to manage table creation separately
- You are using the adapter as an intermediary between SQLAlchemy and your system
Note: When create_table=False, you are responsible for ensuring the required tables exist in the database before using the adapter.
Getting Help
License
This project is licensed under the Apache 2.0 license.
Metadata
Release files for sqlalchemy-adapter 1.9.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sqlalchemy_adapter-1.9.0.tar.gz | 12.5 kB | Without provenance | Details for sqlalchemy_adapter-1.9.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sqlalchemy_adapter-1.9.0-py3-none-any.whl | Python 3 | none | any | Details for sqlalchemy_adapter-1.9.0-py3-none-any.whl |
Total release size: 22.4 kB
Release files / sqlalchemy_adapter-1.9.0.tar.gz
| Download URL | sqlalchemy_adapter-1.9.0.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7a7f88c0ecda41ba3a6635197dd22f65cbf73e352bfb0f4a26207c24e683261a
|
|
BLAKE2b-256 checksum How to use checksums |
f197a7a3ba4edb229be8c21e7ff962d6d40ca0955dbdd6d8545bd5583ec381fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.12
|
Release files / sqlalchemy_adapter-1.9.0-py3-none-any.whl
| Download URL | sqlalchemy_adapter-1.9.0-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7c100e2f9c4ca82a2dad82b9b8e4a2f03f3aa2d0204732e138e41c46623da6f7
|
|
BLAKE2b-256 checksum How to use checksums |
9b85bfd277c82b52318725499855e7ea42368d49df3327dbc63ea16ba9973fc0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.12
|
Security for sqlalchemy-adapter 1.9.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.