django-opa-permissions
Object-level Django permissions backed by OPA
(Rego) policies stored in the database, evaluated in-process via
opa-golib-python-bindings —
no OPA server needed.
Features
- Policies as DB objects —
Policy(UUID pk, Rego source) grouped intoPolicySets via an ordered membership (a policy may belong to several sets); aPolicySetBindingmaps a Django model (ContentType) to the policyset governing it. Unbound models deny everything (superusers excepted). - Auth backend —
OpaPermissionBackendimplementsuser.has_perm("app.view_book", obj)so Django and DRF (DjangoObjectPermissions) work out of the box. Designed for subclassing: many small hook methods (extend_input,get_builtins,is_bypass,filter_result, …). - Minimal input document — policies receive only the user pk, app label,
model name, action, and (except for browse/create) the object pk. Data is
fetched from inside the policy via custom builtins (
django_opa_fetch,django_opa_fetch_old,django_opa_query) — ORM-backed, no SQL. - Browse partial evaluation —
Model.objects.for_user(user)compiles the policy'sfilterrule with the object left unknown (OPA partial evaluation → UCAST → DjangoQ/Exists), filtering listings in SQL; relation traversal and$some/$all/$nonequantifiers are supported. The authoritative full check (with the object pk) runs inallowed_for_user/has_perm. - Admin integration —
OpaModelAdminMixinwires aModelAdminto OPA: the changelist is gated by thebrowsepermission and filtered with the browse prefilter; object pages checkview/change/deleteper object. - Admin policy debugger — evaluate a policyset as any (visible) user
against any bound model/object:
print()output inline next to the policy line, per-line coverage highlighting, the full output document as a tree, and the residual browse prefilter. No trace logs.
Install
pip install django-opa-permissions
INSTALLED_APPS = [..., "django_opa_permissions"]
AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
"django_opa_permissions.backends.OpaPermissionBackend",
]
Quick start
from django_opa_permissions.managers import OpaPermissionedModel
class Book(OpaPermissionedModel):
title = models.CharField(max_length=300)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
published = models.BooleanField(default=False)
Create a PolicySet, add a Policy, and bind it to the Book content type
(all in the admin). Every policy must declare package policies; the entry
points are allow (full evaluation) and filter (browse partial evaluation):
package policies
import rego.v1
# Owners may do anything with their own books.
allow if {
obj := django_opa_fetch(input.app_label, input.model, input.object.id)
obj.owner_id == input.user.id
}
# Anyone may view or browse published books.
allow if {
input.action in {"view", "browse"}
obj := django_opa_fetch(input.app_label, input.model, input.object.id)
obj.published == true
}
# Browse prefilter: the object is UNKNOWN here — reference its columns via
# input.object; this compiles to SQL through the ORM.
filter if {
input.object.published == true
}
filter if {
input.object.owner_id == to_number(input.user.id)
}
Book.objects.for_user(user) # SQL prefilter (partial evaluation)
Book.objects.allowed_for_user(user) # prefilter + authoritative per-object pass
user.has_perm("library.view_book", book)
book.user_can(user, "change") # or "delete", custom actions, ...
modified.user_can(user, "change", old=snapshot) # policies may fetch both states
See docs/ for the input schema, builtins, filter-rule grammar (relation
paths and quantifiers), and subclassing the backend; example_project/ is a
runnable sqlite demo:
cd example_project
python manage.py migrate
python manage.py load_example_policies
python manage.py create_demo_users # alice/alice-password, bob/bob-password,
# admin/admin-password (superuser)
python manage.py runserver
License
MIT
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_opa_permissions-0.2.0.tar.gz.
File metadata
- Download URL: django_opa_permissions-0.2.0.tar.gz
- Upload date:
- Size: 45.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
645d6c47b5161ffa830091678d688bc9e2381c6a045f0290a1ac9d06cba6db7f
|
|
| MD5 |
96a8c87aafcd8cca59a9f6ff89a48a72
|
|
| BLAKE2b-256 |
5a55dd816e65946c27b688020430f81acf78a5af1eb66ea1bb34fe87e3c749b3
|
Provenance
The following attestation bundles were made for django_opa_permissions-0.2.0.tar.gz:
Publisher:
publish.yml on phi1010/django-opa-permissions
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_opa_permissions-0.2.0.tar.gz -
Subject digest:
645d6c47b5161ffa830091678d688bc9e2381c6a045f0290a1ac9d06cba6db7f - Sigstore transparency entry: 2690997702
- Sigstore integration time:
-
Permalink:
phi1010/django-opa-permissions@713b3e2f92ef25c8a7f8954cbe7d3d334c814d81 -
Branch / Tag:
refs/tags/release/v0.2.0 - Owner: https://github.com/phi1010
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@713b3e2f92ef25c8a7f8954cbe7d3d334c814d81 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_opa_permissions-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_opa_permissions-0.2.0-py3-none-any.whl
- Upload date:
- Size: 31.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b777a438822b94df98a1d8b256da50ea6faa10a77c991a7a2838c1eb4a24db63
|
|
| MD5 |
805d5e87d668278ae7d1214296e859dd
|
|
| BLAKE2b-256 |
8c65e0041a0add19122a02f20def7f854f9a8e7faec884da6e1a369047bc9e9d
|
Provenance
The following attestation bundles were made for django_opa_permissions-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on phi1010/django-opa-permissions
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_opa_permissions-0.2.0-py3-none-any.whl -
Subject digest:
b777a438822b94df98a1d8b256da50ea6faa10a77c991a7a2838c1eb4a24db63 - Sigstore transparency entry: 2690997914
- Sigstore integration time:
-
Permalink:
phi1010/django-opa-permissions@713b3e2f92ef25c8a7f8954cbe7d3d334c814d81 -
Branch / Tag:
refs/tags/release/v0.2.0 - Owner: https://github.com/phi1010
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@713b3e2f92ef25c8a7f8954cbe7d3d334c814d81 -
Trigger Event:
release
-
Statement type: