-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' of https://github.com/SELab-2/UGent-7 into…
… file_structure_checks
- Loading branch information
Showing
10 changed files
with
354 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from rest_framework.permissions import BasePermission, SAFE_METHODS | ||
from rest_framework.request import Request | ||
from rest_framework.viewsets import ViewSet | ||
from authentication.models import User | ||
from api.permissions.role_permissions import is_student, is_assistant, is_teacher | ||
from api.models.course import Course | ||
|
||
|
||
class CoursePermission(BasePermission): | ||
"""Permission class used as default policy for course endpoints.""" | ||
|
||
def has_permission(self, request: Request, view: ViewSet) -> bool: | ||
"""Check if user has permission to view a general course endpoint.""" | ||
user: User = request.user | ||
|
||
# Logged-in users can fetch course information. | ||
if request.method in SAFE_METHODS: | ||
return request.user.is_authenticated | ||
|
||
# Only teachers can create courses. | ||
return is_teacher(user) | ||
|
||
def has_object_permission(self, request: Request, view: ViewSet, course: Course) -> bool: | ||
"""Check if user has permission to view a detailed course endpoint""" | ||
user: User = request.user | ||
|
||
# Logged-in users can fetch course details. | ||
if request.method in SAFE_METHODS: | ||
return user.is_authenticated | ||
|
||
# We only allow teachers and assistants to modify their own courses. | ||
return is_teacher(user) and user.teacher.courses.contains(course) or \ | ||
is_assistant(user) and user.assistant.courses.contains(course) | ||
|
||
|
||
class CourseAssistantPermission(CoursePermission): | ||
"""Permission class for assistant related endpoints.""" | ||
|
||
def has_object_permission(self, request: Request, view: ViewSet, course: Course) -> bool: | ||
user: User = request.user | ||
|
||
# Logged-in users can fetch course assistants. | ||
if request.method in SAFE_METHODS: | ||
return user.is_authenticated | ||
|
||
# Only teachers can modify assistants of their own courses. | ||
return is_teacher(user) and user.teacher.courses.contains(course) | ||
|
||
|
||
class CourseStudentPermission(CoursePermission): | ||
"""Permission class for student related endpoints.""" | ||
def has_permission(self, request: Request, view: ViewSet) -> bool: | ||
return request.user and request.user.is_authenticated | ||
|
||
def has_object_permission(self, request: Request, view: ViewSet, course: Course): | ||
user: User = request.user | ||
|
||
# Logged-in users can fetch course students. | ||
if request.method in SAFE_METHODS: | ||
return user.is_authenticated | ||
|
||
# Only students can add or remove themselves from a course. | ||
if is_student(user) and request.data.get("student_id") == user.id: | ||
return True | ||
|
||
# Teachers and assistants can add and remove any student. | ||
return super().has_object_permission(request, view, course) | ||
|
||
|
||
class CourseProjectPermission(CoursePermission): | ||
"""Permission class for project related endpoints.""" | ||
def has_permission(self, request: Request, view: ViewSet) -> bool: | ||
return request.user and request.user.is_authenticated | ||
|
||
def has_object_permission(self, request: Request, view: ViewSet, course: Course): | ||
user: User = request.user | ||
|
||
# Logged-in users can fetch course projects. | ||
if request.method in SAFE_METHODS: | ||
return user.is_authenticated | ||
|
||
# Teachers and assistants can modify projects. | ||
return super().has_object_permission(request, view, course) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.request import Request | ||
from authentication.models import User | ||
from api.models.student import Student | ||
from api.models.assistant import Assistant | ||
from api.models.teacher import Teacher | ||
|
||
|
||
def is_student(user: User): | ||
return Student.objects.filter(id=user.id).exists() | ||
|
||
|
||
def is_assistant(user: User): | ||
return Assistant.objects.filter(id=user.id).exists() | ||
|
||
|
||
def is_teacher(user: User): | ||
return Teacher.objects.filter(id=user.id).exists() | ||
|
||
|
||
class IsStudent(IsAuthenticated): | ||
def has_permission(self, request: Request, view): | ||
"""Returns true if the request contains a user, | ||
with said user being a student""" | ||
return super().has_permission(request, view) and is_student(request.user) | ||
|
||
|
||
class IsTeacher(IsAuthenticated): | ||
def has_permission(self, request: Request, view): | ||
"""Returns true if the request contains a user, | ||
with said user being a student""" | ||
return super().has_permission(request, view) and is_teacher(request.user) | ||
|
||
|
||
class IsAssistant(IsAuthenticated): | ||
def has_permission(self, request, view): | ||
"""Returns true if the request contains a user, | ||
with said user being a student""" | ||
return super().has_permission(request, view) and is_assistant(request.user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.