forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.py
52 lines (41 loc) · 1.71 KB
/
permissions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from authorization.permissions import SAFE_METHODS, Permission, FilterBackend
from .models import UserProfile, GraderUser, LTIServiceUser
from course.models import CourseInstance
class IsAdminOrUserObjIsSelf(Permission, FilterBackend):
def is_super(self, user):
return (
user.is_staff or
user.is_superuser or
isinstance(user, GraderUser) # grader is considered admin
)
def has_object_permission(self, request, view, obj):
if not isinstance(obj, UserProfile):
return True
user = request.user
return user and (
(user.id is not None and user.id == obj.user_id) or
self.is_super(user)
)
def filter_queryset(self, request, queryset, view):
user = request.user
if issubclass(queryset.model, UserProfile) and not self.is_super(user):
queryset = queryset.filter(user_id=user.id)
return queryset
class IsTeacherOrAdminOrSelf(IsAdminOrUserObjIsSelf):
def is_super(self, user):
if super().is_super(user):
return True
# FIXME: inefficient database query
# Loop over every course instance in the database to check if the user
# is a teacher on any course instance.
every_course = CourseInstance.objects.all()
return any(course.is_teacher(user) for course in every_course)
class GraderUserCanOnlyRead(Permission):
def has_permission(self, request, view):
return (
not isinstance(request.user, GraderUser) or
request.method in SAFE_METHODS
)
class IsLTIServiceUser(Permission):
def has_permission(self, request, view):
return isinstance(request.user, LTIServiceUser)