Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Add /project/{id}/groups and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
msathieu committed Mar 10, 2024
1 parent 1bc9b94 commit 7cae56a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
13 changes: 13 additions & 0 deletions backend/domain/logic/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from db.errors.database_errors import ActionAlreadyPerformedError
from db.models.models import Student, Subject, Teacher
from domain.logic.basic_operations import get, get_all
from domain.logic.student import is_user_student
from domain.logic.teacher import is_user_teacher
from domain.models.SubjectDataclass import SubjectDataclass


Expand Down Expand Up @@ -55,3 +57,14 @@ def get_subjects_of_student(session: Session, student_id: int) -> list[SubjectDa
student: Student = get(session, Student, ident=student_id)
subjects: list[Subject] = student.subjects
return [vak.to_domain_model() for vak in subjects]


def is_user_authorized_for_subject(subject_id: int, session: Session, uid: int) -> bool:
subjects = []
if is_user_teacher(session, uid):
subjects += get_subjects_of_teacher(session, uid)
if is_user_student(session, uid):
subjects += get_subjects_of_student(session, uid)
if subject_id in [subject.id for subject in subjects]:
return True
return False
20 changes: 13 additions & 7 deletions backend/routes/dependencies/role_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

from db.sessions import get_session
from domain.logic.admin import get_admin, is_user_admin
from domain.logic.project import get_project
from domain.logic.student import get_student, is_user_student
from domain.logic.subject import get_subjects_of_student, get_subjects_of_teacher
from domain.logic.subject import get_subjects_of_student, get_subjects_of_teacher, is_user_authorized_for_subject
from domain.logic.teacher import get_teacher, is_user_teacher
from domain.models.AdminDataclass import AdminDataclass
from domain.models.StudentDataclass import StudentDataclass
Expand Down Expand Up @@ -47,12 +48,17 @@ def ensure_user_authorized_for_subject(
session: Session = Depends(get_session),
uid: int = Depends(get_authenticated_user),
) -> None:
subjects = []
if is_user_teacher(session, uid):
subjects += get_subjects_of_teacher(session, uid)
if is_user_student(session, uid):
subjects += get_subjects_of_student(session, uid)
if subject_id not in [subject.id for subject in subjects]:
if not is_user_authorized_for_subject(subject_id, session, uid):
raise NoAccessToSubjectError


def ensure_user_authorized_for_project(
project_id: int,
session: Session = Depends(get_session),
uid: int = Depends(get_authenticated_user),
) -> None:
project = get_project(session, project_id)
if not is_user_authorized_for_subject(project.subject_id, session, uid):
raise NoAccessToSubjectError


Expand Down
25 changes: 21 additions & 4 deletions backend/routes/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@
from sqlalchemy.orm import Session

from db.sessions import get_session
from domain.logic.group import get_groups_of_project
from domain.logic.project import get_project
from domain.models.GroupDataclass import GroupDataclass
from domain.models.ProjectDataclass import ProjectDataclass
from routes.dependencies.role_dependencies import ensure_user_authorized_for_subject
from routes.dependencies.role_dependencies import (
ensure_user_authorized_for_project,
get_authenticated_user,
)

project_router = APIRouter()


@project_router.get("/projects/{project_id}")
def project_get(project_id: int, session: Session = Depends(get_session)) -> ProjectDataclass:
@project_router.get("/projects/{project_id}", dependencies=[Depends(ensure_user_authorized_for_project)])
def project_get(
project_id: int,
session: Session = Depends(get_session),
uid: int = Depends(get_authenticated_user),
) -> ProjectDataclass:
project: ProjectDataclass = get_project(session, project_id)
ensure_user_authorized_for_subject(project.subject_id)
return project


@project_router.get("/projects/{project_id}/groups", dependencies=[Depends(ensure_user_authorized_for_project)])
def project_get_groups(
project_id: int,
session: Session = Depends(get_session),
uid: int = Depends(get_authenticated_user),
) -> list[GroupDataclass]:
return get_groups_of_project(session, project_id)

0 comments on commit 7cae56a

Please sign in to comment.