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.createorexport.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
Metadata
Release files for pyscoped-rules 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyscoped_rules-0.1.0.tar.gz | 15.4 kB | Without provenance | Details for pyscoped_rules-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyscoped_rules-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pyscoped_rules-0.1.0-py3-none-any.whl |
Total release size: 25.1 kB
Release files / pyscoped_rules-0.1.0.tar.gz
| Download URL | pyscoped_rules-0.1.0.tar.gz |
|---|---|
| Size | 15.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8c0622339331ee9aeedd6580cfb46e600ed28b231e75689d71ef83caf230f750
|
|
BLAKE2b-256 checksum How to use checksums |
81d212d09cb99b0b4baf013ba76dc0a72a4ecfb58b07dc33636246e02f8c4d89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.9
|
Release files / pyscoped_rules-0.1.0-py3-none-any.whl
| Download URL | pyscoped_rules-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1d2bf94f40c8f5d39b63e75554c58508f5d46305199f4e2e8ba485f2eb07ba68
|
|
BLAKE2b-256 checksum How to use checksums |
9372a419538a458f5dc6e850f98636f974a2711a75c2b4e13fca712bfc16c2a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.9
|
Security for pyscoped-rules 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.