Skip to content

Commit

Permalink
fix: permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Feb 8, 2024
1 parent d1aaf51 commit 53e2bee
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 137 deletions.
1 change: 0 additions & 1 deletion codeforlife/permissions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@

from .allow_none import AllowNone
from .is_cron_request_from_google import IsCronRequestFromGoogle
from .is_self import IsSelf
from .operators import AND, NOT, OR, Permission
42 changes: 0 additions & 42 deletions codeforlife/permissions/is_self.py

This file was deleted.

35 changes: 4 additions & 31 deletions codeforlife/user/permissions/in_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,23 @@
Created on 12/12/2023 at 15:18:10(+00:00).
"""

import typing as t

from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.views import APIView

from ..models import User


class InClass(IsAuthenticated):
"""Request's user must be in a class."""

def __init__(self, class_id: t.Optional[str] = None):
"""Initialize permission.
Args:
class_id: A class' ID. If None, check if user is in any class.
Else, check if user is in the specific class.
"""

super().__init__()
self.class_id = class_id

def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.class_id == other.class_id
)
return isinstance(other, self.__class__)

def has_permission(self, request: Request, view: APIView):
def has_permission(self, request, view):
user = request.user
if super().has_permission(request, view) and isinstance(user, User):
if user.teacher is not None:
classes = user.teacher.class_teacher
if self.class_id is not None:
classes = classes.filter(access_code=self.class_id)
return classes.exists()

return user.teacher.class_teacher.exists()
if user.student is not None:
if self.class_id is None:
return True
return (
user.student.class_field is not None
and user.student.class_field.access_code == self.class_id
)
return user.student.class_field is not None

return False
28 changes: 3 additions & 25 deletions codeforlife/user/permissions/in_school.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,18 @@
Created on 12/12/2023 at 15:18:27(+00:00).
"""

import typing as t

from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.views import APIView

from ..models import User


class InSchool(IsAuthenticated):
"""Request's user must be in a school."""

def __init__(self, school_id: t.Optional[int] = None):
"""Initialize permission.
Args:
school_id: A school's ID. If None, check if user is in any school.
Else, check if user is in the specific school.
"""

super().__init__()
self.school_id = school_id

def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.school_id == other.school_id
)

def has_permission(self, request: Request, view: APIView):
def in_school(school_id: int):
return self.school_id is None or self.school_id == school_id
return isinstance(other, self.__class__)

def has_permission(self, request, view):
user = request.user
return (
super().has_permission(request, view)
Expand All @@ -44,12 +23,11 @@ def in_school(school_id: int):
(
user.teacher is not None
and user.teacher.school_id is not None
and in_school(user.teacher.school_id)
)
or (
user.student is not None
and user.student.class_field is not None
and in_school(user.student.class_field.teacher.school_id)
and user.student.class_field.teacher.school_id is not None
)
)
)
7 changes: 3 additions & 4 deletions codeforlife/user/permissions/is_independent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"""

from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.views import APIView

from ..models import User

Expand All @@ -16,11 +14,12 @@ class IsIndependent(IsAuthenticated):
def __eq__(self, other):
return isinstance(other, self.__class__)

def has_permission(self, request: Request, view: APIView):
def has_permission(self, request, view):
user = request.user
return (
super().has_permission(request, view)
and isinstance(user, User)
and user.teacher is None
and user.student is None
and user.student is not None
and user.student.class_field is None
)
25 changes: 4 additions & 21 deletions codeforlife/user/permissions/is_student.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,23 @@
Created on 12/12/2023 at 13:55:40(+00:00).
"""

import typing as t

from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.views import APIView

from ..models import User


class IsStudent(IsAuthenticated):
"""Request's user must be a student."""

def __init__(self, student_id: t.Optional[int] = None):
"""Initialize permission.
Args:
student_id: A student's ID. If None, check if the user is any
student. Else, check if the user is the specific student.
"""

super().__init__()
self.student_id = student_id

def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.student_id == other.student_id
)
return isinstance(other, self.__class__)

def has_permission(self, request: Request, view: APIView):
def has_permission(self, request, view):
user = request.user
return (
super().has_permission(request, view)
and isinstance(user, User)
and user.teacher is None
and user.student is not None
and (self.student_id is None or user.student.id == self.student_id)
and user.student.class_field is not None
)
16 changes: 3 additions & 13 deletions codeforlife/user/permissions/is_teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,38 @@
import typing as t

from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.views import APIView

from ..models import User


class IsTeacher(IsAuthenticated):
"""Request's user must be a teacher."""

def __init__(
self,
teacher_id: t.Optional[int] = None,
is_admin: t.Optional[bool] = None,
):
def __init__(self, is_admin: t.Optional[bool] = None):
"""Initialize permission.
Args:
teacher_id: A teacher's ID. If None, check if the user is any
teacher. Else, check if the user is the specific teacher.
is_admin: If the teacher is an admin. If None, don't check if the
teacher is an admin. Else, check if the teacher is (not) an
admin.
"""

super().__init__()
self.teacher_id = teacher_id
self.is_admin = is_admin

def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.teacher_id == other.teacher_id
and self.is_admin == other.is_admin
)

def has_permission(self, request: Request, view: APIView):
def has_permission(self, request, view):
user = request.user
return (
super().has_permission(request, view)
and isinstance(user, User)
and user.student is None
and user.teacher is not None
and (self.teacher_id is None or user.teacher.id == self.teacher_id)
and (
self.is_admin is None or user.teacher.is_admin == self.is_admin
)
Expand Down

0 comments on commit 53e2bee

Please sign in to comment.