edx-rbac

raw JSON →
3.0.0 verified Mon Apr 27 auth: no python

Library for managing role-based access control (RBAC) for Django applications, used within the Open edX ecosystem. Current version: 3.0.0 (2025). Release cadence: sporadic, with minor and patch releases every few months.

pip install edx-rbac
error ImportError: cannot import name 'get_user_role_permissions' from 'edx_rbac'
cause Incorrect import path from edx_rbac instead of edx_rbac.utils.
fix
Change import to: from edx_rbac.utils import get_user_role_permissions
error AttributeError: module 'edx_rbac' has no attribute 'RoleAssignment'
cause Trying to import RoleAssignment from the package root instead of edx_rbac.models.
fix
Use: from edx_rbac.models import RoleAssignment
error TypeError: user_has_role() missing 1 required positional argument: 'role'
cause Called user_has_role(user) without specifying role.
fix
Provide the role string: user_has_role(user, 'admin')
error django.core.exceptions.ImproperlyConfigured: The RBAC_BACKEND setting is not configured.
cause Missing required Django setting for RBAC.
fix
Add RBAC_BACKEND to settings, e.g.: RBAC_BACKEND = 'edx_rbac.backends.RoleBackend'
breaking Version 3.0.0 drops Python 3.11 support. Upgrade to Python 3.12+.
fix Upgrade Python to 3.12 or later, then pin edx-rbac>=3.0.0.
breaking Version 2.0.0 drops Python 3.8 support.
fix Upgrade Python to 3.9+.
gotcha Do not import from edx_rbac.helpers; that module is deprecated and will be removed. Use edx_rbac.utils instead.
fix Change imports to edx_rbac.utils.
gotcha The default RoleAssignment model requires a valid `context` field; using an empty string may cause unexpected behavior.
fix Always provide a meaningful context when creating RoleAssignment objects.

Create a user, assign a role, and verify role existence.

from django.contrib.auth.models import User
from edx_rbac.models import RoleAssignment
from edx_rbac.utils import user_has_role

# Create a user and assign a role
user = User.objects.create_user('testuser', 'test@example.com', 'password')
RoleAssignment.objects.create(user=user, role='admin', context='default')

# Check if user has a role
has_role = user_has_role(user, 'admin')
print(f'User has admin role: {has_role}')