You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.8 KiB
58 lines
1.8 KiB
|
|
|
|
|
|
|
|
|
|
from rest_framework.permissions import BasePermission, SAFE_METHODS
|
|
|
|
|
|
class IsActiveUser(BasePermission):
|
|
|
|
def has_permission(self, request, view):
|
|
return request.user and request.user.is_active
|
|
|
|
|
|
class IsSuperAdmin(BasePermission):
|
|
"""
|
|
Allows access only to super admins and staff admins.
|
|
"""
|
|
def has_permission(self, request, view):
|
|
return (
|
|
request.user and
|
|
request.user.is_authenticated and
|
|
request.user.is_active and
|
|
(request.user.is_superuser or request.user.user_type in ['super_admin', 'admin'])
|
|
)
|
|
|
|
|
|
class IsPanelUser(BasePermission):
|
|
"""
|
|
Allows access to all administrative panel roles: super admins, admins, and professors.
|
|
"""
|
|
def has_permission(self, request, view):
|
|
return (
|
|
request.user and
|
|
request.user.is_authenticated and
|
|
request.user.is_active and
|
|
(request.user.is_superuser or request.user.user_type in ['super_admin', 'admin', 'professor'])
|
|
)
|
|
|
|
|
|
class IsSuperAdminOrReadOnlyForProfessor(BasePermission):
|
|
"""
|
|
Allows full read-write access to super admins/admins,
|
|
but only read-only (GET, HEAD, OPTIONS) access to professors.
|
|
"""
|
|
def has_permission(self, request, view):
|
|
if not request.user or not request.user.is_authenticated or not request.user.is_active:
|
|
return False
|
|
|
|
# Super admin / Admin can do everything
|
|
if request.user.is_superuser or request.user.user_type in ['super_admin', 'admin']:
|
|
return True
|
|
|
|
# Professor has read-only access
|
|
if request.user.user_type == 'professor':
|
|
return request.method in SAFE_METHODS
|
|
|
|
return False
|