A simple, light-weight privilege checker decorator for Django Ninja APIs.
Project description
django-ninja-rbac
A lightweight RBAC (Role-Based Access Control) decorator for Django Ninja APIs.
Protect your endpoints using simple privilege strings with support for wildcard permissions.
Installation
pip install django-ninja-rbac
Quick Start
Attach permissions to your authenticated user:
request.user.permissions = [
"customers:view",
"customers:update",
]
Protect your Django Ninja endpoints:
from ninja_rbac import require_permissions
@api.get("/customers")
@require_permissions(["customers:view"])
def get_customers(request):
return {"success": True}
If the user does not have the required permission, a 403 Forbidden response is returned:
{
"message": "You are not authorized to perform this action"
}
Multiple Permissions
By default, the user must have at least one of the specified permissions.
@require_permissions([
"customers:view",
"customers:update",
])
def endpoint(request):
...
Equivalent to:
customers:view OR customers:update
Require All Permissions
Set require_all=True to require every permission.
@require_permissions(
[
"customers:view",
"customers:update",
],
require_all=True,
)
def endpoint(request):
...
Equivalent to:
customers:view AND customers:update
Wildcard Permissions
Resource Wildcards
user.permissions = [
"customers:*"
]
Allows:
customers:view
customers:create
customers:update
customers:delete
Global Wildcard
user.permissions = [
"*:*"
]
Allows access to all resources and actions.
Permission Examples
customers:view
customers:create
customers:update
customers:delete
orders:view
orders:create
orders:update
orders:delete
customers:*
orders:*
*:*
How It Works
Given a required permission:
customers:view
The package checks the user's permissions in the following order:
customers:view
customers:*
*:*
The first matching permission grants access.
Requirements
- Python 3.11+
- Django Ninja 1.5+
License
MIT
Project details
Release history Release notifications | RSS feed
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_ninja_rbac-1.0.0.tar.gz.
File metadata
- Download URL: django_ninja_rbac-1.0.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f854ebb145b22e966cbe907c67550269f90d73f18aed23da3b52881ace1ea556
|
|
| MD5 |
49366dfe46e2c82886a0612821d834af
|
|
| BLAKE2b-256 |
f3d2f50fbc4a6069255fe9de4160cfeea13d19918e03a694961f2d65ae4a9b8a
|
File details
Details for the file django_ninja_rbac-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_ninja_rbac-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
112a6c1233e2959c9c519678a4a36a8345d0a27bbe2e6a0ddc8b65eab41bbc12
|
|
| MD5 |
694cee3b53f9a14fa833259954a59562
|
|
| BLAKE2b-256 |
304d5045bc24c0e8273d3898d029ddb329900ef761ff7b7fb10990520c978146
|