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.
56 lines
1.6 KiB
56 lines
1.6 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 to super admins and admins with full panel privileges.
|
|
"""
|
|
def has_permission(self, request, view):
|
|
return (
|
|
request.user and
|
|
request.user.is_authenticated and
|
|
(request.user.is_super_admin_panel_user() or request.user.is_admin_panel_user())
|
|
)
|
|
|
|
|
|
class IsPanelUser(BasePermission):
|
|
"""
|
|
Allows access to super admins, admins, and professors.
|
|
"""
|
|
def has_permission(self, request, view):
|
|
return (
|
|
request.user and
|
|
request.user.is_authenticated and
|
|
request.user.can_access_admin_panel()
|
|
)
|
|
|
|
|
|
class IsSuperAdminOrReadOnlyForProfessor(BasePermission):
|
|
"""
|
|
Allows full read-write access to super admins and 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_super_admin_panel_user() or request.user.is_admin_panel_user():
|
|
return True
|
|
|
|
# Professor has read-only access
|
|
if request.user.is_professor_panel_user():
|
|
return request.method in SAFE_METHODS
|
|
|
|
return False
|