mirror of
https://github.com/yogeshojha/rengine.git
synced 2026-09-06 18:07:40 +02:00
21 lines
786 B
Python
21 lines
786 B
Python
from rest_framework.permissions import BasePermission
|
|
from rest_framework.exceptions import PermissionDenied
|
|
from rolepermissions.checkers import has_permission
|
|
|
|
class HasPermission(BasePermission):
|
|
"""
|
|
This is a custom permission class for DRF that checks if the user
|
|
has the required permission.
|
|
Usage in drf views:
|
|
permission_classes = [HasPermission]
|
|
permission_required = PERM_MODIFY_SCAN_CONFIGURATIONS
|
|
"""
|
|
|
|
def has_permission(self, request, view):
|
|
permission_code = getattr(view, 'permission_required', None)
|
|
if not permission_code:
|
|
raise PermissionDenied(detail="Permission is not specified for this view.")
|
|
|
|
if not has_permission(request.user, permission_code):
|
|
raise PermissionDenied(detail="This user does not have enough permissions")
|
|
return True |