Skip to main content

A flexible RBAC system for Django

Project description

Django Ultra RBAC

A flexible and powerful Role-Based Access Control (RBAC) system for Django applications.

PyPI version Python versions Django versions License

Features

  • Flexible role and permission management
  • Separate Role and RolePermission models for better control
  • Global permission checking function
  • Built-in caching for improved performance
  • Decorators for view-level permission control
  • Django admin integration
  • Resource-level permissions support
  • Comprehensive test suite

Installation

pip install django-ultra-rbac

Add 'django_ultra_rbac' to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    ...
    'django_ultra_rbac',
]

Run migrations:

python manage.py migrate

Quick Start

  1. Create permissions:
from django_ultra_rbac.models import Permission

permission = Permission.objects.create(
    name='Can Edit Posts',
    codename='can_edit_post',
    description='Allows user to edit blog posts'
)
  1. Create roles:
from django_ultra_rbac.models import Role, RolePermission

role = Role.objects.create(
    name='Editor',
    description='Content editor role'
)

RolePermission.objects.create(
    role=role,
    permission=permission
)
  1. Assign roles to users:
from django_ultra_rbac.models import UserRole

UserRole.objects.create(
    user=user,
    role=role
)
  1. Check permissions:
from django_ultra_rbac.utils import has_permission

if has_permission(user, 'can_edit_post'):
    # User has permission
    pass

Usage Examples

View Decorator

from django_ultra_rbac.decorators import permission_required

@permission_required('can_view_dashboard')
def dashboard(request):
    return render(request, 'dashboard.html')

Template Usage

{% load rbac_tags %}

{% if request.user|has_permission:'can_edit_users' %}
    <a href="{% url 'edit_users' %}">Edit Users</a>
{% endif %}

Global Permission Checking

from django_ultra_rbac.utils import has_permission, clear_permission_cache

# Check permission
if has_permission(user, 'can_access_admin'):
    # Allow access
    pass

# Clear cache when permissions change
clear_permission_cache(user)

Models

Permission

  • name: CharField
  • codename: CharField (unique)
  • description: TextField

Role

  • name: CharField
  • description: TextField
  • is_active: BooleanField
  • created_at: DateTimeField
  • updated_at: DateTimeField

RolePermission

  • role: ForeignKey(Role)
  • permission: ForeignKey(Permission)
  • is_active: BooleanField
  • created_at: DateTimeField
  • updated_at: DateTimeField

UserRole

  • user: ForeignKey(User)
  • role: ForeignKey(Role)
  • is_active: BooleanField
  • created_at: DateTimeField
  • updated_at: DateTimeField

ResourcePermission

  • permission: ForeignKey(Permission)
  • content_type: ForeignKey(ContentType)
  • object_id: PositiveIntegerField
  • content_object: GenericForeignKey
  • is_active: BooleanField

Testing

# Install development dependencies
pip install -e ".[test]"

# Run tests
pytest

# Run tests with coverage
coverage run -m pytest
coverage report

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add 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 documentation
  2. Look for existing issues or create a new one
  3. Submit detailed bug reports with error messages and minimal reproducible examples

Changelog

[0.1.0] - 2024-12-22

  • Initial release
  • Basic RBAC functionality
  • Django admin integration
  • Permission caching
  • View decorators
  • Template tags

Roadmap

  • Role hierarchy support
  • API endpoints for permission management
  • Improved caching mechanisms
  • GraphQL support
  • Django REST framework integration
  • Bulk permission management

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_ultra_rbac-0.1.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

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

django_ultra_rbac-0.1.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file django_ultra_rbac-0.1.0.tar.gz.

File metadata

  • Download URL: django_ultra_rbac-0.1.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.0

File hashes

Hashes for django_ultra_rbac-0.1.0.tar.gz
Algorithm Hash digest
SHA256 661a86f25153983bd7bde5b66df0b959012c25f850c7814bf2384f698b04dad7
MD5 f1ed771dde2922a80103190ce854c793
BLAKE2b-256 e02113367ccfe8610d7fe74fff5aecca17612f724a96fc544fe86730e340d6a5

See more details on using hashes here.

File details

Details for the file django_ultra_rbac-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ultra_rbac-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 590d562a53ad818d045e94484d8e389145054c1521d480739dbb9da434562747
MD5 797a0d1b14dbb5ed7684cc98a3bd0527
BLAKE2b-256 a29163ec7cd4f5b18b4900e3b0283d734106400a43ee74b593766be37fd4eed1

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