Skip to main content

Reusable Django app for organization-aware roles and permissions.

Project description


# django-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

```bash
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.2.0.tar.gz (8.7 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.2.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_org_authz-0.2.0.tar.gz
  • Upload date:
  • Size: 8.7 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.2.0.tar.gz
Algorithm Hash digest
SHA256 60ce077313886ceeafcb810183bafa43617d780655a138b69398ddd6938f972b
MD5 6aeff81e0397d86d83ec3428b32dabfa
BLAKE2b-256 cd90cf3ff68e4841f31c3b52cafa75fb83955f7dad782684aeb9a4e41b491b28

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_org_authz-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2e764754d0a4f30f714c97b43194aae724751736f3af84cf78069e37894764d
MD5 435b6fca6f9cb397892a7ddf4a26dc14
BLAKE2b-256 7c3b046c3f496fc78a82194754ddbc8680fae28accc0793e3ac8e3561506dd78

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