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.8.tar.gz (3.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.8-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_ninja_shield-0.0.8.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.1 Linux/6.11.0-1018-azure

File hashes

Hashes for django_ninja_shield-0.0.8.tar.gz
Algorithm Hash digest
SHA256 4028d952b11e19d6bc013b4c967a7a737e8166c4fd2d4cc085e0429bdb08f8c9
MD5 21751a132dcae9beae648bf9fd135dda
BLAKE2b-256 4419a373ddaa66569370296f7a9f1a8abd03a083ac655f1cc1a94525b80aa890

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_ninja_shield-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.1 Linux/6.11.0-1018-azure

File hashes

Hashes for django_ninja_shield-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 2b59a7cdb2b3e1898489827e40babb9d59b44d678c5ea02065c1966544034b7e
MD5 6a3d269b00db94110fefbc54bd6002b9
BLAKE2b-256 00fe1fe8123790e53e93028b56483c8d979373048be088c2d7ecdfac693161ad

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