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.1.tar.gz (7.8 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.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_ultra_rbac-0.1.1.tar.gz
  • Upload date:
  • Size: 7.8 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.1.tar.gz
Algorithm Hash digest
SHA256 a00c196dda63c9371cd3e848ab9ec1295e3a3f1eff29bb3f038de1d2c489e4a6
MD5 6bf5d6c727136d9d6298b915a21287be
BLAKE2b-256 49dbd8677eeb0108f170f2f22e5318b80d84410595eb419c798c91ef07b09986

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_ultra_rbac-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6003c4f07252917d8372d2999e4e5c66f5c93bde36dd0c130358535fc9c7a784
MD5 96c7cecd329798fc9521d9a20d7900e5
BLAKE2b-256 eecbc7fb5d065e2da6651666155df7f8f2801b9e114543daeb3727de95d30401

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