Skip to main content

A python package to control permissions for django ninja.

Project description

Django Ninja Shield

A powerful and flexible permissions control package for Django Ninja. This package allows you to easily manage and combine permissions for your Django Ninja API endpoints using a clean and intuitive syntax.

Features

  • 🛡️ Simple permission decorators for Django Ninja endpoints
  • ⚡ Combine permissions using logical operations (AND, OR, NOT)
  • 🔒 Built on top of Django's permission system
  • 🚀 Type-safe with full typing support

Installation

You can install the package using pip:

pip install django-ninja-shield

Or if you're using Poetry:

poetry add django-ninja-shield

Basic Usage

Here's a simple example of how to use django-ninja-shield:

from django_ninja_shield import requires_permissions
from ninja import Router

router = Router()

@router.get("/articles")
@requires_permissions("articles.view_article")
def get_articles(request):
    return {"message": "You have permission to view articles"}

Advanced Usage

Combining Permissions

You can combine multiple permissions using logical operators:

from django_ninja_shield import requires_permissions, P

router = Router()

# Using AND operator
@router.post("/articles")
@requires_permissions(P("articles.add_article") & P("articles.change_article"))
def create_article(request):
    return {"message": "You have permission to create articles"}

# Using OR operator
@router.get("/dashboard")
@requires_permissions(P("admin.view_dashboard") | P("staff.view_dashboard"))
def view_dashboard(request):
    return {"message": "You have permission to view the dashboard"}

# Using NOT operator
@router.get("/public")
@requires_permissions(~P("articles.is_restricted"))
def public_view(request):
    return {"message": "This is a public view"}

Complex Permission Combinations

You can create complex permission rules by combining multiple operations:

from django_ninja_shield import requires_permissions, P, IsAdmin, IsUseruser, IsStaff, IsActive

@router.put("/articles/{article_id}")
@requires_permissions(
    (P("articles.change_article") & P("articles.view_article")) |
    IsAdmin() # or: IsStaff(), IsSuperuser, IsActive
)
def update_article(request, article_id: int):
    return {"message": f"Article {article_id} updated"}

Response Format

When permission is denied, the API returns a 403 Forbidden response with the following format:

{
  "detail": "Permission denied"
}

Requirements

  • Python ≥ 3.10
  • Django ≥ 4.0.0

License

This project is licensed under the MIT License.

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_ninja_shield-0.0.4.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

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

django_ninja_shield-0.0.4-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file django_ninja_shield-0.0.4.tar.gz.

File metadata

  • Download URL: django_ninja_shield-0.0.4.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.1 Linux/6.8.0-1021-azure

File hashes

Hashes for django_ninja_shield-0.0.4.tar.gz
Algorithm Hash digest
SHA256 4826c38d1e0c73eaa93328ff3781f792ef8ad839766cbe32f91c3ef8ecb2b16a
MD5 9f0b464c20163ee49b098910208e3e83
BLAKE2b-256 0c5373797417a2f404ed66e1ed2cd1d52fd38b4701c8ffced0e4a57d02068617

See more details on using hashes here.

File details

Details for the file django_ninja_shield-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: django_ninja_shield-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.1 Linux/6.8.0-1021-azure

File hashes

Hashes for django_ninja_shield-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7f7141a665a613de5a2552c50209c4b67b210bc5c996aabc4b569616f39908b2
MD5 66b1a93b432a87d2d7a671aa4eda90ab
BLAKE2b-256 178031216b290951c56d07dbc3f340a678f6cfafaa1332d3b22b6a985caf3c2a

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