Permissions¶
The drf_corekit.permissions module provides reusable permission utilities and
focuses on composable building blocks rather than app specific roles.
Role Based Permissions¶
RolePermission¶
Generic role based permission checker.
Supports simple attribute based authorization using:
is_staffis_superuseruser_type(optional)
Behavior¶
This permission:
- Verifies the user is authenticated
- Evaluates configured role requirements
- Applies the same checks at both view and object level
- Supports composition with other DRF permissions
This permission does NOT:
- Check object ownership
- Inspect object state
- Define application specific roles
Configuration¶
Subclasses define one or more role requirements.
Require staff access:
Require superuser access:
Require a specific user type:
Multiple requirements can be combined:
Permission Composition¶
Role permissions work seamlessly with DRF permission operators.
Allow staff or object owners:
Require both admin access and ownership:
Require authentication and either staff access or ownership:
Role Composition¶
RolesOrReadOnly¶
Allows unrestricted read access while restricting unsafe methods to configured roles.
Configuration¶
Multiple role permissions can be configured:
Behavior¶
| Method | Access |
|---|---|
GET, HEAD, OPTIONS |
Allowed for everyone |
| Unsafe methods | Requires at least one role permission |
Ownership Permissions¶
OwnershipPermission¶
Generic object level permission based on ownership rules.
Features¶
- Attribute based ownership resolution
- Callable based ownership resolution
- Support for multiple owners
- Optional admin bypass
- No dependency on a specific user model structure
Configuration¶
Basic ownership check:
Custom ownership resolution:
class IsTicketParticipant(OwnershipPermission):
ownership_attr = lambda obj: (
obj.created_by,
obj.assigned_to,
)
Owner or admin:
class IsOwnerOrAdmin(OwnershipPermission):
ownership_attr = "owner"
allow_admin_bypass = True
admin_checker = lambda user: user.is_superuser
Behavior¶
This permission:
- Verifies the user is authenticated
- Resolves ownership using
ownership_attr - Supports single or multiple owners
- Supports callable ownership resolution
- Supports configurable admin bypass
This permission does NOT:
- Depend on profiles or custom user model structures
- Define what an administrator is
- Implement application specific role logic
Ownership Resolution¶
ownership_attr may be:
- An attribute name
- A callable
- A callable returning multiple owners
Action Based Permissions¶
ActionRolePermission¶
Maps DRF ViewSet actions to permission classes.
Example¶
class MyPermissions(ActionRolePermission):
ACTION_ROLE_MAP = {
"list": [IsAdmin],
"create": [IsAdmin],
"retrieve": [IsOwner, IsAdmin],
}
Behavior¶
- Selects permissions based on view.action
- Grants access if ANY mapped permission allows it
- Supports both permission level and object level checks