-
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 #106 from SELab-2/permissions
Permissions
- Loading branch information
Showing
15 changed files
with
146 additions
and
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from rest_framework.permissions import BasePermission | ||
from rest_framework.request import Request | ||
from rest_framework.viewsets import ViewSet | ||
from api.permissions.role_permissions import is_teacher, is_assistant | ||
from api.models.assistant import Assistant | ||
|
||
|
||
class AssistantPermission(BasePermission): | ||
"""Permission class used as default policy for assistant endpoint.""" | ||
def has_permission(self, request: Request, view: ViewSet) -> bool: | ||
"""Check if user has permission to view a general assistant endpoint.""" | ||
user = request.user | ||
|
||
if view.action == "list": | ||
# Only teachers can query the assistant list. | ||
return user.is_authenticated and is_teacher(user) | ||
|
||
return is_teacher(user) or is_assistant(user) | ||
|
||
def has_object_permission(self, request: Request, view: ViewSet, assistant: Assistant) -> bool: | ||
# Teachers can view the details of all assistants. | ||
# Users can view their own assistant object. | ||
return is_teacher(request.user) or request.user.id == assistant.id |
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 |
---|---|---|
@@ -1,8 +1,28 @@ | ||
from rest_framework import viewsets | ||
from authentication.serializers import UserSerializer | ||
from django.utils.translation import gettext | ||
from rest_framework.viewsets import ReadOnlyModelViewSet | ||
from rest_framework.response import Response | ||
from rest_framework.request import Request | ||
from rest_framework.permissions import IsAdminUser | ||
from authentication.serializers import UserSerializer, UserIDSerializer | ||
from authentication.models import User | ||
|
||
|
||
class AdminViewSet(viewsets.ReadOnlyModelViewSet): | ||
class AdminViewSet(ReadOnlyModelViewSet): | ||
queryset = User.objects.filter(is_staff=True) | ||
serializer_class = UserSerializer | ||
permission_classes = [IsAdminUser] | ||
|
||
def create(self, request: Request) -> Response: | ||
""" | ||
Make the provided user admin by setting `is_staff` = true. | ||
""" | ||
serializer = UserIDSerializer( | ||
data=request.data | ||
) | ||
|
||
if serializer.is_valid(raise_exception=True): | ||
serializer.validated_data["user"].make_admin() | ||
|
||
return Response({ | ||
"message": gettext("admins.success.add") | ||
}) |
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 |
---|---|---|
@@ -1,32 +1,28 @@ | ||
from rest_framework import viewsets, status | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import ReadOnlyModelViewSet | ||
from rest_framework.permissions import IsAdminUser | ||
from api.permissions.assistant_permissions import AssistantPermission | ||
from ..models.assistant import Assistant | ||
from ..serializers.assistant_serializer import AssistantSerializer | ||
from ..serializers.course_serializer import CourseSerializer | ||
|
||
|
||
class AssistantViewSet(viewsets.ModelViewSet): | ||
class AssistantViewSet(ReadOnlyModelViewSet): | ||
|
||
queryset = Assistant.objects.all() | ||
serializer_class = AssistantSerializer | ||
permission_classes = [IsAdminUser | AssistantPermission] | ||
|
||
@action(detail=True, methods=["get"]) | ||
def courses(self, request, pk=None): | ||
def courses(self, request, **_): | ||
"""Returns a list of courses for the given assistant""" | ||
assistant = self.get_object() | ||
courses = assistant.courses | ||
|
||
try: | ||
queryset = Assistant.objects.get(id=pk) | ||
courses = queryset.courses.all() | ||
|
||
# Serialize the course objects | ||
serializer = CourseSerializer( | ||
courses, many=True, context={"request": request} | ||
) | ||
return Response(serializer.data) | ||
# Serialize the course objects | ||
serializer = CourseSerializer( | ||
courses, many=True, context={"request": request} | ||
) | ||
|
||
except Assistant.DoesNotExist: | ||
# Invalid assistant ID | ||
return Response( | ||
status=status.HTTP_404_NOT_FOUND, | ||
data={"message": "Assistant not found"}, | ||
) | ||
return Response(serializer.data) |
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 |
---|---|---|
@@ -1,52 +1,40 @@ | ||
from rest_framework import viewsets, status | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
from ..models.student import Student | ||
from ..serializers.student_serializer import StudentSerializer | ||
from ..serializers.course_serializer import CourseSerializer | ||
from ..serializers.group_serializer import GroupSerializer | ||
from rest_framework.permissions import IsAdminUser | ||
from api.permissions.role_permissions import IsSameUser, IsTeacher | ||
from api.models.student import Student | ||
from api.serializers.student_serializer import StudentSerializer | ||
from api.serializers.course_serializer import CourseSerializer | ||
from api.serializers.group_serializer import GroupSerializer | ||
|
||
|
||
class StudentViewSet(viewsets.ModelViewSet): | ||
queryset = Student.objects.all() | ||
serializer_class = StudentSerializer | ||
permission_classes = [IsAdminUser | IsTeacher | IsSameUser] | ||
|
||
@action(detail=True, methods=["get"]) | ||
def courses(self, request, pk=None): | ||
@action(detail=True) | ||
def courses(self, request, **_): | ||
"""Returns a list of courses for the given student""" | ||
student = self.get_object() | ||
courses = student.courses.all() | ||
|
||
try: | ||
queryset = Student.objects.get(id=pk) | ||
courses = queryset.courses.all() | ||
# Serialize the course objects | ||
serializer = CourseSerializer( | ||
courses, many=True, context={"request": request} | ||
) | ||
|
||
# Serialize the course objects | ||
serializer = CourseSerializer( | ||
courses, many=True, context={"request": request} | ||
) | ||
return Response(serializer.data) | ||
return Response(serializer.data) | ||
|
||
except Student.DoesNotExist: | ||
# Invalid student ID | ||
return Response( | ||
status=status.HTTP_404_NOT_FOUND, data={"message": "Student not found"} | ||
) | ||
|
||
@action(detail=True, methods=["get"]) | ||
def groups(self, request, pk=None): | ||
@action(detail=True) | ||
def groups(self, request, **_): | ||
"""Returns a list of groups for the given student""" | ||
|
||
try: | ||
queryset = Student.objects.get(id=pk) | ||
groups = queryset.groups.all() | ||
|
||
# Serialize the group objects | ||
serializer = GroupSerializer( | ||
groups, many=True, context={"request": request} | ||
) | ||
return Response(serializer.data) | ||
|
||
except Student.DoesNotExist: | ||
# Invalid student ID | ||
return Response( | ||
status=status.HTTP_404_NOT_FOUND, data={"message": "Student not found"} | ||
) | ||
student = self.get_object() | ||
groups = student.groups.all() | ||
|
||
# Serialize the group objects | ||
serializer = GroupSerializer( | ||
groups, many=True, context={"request": request} | ||
) | ||
return Response(serializer.data) |
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 |
---|---|---|
@@ -1,31 +1,30 @@ | ||
from rest_framework import viewsets, status | ||
from rest_framework import status | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
from ..models.teacher import Teacher | ||
from ..serializers.teacher_serializer import TeacherSerializer | ||
from ..serializers.course_serializer import CourseSerializer | ||
from rest_framework.viewsets import ReadOnlyModelViewSet | ||
from rest_framework.permissions import IsAdminUser | ||
|
||
from api.models.course import Course | ||
from api.models.teacher import Teacher | ||
from api.serializers.teacher_serializer import TeacherSerializer | ||
from api.serializers.course_serializer import CourseSerializer | ||
from api.permissions.role_permissions import IsSameUser | ||
|
||
class TeacherViewSet(viewsets.ModelViewSet): | ||
|
||
class TeacherViewSet(ReadOnlyModelViewSet): | ||
queryset = Teacher.objects.all() | ||
serializer_class = TeacherSerializer | ||
permission_classes = [IsAdminUser | IsSameUser] | ||
|
||
@action(detail=True, methods=["get"]) | ||
def courses(self, request, pk=None): | ||
"""Returns a list of courses for the given teacher""" | ||
teacher = self.get_object() | ||
courses = teacher.courses.all() | ||
|
||
try: | ||
queryset = Teacher.objects.get(id=pk) | ||
courses = queryset.courses.all() | ||
|
||
# Serialize the course objects | ||
serializer = CourseSerializer( | ||
courses, many=True, context={"request": request} | ||
) | ||
return Response(serializer.data) | ||
# Serialize the course objects | ||
serializer = CourseSerializer( | ||
courses, many=True, context={"request": request} | ||
) | ||
|
||
except Teacher.DoesNotExist: | ||
# Invalid teacher ID | ||
return Response( | ||
status=status.HTTP_404_NOT_FOUND, data={"message": "Teacher not found"} | ||
) | ||
return Response(serializer.data) |
Oops, something went wrong.