-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
219 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
from .is_school_member import IsSchoolMember | ||
from .is_school_teacher import IsSchoolTeacher | ||
""" | ||
© Ocado Group | ||
Created on 14/12/2023 at 14:05:06(+00:00). | ||
""" | ||
|
||
from .in_class import InClass | ||
from .in_school import InSchool | ||
from .is_independent import IsIndependent | ||
from .is_student import IsStudent | ||
from .is_teacher import IsTeacher |
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,46 @@ | ||
""" | ||
© Ocado Group | ||
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 has_permission(self, request: Request, view: APIView): | ||
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() | ||
|
||
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 False |
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,49 @@ | ||
""" | ||
© Ocado Group | ||
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 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 | ||
|
||
user = request.user | ||
return ( | ||
super().has_permission(request, view) | ||
and isinstance(user, User) | ||
and ( | ||
( | ||
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) | ||
) | ||
) | ||
) |
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 @@ | ||
""" | ||
© Ocado Group | ||
Created on 12/12/2023 at 13:55:47(+00:00). | ||
""" | ||
|
||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.request import Request | ||
from rest_framework.views import APIView | ||
|
||
from ..models import User | ||
|
||
|
||
class IsIndependent(IsAuthenticated): | ||
"""Request's user must be independent.""" | ||
|
||
def has_permission(self, request: Request, view: APIView): | ||
user = request.user | ||
return ( | ||
super().has_permission(request, view) | ||
and isinstance(user, User) | ||
and user.teacher is None | ||
and user.student is None | ||
) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,36 @@ | ||
""" | ||
© Ocado Group | ||
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 has_permission(self, request: Request, view: APIView): | ||
user = request.user | ||
return ( | ||
super().has_permission(request, view) | ||
and isinstance(user, User) | ||
and user.student is not None | ||
and (self.student_id is None or user.student.id == self.student_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
© Ocado Group | ||
Created on 12/12/2023 at 13:55:22(+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 IsTeacher(IsAuthenticated): | ||
"""Request's user must be a teacher.""" | ||
|
||
def __init__( | ||
self, | ||
teacher_id: t.Optional[int] = None, | ||
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 has_permission(self, request: Request, view: APIView): | ||
user = request.user | ||
return ( | ||
super().has_permission(request, view) | ||
and isinstance(user, User) | ||
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 | ||
) | ||
) |
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