Skip to main content

Roles, permissions, entitlements, and limits for scoped SaaS authorization.

Project description

pyrule

pyrule is a framework-agnostic authorization core for SaaS products that need roles, permissions, subscription entitlements, usage limits, and conditional rules in one decision path.

It is designed to answer one question:

decision = engine.check(actor=user, action="project.create", scope=tenant)

The result is a structured Decision, not only a boolean, so application code can distinguish missing role permissions from upgrade prompts, quota failures, and rule denials.

Install

From a checkout:

python3 -m pip install -e .

YAML matrix files are optional:

python3 -m pip install -e ".[yaml]"

Quick Start

from pyrule import AuthorizationEngine, Matrix

matrix = Matrix.from_dict({
    "permissions": {
        "project.create": {"limit": "projects.max"},
        "project.read": {},
        "project.update": {},
        "export.csv": {"feature": "export.csv"},
    },
    "roles": {
        "admin": {"grants": ["project.*", "export.csv"]},
        "viewer": {"grants": ["project.read"]},
    },
    "plans": {
        "free": {
            "features": {"export.csv": False},
            "limits": {"projects.max": 3},
        },
        "pro": {
            "features": {"export.csv": True},
            "limits": {"projects.max": 50},
        },
    },
    "rules": [
        {
            "name": "project_scope_required",
            "action": "project.*",
            "require": "resource_in_scope",
        },
    ],
})

engine = AuthorizationEngine(
    matrix=matrix,
    role_provider=lambda actor, scope: ["admin"],
    plan_provider=lambda scope: "pro",
    usage_provider=lambda key, actor, scope, resource, context: 12,
    predicates={
        "resource_in_scope": lambda actor, scope, resource, context: (
            resource is None or resource.tenant_id == scope.id
        ),
    },
)

decision = engine.check(user, "project.create", scope=tenant)
assert decision.allowed

Documentation

Core Ideas

  • Permissions describe actions such as project.create or export.csv.
  • Roles grant permissions to actors inside a scope.
  • Plans grant subscription features and numeric limits to a tenant or account.
  • Rules apply host-application predicates after the basic role, feature, and limit checks pass.
  • Decisions keep structured denial reasons for UI, logs, and audits.

Runtime Dependencies

The core package uses the Python standard library. YAML support is optional through pyrule[yaml].

Testing

PYTHONDONTWRITEBYTECODE=1 PYTHONPATH=src python3 -m unittest discover -s tests

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

pyscoped_rules-0.1.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

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

pyscoped_rules-0.1.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyscoped_rules-0.1.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for pyscoped_rules-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8c0622339331ee9aeedd6580cfb46e600ed28b231e75689d71ef83caf230f750
MD5 7b84acbe9f2bb2523f599bc466aaebab
BLAKE2b-256 81d212d09cb99b0b4baf013ba76dc0a72a4ecfb58b07dc33636246e02f8c4d89

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyscoped_rules-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for pyscoped_rules-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d2bf94f40c8f5d39b63e75554c58508f5d46305199f4e2e8ba485f2eb07ba68
MD5 5870b23c1b2e375aadf889ca742ef112
BLAKE2b-256 9372a419538a458f5dc6e850f98636f974a2711a75c2b4e13fca712bfc16c2a7

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