-
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 pull request #72 from SELab-2/group-logic
Group logic
- Loading branch information
Showing
12 changed files
with
749 additions
and
229 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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 | ||
|
||
|
||
class GroupPermission(BasePermission): | ||
|
||
def has_permission(self, request: Request, view: ViewSet) -> bool: | ||
"""Check if user has permission to view a general group endpoint.""" | ||
user: User = request.user | ||
|
||
# The general group endpoint that lists all groups is not accessible for any role. | ||
if request.method in SAFE_METHODS: | ||
return True | ||
|
||
# We only allow teachers and assistants to create new groups. | ||
return is_teacher(user) or is_assistant(user) | ||
|
||
def has_object_permission(self, request: Request, view: ViewSet, group) -> bool: | ||
"""Check if user has permission to view a detailed group endpoint""" | ||
user: User = request.user | ||
course = group.project.course | ||
teacher_or_assitant = is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \ | ||
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists() | ||
|
||
if request.method in SAFE_METHODS: | ||
# Users that are linked to the course can view the group. | ||
return teacher_or_assitant or (is_student(user) and user.student.courses.filter(id=course.id).exists()) | ||
|
||
# We only allow teachers and assistants to modify specified groups. | ||
return teacher_or_assitant | ||
|
||
|
||
class GroupStudentPermission(BasePermission): | ||
"""Permission class for student related group endpoints""" | ||
|
||
def has_object_permission(self, request: Request, view: ViewSet, group) -> bool: | ||
user: User = request.user | ||
course = group.project.course | ||
teacher_or_assitant = is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \ | ||
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists() | ||
|
||
if request.method in SAFE_METHODS: | ||
# Users related to the course can view the students of the group. | ||
return teacher_or_assitant or (is_student(user) and user.student.courses.filter(id=course.id).exists()) | ||
|
||
# Students can only add and remove themselves from a group. | ||
if is_student(user) and request.data.get("student_id") == user.id: | ||
# Make sure the student is actually part of the course. | ||
return user.student.courses.filter(id=course.id).exists() | ||
|
||
# Teachers and assistants can add and remove any student from a group | ||
return teacher_or_assitant |
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,51 @@ | ||
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 | ||
|
||
|
||
class ProjectPermission(BasePermission): | ||
"""Permission class for project related endpoints""" | ||
|
||
def has_permission(self, request: Request, view: ViewSet) -> bool: | ||
"""Check if user has permission to view a general project endpoint.""" | ||
user: User = request.user | ||
|
||
# The general project endpoint that lists all projects is not accessible for any role. | ||
if request.method in SAFE_METHODS: | ||
return True | ||
|
||
# We only allow teachers and assistants to create new projects. | ||
return is_teacher(user) or is_assistant(user) | ||
|
||
def has_object_permission(self, request: Request, view: ViewSet, project) -> bool: | ||
"""Check if user has permission to view a detailed project endpoint""" | ||
user: User = request.user | ||
course = project.course | ||
teacher_or_assistant = is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \ | ||
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists() | ||
|
||
if request.method in SAFE_METHODS: | ||
# Users that are linked to the course can view the project. | ||
return teacher_or_assistant or (is_student(user) and user.student.courses.filter(id=course.id).exists()) | ||
|
||
# We only allow teachers and assistants to modify specified projects. | ||
return teacher_or_assistant | ||
|
||
|
||
class ProjectGroupPermission(BasePermission): | ||
"""Permission class for project related group endpoints""" | ||
|
||
def has_object_permission(self, request: Request, view: ViewSet, project) -> bool: | ||
user: User = request.user | ||
course = project.course | ||
teacher_or_assistant = is_teacher(user) and user.teacher.courses.filter(id=course.id).exists() or \ | ||
is_assistant(user) and user.assistant.courses.filter(id=course.id).exists() | ||
|
||
if request.method in SAFE_METHODS: | ||
# Users that are linked to the course can view the group. | ||
return teacher_or_assistant or (is_student(user) and user.student.courses.filter(id=course.id).exists()) | ||
|
||
# We only allow teachers and assistants to create new groups. | ||
return teacher_or_assistant |
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.