Skip to main content

A Django package for managing URL-based permissions through user groups with HTTP method support

Project description

Django URL Permissions

A Django package that provides a flexible and efficient way to manage URL-based permissions through Django's user groups. This package allows you to control access to specific URLs based on user group membership and HTTP methods.

Features

  • 🔒 Control access to URLs based on user groups
  • 🌐 Support for all HTTP methods (GET, POST, PUT, PATCH, DELETE, etc.)
  • ⚡ Option to grant all-method access with a single permission
  • 🔌 Easy integration through middleware
  • ⚙️ Configurable exempt URLs
  • 🐍 Support for Django 3.2+ and Python 3.7+
  • 👨‍💼 Built-in admin interface integration
  • 🚀 Efficient database querying with proper indexing

Installation

Install the package using pip:

pip install django-url-group-permissions

Quick Start

  1. Add 'django_url_group_permissions' to your INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'django_url_group_permissions',
]
  1. Add the middleware to your settings:
MIDDLEWARE = [
    ...
    'django_url_group_permissions.middleware.UrlPermissionMiddleware',
]
  1. Configure optional settings:
# URLs that don't require permission checks
URL_PERMISSION_EXEMPT_URLS = [
    '/admin/',
    '/login/',
    '/static/',
    '/media/',
]

# Global switch to enable/disable permission checks
URL_PERMISSION_REQUIRED = True
  1. Run migrations:
python manage.py migrate

Usage

There are two ways to implement URL permissions in your project:

  1. Global Permission Check (Recommended for new projects)

    Set in your settings.py:

    URL_PERMISSION_CHECK_ALL_VIEWS = True
    

    This will enforce URL permissions on all views automatically (except exempt URLs).

  2. Decorator Approach (For selective permission checking)

    If URL_PERMISSION_CHECK_ALL_VIEWS = False, you can use the decorator to specify which views require URL permissions:

    from django_url_group_permissions import url_permission_required
    from django.contrib.auth.decorators import login_required
    from django.shortcuts import render
    
    @url_permission_required
    @login_required
    def my_view(request):
        return render(request, 'my_template.html')
    

Note: The order of decorators matters. @url_permission_required should be placed before @login_required to ensure the user is authenticated before checking URL permissions.

In both cases, you'll need to configure the permissions for each group through the Django admin interface.

Managing URL Permissions

URL permissions are managed through the Django admin interface, similar to model permissions:

  1. Go to Django admin (/admin/)
  2. Navigate to "Groups"
  3. Create or edit a group
  4. In the group edit page, you'll find a "URL Permissions" section below the standard model permissions
  5. Use the interface to:
    • View available URL permissions
    • Add/remove URL permissions for different HTTP methods
    • Filter URLs using the search box
    • Choose multiple permissions at once

Users will only be able to access URLs that their groups have been granted permission to access.

Supported HTTP Methods

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
  • HEAD
  • OPTIONS
  • ALL (special permission that grants access to all methods)

Configuration Options

Setting Type Default Description
URL_PERMISSION_REQUIRED bool True Global switch to enable/disable permission checks
URL_PERMISSION_EXEMPT_URLS list [] List of URL prefixes that bypass permission checks
URL_PERMISSION_CHECK_ALL_VIEWS bool False If True, all views require URL permissions unless exempt. If False, only views with @url_permission_required decorator are checked

Model Fields

Field Type Description
group ForeignKey The Django group this permission applies to
url CharField The URL pattern this permission controls
http_method CharField The HTTP method or 'ALL'
is_active BooleanField Whether this permission is currently active
description TextField Optional description of the permission
created_at DateTimeField When the permission was created
updated_at DateTimeField When the permission was last updated

Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you encounter any problems or have questions, please:

  1. Check the GitHub Issues for existing problems or solutions
  2. Create a new issue if your problem is not yet reported

Changelog

1.0.0 (Initial Release)

  • Basic URL permission functionality
  • Group-based permission management
  • HTTP method support
  • Middleware implementation
  • Admin interface integration

Authors

Acknowledgments

  • Thanks to the Django community for the amazing framework
  • Inspired by the need for flexible URL-based permissions in Django applications

Settings

  • URL_PERMISSION_REQUIRED: Enable/disable URL permission checking globally (default: True)
  • URL_PERMISSION_EXEMPT_URLS: List of URL prefixes to exclude from permission checking (default: [])
  • URL_PERMISSION_CHECK_ALL_VIEWS: If True, all views require URL permissions unless exempt. If False, only views with @url_permission_required decorator are checked (default: False)

Example:

# settings.py

# Check permissions for all views
URL_PERMISSION_CHECK_ALL_VIEWS = True

# Exempt certain URLs from permission checking
URL_PERMISSION_EXEMPT_URLS = [
    '/admin/',
    '/login/',
    '/public/',
]

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_url_group_permissions-1.0.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

django_url_group_permissions-1.0.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file django_url_group_permissions-1.0.1.tar.gz.

File metadata

File hashes

Hashes for django_url_group_permissions-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b38b6e824f85ce5deebcfb99cd3a9bd90ffb19749d4d6cb68309d42fe96add31
MD5 3aee13c34ec93bef2278bea119d318bd
BLAKE2b-256 3ae87a9311471a249d79b55d20360bd5969ee2a84d9b59f8fb986ca83d9918a6

See more details on using hashes here.

File details

Details for the file django_url_group_permissions-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_url_group_permissions-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 96907a6d3b84a453e161c736a0f04e4ae763ff1a71f71a1968687989a4a2c294
MD5 1ee313e6c0c30475d718cbed667d4a37
BLAKE2b-256 99f82f46b137d5b1b33d72418c61830c9f3d34b816809920ae17e15c8a27b5b5

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