Skip to main content

SQLAlchemy Acces List

Virtual Environment

To initialize virtual env run following command in project root, pipenv required

$ pipenv shell

Testing ACL

Easiest way to test is creating simple script

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_acl import ACL
from models import Base, Wage


# creating engine and session, creating tables in db according to models
engine = create_engine('sqlite:///db.sqlite')
Session = sessionmaker(bind=engine, query_cls=ACL.Query)
session = Session()
Base.metadata.create_all(bind=engine)

# attchaing engine to ACL, initializing ACL
ACL.setup(engine)

# example entries for db
# defining few wages and two users, and acl
# admin has access to all entries in wages table, while std_user cannot access 'Prezio`
entries = [
    # wages
    Wage(id=1, person='Prezio', amount=1000000),
    Wage(id=2, person='Programista15k', amount=15000),
    Wage(id=3, person='Programista10k', amount=10000),
    Wage(id=4, person='Praktykantka', amount=1000),

    # users
    ACL.UserModel(id=1, username='admin'),
    ACL.UserModel(id=2, username='std_user'),

    # admin access
    ACL.ACLEntry(user_id=1, dest_table='wages', dest_id=1),
    ACL.ACLEntry(user_id=1, dest_table='wages', dest_id=2),
    ACL.ACLEntry(user_id=1, dest_table='wages', dest_id=3),
    ACL.ACLEntry(user_id=1, dest_table='wages', dest_id=4),

    # std_user access
    ACL.ACLEntry(user_id=2, dest_table='wages', dest_id=2),
    ACL.ACLEntry(user_id=2, dest_table='wages', dest_id=3),
    ACL.ACLEntry(user_id=2, dest_table='wages', dest_id=4)
]
# adding entries to session and commiting to db
session.add_all(entries)
session.commit()


# retrieving admin and std_user objects
admin = session.query(ACL.UserModel).filter_by(id=1).scalar()
std_user = session.query(ACL.UserModel).filter_by(id=2).scalar()


# when user is not set empty array will be returned
print(session.query(Wage).all())
# []

# setting user before executing query
ACL.set_user(admin)
print(session.query(Wage).all())
# [<Wage 1:Prezio:1000000>, <Wage 2:Programista15k:15000>, <Wage 3:Programista10k:10000>, <Wage 4:Praktykantka:1000>]

# setting user before executing query
ACL.set_user(std_user)
print(session.query(Wage).all())
# [<Wage 2:Programista15k:15000>, <Wage 3:Programista10k:10000>, <Wage 4:Praktykantka:1000>]

# unsetting user
ACL.unset_user()
print(session.query(Wage).all())
# []

ACL.Query

ACL.Query can be used with legacy sqlalchemy project as well as with flask_sqlalchemy project. Due to different usage of those two, connecting ACL may vary, see legacy_sqlalchemy_test.py and flask_sqlalchemy_test.py.

Release files for sqlalchemy-acl-matt-kubica 0.0.1

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

Source distribution (sdist)

Source distribution for sqlalchemy-acl-matt-kubica 0.0.1
File Size Uploaded
sqlalchemy-acl-matt-kubica-0.0.1.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-acl-matt-kubica 0.0.1
File Interpreter ABI Platform
sqlalchemy_acl_matt_kubica-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.1 kB

Release files / sqlalchemy-acl-matt-kubica-0.0.1.tar.gz

Download URL sqlalchemy-acl-matt-kubica-0.0.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
954199de9a5460e21bbc6a74f4dbdf3d13317185ce63978df8092c4d7edf07fd
BLAKE2b-256 checksum
How to use checksums
0bbd9c1126fcd15f784adf0c0ee767aaa9ba2f03843a700981fba197fc8f8916
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

Release files / sqlalchemy_acl_matt_kubica-0.0.1-py3-none-any.whl

Download URL sqlalchemy_acl_matt_kubica-0.0.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c3e97939a6c026940eeacdefe48fc5c602b12cc8651647b987ba61306d5cb74
BLAKE2b-256 checksum
How to use checksums
94d9712d86b37a4aac4fd2f4d7604623e27acba5164c03dac035400e40799a3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

Release history Release notifications | RSS feed

This release

0.0.1 This release

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