Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permissions cleanup #131

Merged
merged 7 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/api/permissions/assistant_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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":
if view.action in ['list', 'create', 'destroy']:
# Only teachers can query the assistant list.
return user.is_authenticated and is_teacher(user)

Expand Down
17 changes: 17 additions & 0 deletions backend/api/permissions/student_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework.permissions import IsAuthenticated, SAFE_METHODS
from api.permissions.role_permissions import is_teacher
from authentication.models import User

class StudentPermission(IsAuthenticated):

def has_permission(self, request, view):
"""Check if user has permission to view a general student endpoint."""
user: User = request.user
if view.action in ['list', 'create', 'update', 'partial_update', 'destroy']:
DeLany123 marked this conversation as resolved.
Show resolved Hide resolved
return False
return True

def has_object_permission(self, request, view, obj):
"""Check if user has permission to view a detailed group endpoint"""
user: User = request.user
return request.method in SAFE_METHODS and (user.id == request.user.id or is_teacher(user))
17 changes: 17 additions & 0 deletions backend/api/permissions/teacher_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework.permissions import IsAuthenticated, SAFE_METHODS
from authentication.models import User

# (Almost) same as StudentPermission
class TeacherPermission(IsAuthenticated):

def has_permission(self, request, view):
"""Check if user has permission to view a general Teacher endpoint."""
user: User = request.user
if view.action in ['list', 'create', 'update', 'partial_update', 'destroy']:
DeLany123 marked this conversation as resolved.
Show resolved Hide resolved
return False
return True

def has_object_permission(self, request, view, obj):
"""Check if user has permission to view a detailed group endpoint"""
user: User = request.user
return request.method in SAFE_METHODS and user.id == request.user.id
4 changes: 2 additions & 2 deletions backend/api/views/assistant_view.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.viewsets import ModelViewSet
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(ReadOnlyModelViewSet):
class AssistantViewSet(ModelViewSet):

queryset = Assistant.objects.all()
serializer_class = AssistantSerializer
Expand Down
5 changes: 3 additions & 2 deletions backend/api/views/student_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser
from api.permissions.student_permissions import StudentPermission
from api.permissions.role_permissions import IsSameUser, IsTeacher
from api.models.student import Student
from api.serializers.student_serializer import StudentSerializer
Expand All @@ -12,12 +13,12 @@
class StudentViewSet(viewsets.ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
permission_classes = [IsAdminUser | IsTeacher | IsSameUser]
permission_classes = [IsAdminUser | StudentPermission]

@action(detail=True)
def courses(self, request, **_):
"""Returns a list of courses for the given student"""
student = self.get_object()
student = self.get_object()
courses = student.courses.all()

# Serialize the course objects
Expand Down
12 changes: 6 additions & 6 deletions backend/api/views/teacher_view.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.viewsets import ModelViewSet
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
from api.permissions.teacher_permissions import TeacherPermission
from rest_framework.permissions import IsAuthenticated


class TeacherViewSet(ReadOnlyModelViewSet):
class TeacherViewSet(ModelViewSet):
queryset = Teacher.objects.all()
serializer_class = TeacherSerializer
permission_classes = [IsAdminUser | IsSameUser]
permission_classes = [IsAdminUser | TeacherPermission]

@action(detail=True, methods=["get"])
@action(detail=True, methods=["get"], permission_classes=[IsAuthenticated])
def courses(self, request, pk=None):
"""Returns a list of courses for the given teacher"""
teacher = self.get_object()
Expand Down
5 changes: 4 additions & 1 deletion backend/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class User(AbstractBaseUser):
USERNAME_FIELD = "username"
EMAIL_FIELD = "email"

def make_admin(self):
self.is_staff = True
self.save()

@staticmethod
def get_dummy_admin():
return User(
Expand All @@ -45,7 +49,6 @@ def get_dummy_admin():
is_staff=True
)


class Faculty(models.Model):
"""This model represents a faculty."""

Expand Down
Loading