Skip to main content

Reusable Django app for organization-aware roles and permissions.

Project description

WHAT IS org-authz ?

Reusable Django app for organization-aware roles and permissions (RBAC) with automatic multi-tenant filtering.

It provides:

  • ✅ Organization, Role, Permission, and UserRole models
  • OrgAuthzViewSet mixin that filters data by the user’s organization(s)
  • ✅ A simple @org_permission_required decorator for protecting views
  • ✅ Plug-and-play integration with any Django REST API

🚀 Installation

pip install django-org-authz

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...,
    "rest_framework",
    "org_authz",
]

Run migrations:

python manage.py migrate

🧩 Quick Start

1. Define your Organization (optional)

You can use the built-in Organization model or subclass it.

# pharmacy/models.py
from org_authz.models import Organization

class Pharmacy(Organization):
    license_number = models.CharField(max_length=100, unique=True)

2. Create a model linked (directly or indirectly) to your organization

class Invoice(models.Model):
    pharmacy = models.ForeignKey(Pharmacy, on_delete=models.CASCADE)
    total = models.DecimalField(max_digits=10, decimal_places=2)

3. Use the provided OrgAuthzViewSet mixin

It automatically filters results based on the organizations the authenticated user is allowed to access.

# pharmacy/views.py
from org_authz.views.mixins import OrgAuthzViewSet
from .models import Invoice
from .serializers import InvoiceSerializer

class InvoiceViewSet(OrgAuthzViewSet):
    queryset = Invoice.objects.all()
    serializer_class = InvoiceSerializer

💡 Works even for nested relationships like:

Invoice → Pharmacy → Organization

4. Register your routes

# pharmacy/urls.py
from rest_framework.routers import DefaultRouter
from .views import InvoiceViewSet

router = DefaultRouter()
router.register("invoices", InvoiceViewSet)

urlpatterns = router.urls

5. Protect views using @org_permission_required

from org_authz.decorators import org_permission_required

@org_permission_required("can_add_invoice")
def create_invoice(request):
    ...

🧠 Use Cases

Use Case Description
🏥 Pharmacies Each pharmacy has its own roles, permissions, and users. Staff can only access their pharmacy’s data.
🏨 Hotels Managers, receptionists, and cleaners have role-based access, isolated per hotel branch.
🚗 Manufacturing (e.g. Tesla) Different factories or branches act as organizations, each managing its own data securely.
🏫 Schools Teachers, admins, and students belong to their specific school organizations.

⚙️ How It Works

  • Users are linked to organizations via the UserRole model.
  • Each role has assigned Permission objects.
  • OrgAuthzViewSet introspects your model to find its organization relationship (direct or nested) and filters automatically.
  • You can define your own Organization subclass per business domain.

🧱 Models Overview

Model Purpose
Organization Represents a company, branch, or tenant.
Role Defines what a user can do within an organization.
Permission Represents an action (e.g. can_add_invoice).
UserRole Connects users to roles and organizations.

🧪 Example Project Structure

myproject/
├── pharmacy/
│   ├── models.py
│   ├── views.py
│   ├── serializers.py
│   └── urls.py
├── org_authz/
│   └── (installed app)
└── settings.py

🪄 Example: Multi-organization setup

If your project has multiple organization types:

class Hotel(Organization):
    city = models.CharField(max_length=100)

class Booking(models.Model):
    hotel = models.ForeignKey(Hotel, on_delete=models.CASCADE)
    guest_name = models.CharField(max_length=255)

Then:

class BookingViewSet(OrgAuthzViewSet):
    queryset = Booking.objects.all()
    serializer_class = BookingSerializer

OrgAuthzViewSet will automatically detect:

Booking → hotel → organization

and apply filtering to only return bookings for the hotels the current user has access to.


📜 License

MIT © Victor Mwendwa


🌐 Links

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

django_org_authz-0.3.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

django_org_authz-0.3.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file django_org_authz-0.3.0.tar.gz.

File metadata

  • Download URL: django_org_authz-0.3.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for django_org_authz-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8557339f35a57379856a23a6c32d0f05a3a4c641d63709b3c1f796097d83ad6f
MD5 3c8fce9332494cbbdab16ffe6010bf66
BLAKE2b-256 64654e11ebde2c9b0932888e46609883993f8725403363e3c2b0f704fde0a118

See more details on using hashes here.

File details

Details for the file django_org_authz-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_org_authz-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 94e07c36d5499b7e27ae188c8bbc250b1b1ee4079e57f639e7cbbd1dfcf5344c
MD5 173968913829a47548015c459b30bbb3
BLAKE2b-256 124e3eb9b79bae64df33ac1ddd6338cdf1390027e8c03e4276995bef8b06cdcd

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