Skip to main content

SQLAlchemy Adapter for PyCasbin

GitHub Actions Coverage Status Version PyPI - Wheel Pyversions Download License

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 casbin_sqlalchemy_adapter

Simple Example

import casbin_sqlalchemy_adapter
import casbin

adapter = casbin_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

Soft Delete example

Soft Delete for casbin rules is supported, only when using a custom casbin rule model. The Soft Delete mechanism is enabled by passing the attribute of the flag indicating whether a rule is deleted to db_class_softdelete_attribute. That attribute needs to be of type sqlalchemy.Boolean.

adapter = Adapter(
    engine,
    db_class=MyCustomCasbinRuleModel,
    db_class_softdelete_attribute=MyCustomCasbinRuleModel.is_deleted,
)

Please be aware that this adapter only sets a flag like is_deleted to True. The provided model needs to handle the update of fields like deleted_by, deleted_at, etc. An example for this is given in examples/softdelete.py.

Getting Help

License

This project is licensed under the Apache 2.0 license.

Metadata

Release files for casbin-sqlalchemy-adapter 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for casbin-sqlalchemy-adapter 1.4.0
File Size Uploaded
casbin_sqlalchemy_adapter-1.4.0.tar.gz 12.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for casbin-sqlalchemy-adapter 1.4.0
File Interpreter ABI Platform
casbin_sqlalchemy_adapter-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.4 kB

Release files / casbin_sqlalchemy_adapter-1.4.0.tar.gz

Download URL casbin_sqlalchemy_adapter-1.4.0.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3cc439e3911a0f0c0de66ffde7e0de0586be1e1c772302e003d84164f4bfe291
BLAKE2b-256 checksum
How to use checksums
1a4f2e980d63960acdb106edc97b40abd42a752f71082ded7247a0fde8a0dece
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release files / casbin_sqlalchemy_adapter-1.4.0-py3-none-any.whl

Download URL casbin_sqlalchemy_adapter-1.4.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3e4e5d05e47fc6e04995ac26d36f53243f87ac9e3c66b24874ce6353e784897
BLAKE2b-256 checksum
How to use checksums
8054cd2fd15e94426feb1744171b3e9f075f9880e115a69c1e3356ad2a07b437
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page