Skip to main content

SQLAlchemy Adapter for PyCasbin

Project description

SQLAlchemy Adapter for PyCasbin

build 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 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_adapter-1.8.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_adapter-1.8.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_adapter-1.8.0.tar.gz.

File metadata

  • Download URL: sqlalchemy_adapter-1.8.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for sqlalchemy_adapter-1.8.0.tar.gz
Algorithm Hash digest
SHA256 575795995ccaade90efcf35ba8e8e68ea82bd74bfeeade4367862c9ed0e602c6
MD5 b6237fa5a621b14e8d6530bd72e18e6a
BLAKE2b-256 d10ad156c25ef8c7769f2e6039d9d11b6df9301eadab8c0bfcf2f32fd239d45e

See more details on using hashes here.

File details

Details for the file sqlalchemy_adapter-1.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_adapter-1.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06db8ed3151634d7d5e3ab9d1d42053ea62f5466c647aaf31baf94611d69b20c
MD5 c37d1c9f6a82c2812368d5783d696c1f
BLAKE2b-256 0bf4a18c747a70d2f4b4041a06608c3455432ff79cfa27a7d7fcb02025c98515

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page