Mixins API Reference
mixins
Classes
RebacViewMixin
Structure-agnostic mixin for DRF Views to enforce ReBAC Authorization.
This mixin automatically handles three critical authorization lifecycle hooks in Django REST Framework without requiring manual permission logic:
- Queryset Filtering (
get_queryset): Injectsid__infilters on list views. - Parent Verification (
check_permissions): Checks required parent roles on POST requests. - Object Verification (
check_object_permissions): Checks specific object roles on PUT/PATCH/DELETE.
Attributes:
| Name | Type | Description |
|---|---|---|
rebac_config |
RebacViewConfig | None
|
The strict configuration class defining the authorization rules
for this view. Must be an instance of |
Example
from rest_framework import viewsets
from rebac_data_sync.structs import RebacViewConfig
class DocumentViewSet(RebacViewMixin, viewsets.ModelViewSet):
queryset = Document.objects.all()
serializer_class = DocumentSerializer
rebac_config = RebacViewConfig(
object_type="document",
read_relation="can_read_document",
update_relation="can_update",
delete_relation="can_delete"
)
Raises:
| Type | Description |
|---|---|
ImproperlyConfigured
|
If |
AuthenticationFailed
|
If the Traefik identity header is missing. |
PermissionDenied
|
If the OpenFGA network check denies access. |
Source code in src/rebac/views/mixins.py
Functions
get_serializer_context
DRF hook: Injects ReBAC tools directly into the serializer context.
Source code in src/rebac/views/mixins.py
Functions
mixins
Classes
RebacModelSyncMixin
Structure-agnostic mixin for synchronizing Django models to the ReBAC store via the Outbox pattern.
This mixin intercepts the standard Django save() and delete() lifecycles.
It utilizes the defined RebacModelConfig to calculate the exact ReBAC tuple
differences (diffs) and safely queues them in the local database transaction.
Attributes:
| Name | Type | Description |
|---|---|---|
rebac_config |
RebacModelConfig | None
|
The strict configuration class defining how this model maps
to the OpenFGA graph. Must be an instance of |
pk |
int | str | UUID | None
|
The primary key of the model instance. |
Example
Basic Creator Ownership:
from django.db import models
from rebac.structs import RebacModelConfig, RebacCreatorConfig
class Document(RebacModelSyncMixin, models.Model):
title = models.CharField(max_length=255)
creator_id = models.CharField(max_length=255)
rebac_config = RebacModelConfig(
object_type="document",
creators=[
RebacCreatorConfig(
relation="editor",
local_field="creator_id"
)
]
)
Parent Hierarchies (Cascading):
from rebac.structs import RebacParentConfig
class Folder(RebacModelSyncMixin, models.Model):
name = models.CharField(max_length=255)
org_id = models.CharField(max_length=255)
creator_id = models.CharField(max_length=255)
rebac_config = RebacModelConfig(
object_type="folder",
parents=[
RebacParentConfig(
relation="organization",
parent_type="organization",
local_field="org_id"
)
],
creators=[
RebacCreatorConfig(
relation="owner",
local_field="creator_id"
)
]
)
Custom Role Assignment (Escape Hatch):
If you need to assign ReBAC roles based on dynamic data state (like a boolean field),
you can intercept save() and manually queue tuples into the Outbox:
from rebac.models import RebacSyncOutbox
class Article(RebacModelSyncMixin, models.Model):
title = models.CharField(max_length=255)
is_public = models.BooleanField(default=False)
rebac_config = RebacModelConfig(object_type="article")
def save(self, *args, **kwargs):
# 1. Let the mixin handle the standard config-based tuples first
super().save(*args, **kwargs)
# 2. Inject your custom, dynamic logic
if self.is_public:
self._queue_outbox(
action=RebacSyncOutbox.Action.WRITE,
t={
"user": "user:*", # OpenFGA wildcard for 'everyone'
"relation": "viewer",
"object": f"article:{self.pk}"
}
)
Notes
Limitations:
Because this mixin relies on intercepting the save() method for the
Transactional Outbox pattern, standard Django bulk operations
(e.g., Document.objects.bulk_create()) will bypass this mixin.
You must save instances individually or trigger the outbox manually
for bulk operations.
Source code in src/rebac/models/mixins.py
Functions
__init_subclass__
classmethod
Metaprogramming Hook: Executed automatically when a Django model inherits from this mixin. Wires up framework-level safety nets.
Source code in src/rebac/models/mixins.py
delete
Standard instance deletion override.
Source code in src/rebac/models/mixins.py
👉 See the Model Syncing Guide for full tutorials on Multi-Parent and Multi-Creator architectures.