Skip to main content

No project description provided

Project description

Content Access Control

[!NOTE] Acknowledgements & License Notice

This project utilizes the pycasbin library and is heavily inspired by and incorporates code from the following open-source projects, which are distributed under the Apache 2.0 License:

We are grateful to the original authors for their work. In accordance with the Apache 2.0 License, we acknowledge that significant portions of this codebase are derived from their efforts. You can view the full license here.

This Django app provides a flexible and powerful way to manage fine-grained access control for your models and API endpoints using pycasbin. It allows you to define permissions based on subjects (users or groups), resources (any Django model instance or a URL), and actions.

Key Concepts

  • Subject: Represents who is requesting access. This can be a PolicySubject (linked to a Django User) or a PolicySubjectGroup.
  • Resource: Represents what is being accessed. This can be any Django model instance or a URL path.
  • Action: Represents the operation being performed (e.g., read, write, delete, or an HTTP method like GET, POST).

Policies are stored as CasbinRule instances and can be managed through the Django admin panel.

Setup

  1. Install the app:
pip install django-content-access-control

Then, add django_content_access_control to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # ...
    "django_content_access_control",
    # ...
]

Do not forget to run migrations:

python manage.py migrate
  1. Configure Casbin:

In your settings.py, you need to specify the path to your Casbin model configuration file.

# settings.py
CASBIN_MODEL = str(BASE_DIR / "casbin_model.conf")

An example casbin_model.conf files are in the model_examples/ directory.

3.Protect DRF Endpoints (Optional): To automatically protect your Django Rest Framework views, add SubjectHasUrlPermission to your DEFAULT_PERMISSION_CLASSES.

# settings.py
REST_FRAMEWORK = {
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
        "content_access_control.permissions.SubjectHasUrlPermission",
    ],
    # ...
}

This permission class will check if the current subject has the right to perform the request's method on the request's path.

4.Enable Subject Switching (Optional): If you want to allow users to act as different "subjects" (e.g., personas with different permissions), you can use the PolicySubjectMiddleware. Add it to your MIDDLEWARE settings.

# settings.py
MIDDLEWARE = [
    # ...
    "content_access_control.middleware.PolicySubjectMiddleware",
    # ...
]

When this middleware is active, an authenticated user can specify a subject to act as by sending the X-Policy-Subject-Act-As header with the name of one of their PolicySubjects.

Usage

Defining Permissions for Models

To manage permissions for a specific Django model, you need to register it in the admin panel. This creates a user-friendly interface for creating, viewing, and deleting access rules for instances of that model.

In the admin.py of one of your apps, use the register_permission_admin function.

Examples

Single Action

Registering the Feature model with a single "access" action.

from django.contrib import admin
from .models import Feature
from content_access_control.admin_permission import register_permission_admin

@admin.register(Feature)
class FeatureAdmin(admin.ModelAdmin):
    ...

register_permission_admin(Feature, ["access"])
Multiple Actions

Registering the Chunk model with multiple actions.

from django.contrib import admin
from .models import Chunk
from content_access_control.admin_permission import register_permission_admin

@admin.register(Chunk)
class ChunkAdmin(admin.ModelAdmin):
    ...

register_permission_admin(Chunk, ["create", "read", "update", "delete"])

After registering, a new section for "Chunk Content Access Permission" will appear in the admin panel, allowing you to grant subjects (like PolicySubject or PolicySubjectGroup) specific actions on Chunk objects.

Enforcing Permissions

  • For DRF Views: If you've set up SubjectHasUrlPermission, enforcement is automatic. To grant a user access to an endpoint, you need to create a CasbinRule that allows it. For example, to allow the user john.doe to make GET requests to /api/chunks/, you would create a policy rule: p, john.doe, /api/chunks/, GET. This can be done via the "Casbin Rules" section in the admin panel.

  • For Model Instances: The permissions you define using the dynamically created admin panels (e.g., "Feature Content Access Permission") create policies that link subjects to specific model instances. You can integrate Pycasbin with Django authentication system. To enable the backend, you need to specify it in settings.py.

AUTHENTICATION_BACKENDS = [
  "dauthz.backends.CasbinBackend",
  "django.contrib.auth.backends.ModelBackend",
]

Customizing Admin Widgets

You can customize the widgets used for selecting subjects and resources in the permission admin forms. This is useful for integrating with libraries like django-select2 to provide autocomplete fields for large datasets.

Example of passing a custom widget:

from django.urls import reverse_lazy
from django_select2.forms import Select2Widget
from content_access_control.admin_permission import register_permission_admin
from .models import Feature

register_permission_admin(
    Feature,
    ['access'],
    subject_widget=Select2Widget(
        attrs={
            'data-ajax--url': reverse_lazy('subject_autocomplete'),
            'data-ajax--cache': 'true',
            'data-minimum-input-length': '1',
        }
    ),
    resource_widget=Select2Widget(
        attrs={
            'data-ajax--url': reverse_lazy('resource_autocomplete'),
            'data-ajax--cache': 'true',
            'data-minimum-input-length': '1',
        }
    ),
)

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_content_access_control-0.2.0.tar.gz (16.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_content_access_control-0.2.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_content_access_control-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6baaa481c23bbfdf6d25c9bfb58c364922e77f424b3776b2702a05abde2cf01f
MD5 94c5e0ded9279a2bb66794bf681803d0
BLAKE2b-256 fd4dca6fc1986fe171f0fa4d32f8edd046654a0b7d85a52e7ea090e52c4e8dfb

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_content_access_control-0.2.0.tar.gz:

Publisher: python-package.yml on ChrisW-priv/django-content-access-control

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for django_content_access_control-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 628d99da8a4ce7fac53f86d063c8ee11f1d3df3c2f8d546fb150aefd46e8814e
MD5 765d634a7eda4c00a98f1f4ad40e7280
BLAKE2b-256 5e3b8db9a069d04f7b01fa543fcd82ef4407216f8076b36bc5fa8d925218fb33

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_content_access_control-0.2.0-py3-none-any.whl:

Publisher: python-package.yml on ChrisW-priv/django-content-access-control

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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