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.9.0.tar.gz (12.5 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.9.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlalchemy_adapter-1.9.0.tar.gz
  • Upload date:
  • Size: 12.5 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.9.0.tar.gz
Algorithm Hash digest
SHA256 7a7f88c0ecda41ba3a6635197dd22f65cbf73e352bfb0f4a26207c24e683261a
MD5 75d3403971984b082e279482898aef4c
BLAKE2b-256 f197a7a3ba4edb229be8c21e7ff962d6d40ca0955dbdd6d8545bd5583ec381fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlalchemy_adapter-1.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c100e2f9c4ca82a2dad82b9b8e4a2f03f3aa2d0204732e138e41c46623da6f7
MD5 c0966631abeadf2c05c22b60c317b1d5
BLAKE2b-256 9b85bfd277c82b52318725499855e7ea42368d49df3327dbc63ea16ba9973fc0

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