Django
Permission Custom
from rest_framework import (
permissions,
status as _status,
)
from rest_framework.exceptions import PermissionDenied
class TextUpatePermission(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
allowed_roles = set(['Decision Maker', 'Team Leader', 'Team Member'])
if view.action == 'partial_update':
work_id = request.data.get('work_id')
project_id = Work.objects.get(id=work_id).project_id
try:
user_role = UserProjectRole.objects.get(
project_id=project_id,
user_id=request.user.id,
is_deleted=False,
).role
except UserProjectRole.DoesNotExist:
user_role = None
if user_role in allowed_roles:
return True
elif (user_role == 'Team Member') and (obj.user_id == request.user.id):
return True
raise PermissionDenied(
{
"message": "텍스트를 변경할 수 있는 권한이 없습니다.",
"code": "TEXT_UPDATE_403_FORBIDDEN",
"status": _status.HTTP_403_FORBIDDEN,
}
)
return True