Skip to main content

Fine-grained hierarchical RBAC for Litestar applications

Project description

litestar-permissions

CI PyPI version Python versions License: MIT

Fine-grained hierarchical RBAC for Litestar applications.

Roles are scoped to resources and permissions inherit upward through the hierarchy. An org-admin role on Organization X grants those permissions on every project and application inside it. Built on SQLAlchemy 2.x, ships as a Litestar plugin.

Installation

uv add litestar-permissions

Quick Start

from litestar import Litestar, get
from litestar_permissions import PermissionsPlugin, PermissionsConfig, require_permission
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


config = PermissionsConfig(
    hierarchy={"application": "project", "project": "organization"},
)


@get(
    "/apps/{app_id:str}/deploy",
    guards=[require_permission("application:deploy", resource_id_param="app_id")],
)
async def deploy(app_id: str) -> dict:
    return {"status": "deploying"}


app = Litestar(
    route_handlers=[deploy],
    plugins=[PermissionsPlugin(config=config, base=Base)],
)

The plugin generates four SQLAlchemy tables (roles, permissions, role_permissions, user_role_assignments) bound to your Base. Guards check permissions at the resource scope, walking up the hierarchy to find matching role assignments.

How the Hierarchy Works

Define parent-child relationships between resource types:

PermissionsConfig(
    hierarchy={
        "application": "project",   # application's parent is project
        "project": "organization",   # project's parent is organization
    }
)

When checking application:deploy on a specific app, the resolver checks role assignments scoped to that app, then its parent project, then the parent org, then global. A match at any level grants the permission.

Development

make dev           # install with all dev deps
make test          # run tests
make lint          # run prek hooks (ruff, ty, codespell)
make docs-serve    # live-reload docs on localhost:8001

Documentation

Full docs at jacobcoffee.github.io/litestar-permissions.

License

MIT - see 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

litestar_permissions-0.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

litestar_permissions-0.1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file litestar_permissions-0.1.0.tar.gz.

File metadata

  • Download URL: litestar_permissions-0.1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for litestar_permissions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 00111816032e80a24192e377cfbb52318ca9100f33cc08f2861cfce00274b32a
MD5 5fe8770f0912c42fb4afa398c5a5f5bd
BLAKE2b-256 919ad8fe22cf0d6c65e5aaa47f8ccb3b7c683126212568b5c8b93b83fd6c88c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_permissions-0.1.0.tar.gz:

Publisher: publish.yml on JacobCoffee/litestar-permissions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file litestar_permissions-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for litestar_permissions-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0fb27a2210c59286fca5069260d15ae72e855972926778940531222eaa23c2e8
MD5 ac6530bb6c44f1fed6811d1279bef1f4
BLAKE2b-256 cb5a0f58d488017b903d99e61d1db8a2d7ff90e460ff72fcd19b5047f8d08f3e

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_permissions-0.1.0-py3-none-any.whl:

Publisher: publish.yml on JacobCoffee/litestar-permissions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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