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:
- pycasbin/django-authorization: Much of the core logic and structure is adapted from this library.
- pycasbin/django-orm-adapter: The adapter implementation for Django ORM is based on this project.
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 DjangoUser) or aPolicySubjectGroup. - 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 likeGET,POST).
Policies are stored as CasbinRule instances and can be managed through the
Django admin panel.
Setup
- 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
- 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 aCasbinRulethat allows it. For example, to allow the userjohn.doeto makeGETrequests 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
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_content_access_control-0.2.0.tar.gz.
File metadata
- Download URL: django_content_access_control-0.2.0.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6baaa481c23bbfdf6d25c9bfb58c364922e77f424b3776b2702a05abde2cf01f
|
|
| MD5 |
94c5e0ded9279a2bb66794bf681803d0
|
|
| BLAKE2b-256 |
fd4dca6fc1986fe171f0fa4d32f8edd046654a0b7d85a52e7ea090e52c4e8dfb
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_content_access_control-0.2.0.tar.gz -
Subject digest:
6baaa481c23bbfdf6d25c9bfb58c364922e77f424b3776b2702a05abde2cf01f - Sigstore transparency entry: 484307216
- Sigstore integration time:
-
Permalink:
ChrisW-priv/django-content-access-control@a51819e7f753679232284b4a907bba873bf5732f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/ChrisW-priv
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@a51819e7f753679232284b4a907bba873bf5732f -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_content_access_control-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_content_access_control-0.2.0-py3-none-any.whl
- Upload date:
- Size: 18.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
628d99da8a4ce7fac53f86d063c8ee11f1d3df3c2f8d546fb150aefd46e8814e
|
|
| MD5 |
765d634a7eda4c00a98f1f4ad40e7280
|
|
| BLAKE2b-256 |
5e3b8db9a069d04f7b01fa543fcd82ef4407216f8076b36bc5fa8d925218fb33
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_content_access_control-0.2.0-py3-none-any.whl -
Subject digest:
628d99da8a4ce7fac53f86d063c8ee11f1d3df3c2f8d546fb150aefd46e8814e - Sigstore transparency entry: 484307220
- Sigstore integration time:
-
Permalink:
ChrisW-priv/django-content-access-control@a51819e7f753679232284b4a907bba873bf5732f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/ChrisW-priv
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@a51819e7f753679232284b4a907bba873bf5732f -
Trigger Event:
release
-
Statement type: