This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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
7 changed files
with
120 additions
and
31 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from db.implementation.SqlAdminDAO import SqlAdminDAO | ||
from db.implementation.SqlGroupDAO import SqlGroupDAO | ||
from db.implementation.SqlProjectDAO import SqlProjectDAO | ||
from db.implementation.SqlStudentDAO import SqlStudentDAO | ||
from db.implementation.SqlSubjectDAO import SqlSubjectDAO | ||
from db.implementation.SqlSubmissionDAO import SqlSubmissionDAO | ||
from db.implementation.SqlTeacherDAO import SqlTeacherDAO | ||
from db.implementation.SqlUserDAO import SqlUserDAO | ||
from db.interface.AdminDAO import AdminDAO | ||
from db.interface.DAOProvider import DAOProvider | ||
from db.interface.GroupDAO import GroupDAO | ||
from db.interface.ProjectDAO import ProjectDAO | ||
from db.interface.StudentDAO import StudentDAO | ||
from db.interface.SubjectDAO import SubjectDAO | ||
from db.interface.SubmissionDAO import SubmissionDAO | ||
from db.interface.TeacherDAO import TeacherDAO | ||
from db.interface.UserDAO import UserDAO | ||
|
||
|
||
class SqlDAOProvider(DAOProvider): | ||
def get_admin_dao(self) -> AdminDAO: | ||
return SqlAdminDAO() | ||
|
||
def get_group_dao(self) -> GroupDAO: | ||
return SqlGroupDAO() | ||
|
||
def get_project_dao(self) -> ProjectDAO: | ||
return SqlProjectDAO() | ||
|
||
def get_student_dao(self) -> StudentDAO: | ||
return SqlStudentDAO() | ||
|
||
def get_subject_dao(self) -> SubjectDAO: | ||
return SqlSubjectDAO() | ||
|
||
def get_submission_dao(self) -> SubmissionDAO: | ||
return SqlSubmissionDAO() | ||
|
||
def get_teacher_dao(self) -> TeacherDAO: | ||
return SqlTeacherDAO() | ||
|
||
def get_user_dao(self) -> UserDAO: | ||
return SqlUserDAO() |
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,44 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
from db.interface.AdminDAO import AdminDAO | ||
from db.interface.GroupDAO import GroupDAO | ||
from db.interface.ProjectDAO import ProjectDAO | ||
from db.interface.StudentDAO import StudentDAO | ||
from db.interface.SubjectDAO import SubjectDAO | ||
from db.interface.SubmissionDAO import SubmissionDAO | ||
from db.interface.TeacherDAO import TeacherDAO | ||
from db.interface.UserDAO import UserDAO | ||
|
||
|
||
class DAOProvider(ABC): | ||
@abstractmethod | ||
def get_admin_dao(self) -> AdminDAO: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_group_dao(self) -> GroupDAO: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_project_dao(self) -> ProjectDAO: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_student_dao(self) -> StudentDAO: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_subject_dao(self) -> SubjectDAO: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_submission_dao(self) -> SubmissionDAO: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_teacher_dao(self) -> TeacherDAO: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_user_dao(self) -> UserDAO: | ||
raise NotImplementedError |
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,6 @@ | ||
from db.implementation.SqlDAOProvider import SqlDAOProvider | ||
from db.interface.DAOProvider import DAOProvider | ||
|
||
|
||
def get_dao_provider() -> DAOProvider: | ||
return SqlDAOProvider() |
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,13 +1,12 @@ | ||
from fastapi import Depends | ||
|
||
from db.implementation.SqlAdminDAO import SqlAdminDAO | ||
from db.implementation.SqlUserDAO import SqlUserDAO | ||
from domain.models.UserDataclass import UserDataclass | ||
from routes.db import get_dao_provider | ||
|
||
|
||
def get_authenticated_user() -> UserDataclass: | ||
return SqlUserDAO().get(1) # Actually authenticate user | ||
return get_dao_provider().get_user_dao().get(1) # Actually authenticate user | ||
|
||
|
||
def is_user_admin(user: UserDataclass = Depends(get_authenticated_user)) -> bool: | ||
return SqlAdminDAO().is_user_admin(user.id) | ||
def is_user_admin() -> bool: | ||
user = get_authenticated_user() | ||
admin_dao = get_dao_provider().get_admin_dao() | ||
return admin_dao.is_user_admin(user.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 |
---|---|---|
@@ -1,33 +1,35 @@ | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from fastapi import APIRouter, HTTPException | ||
|
||
from db.errors.database_errors import ItemNotFoundError | ||
from db.implementation.SqlUserDAO import SqlUserDAO | ||
from domain.models.APIUser import APIUser, convert_user | ||
from domain.models.UserDataclass import UserDataclass | ||
from routes.db import get_dao_provider | ||
from routes.login import get_authenticated_user, is_user_admin | ||
|
||
users_router = APIRouter() | ||
|
||
|
||
@users_router.get("/user") | ||
def get_current_user(user: UserDataclass = Depends(get_authenticated_user)) -> APIUser: | ||
return convert_user(user) | ||
def get_current_user() -> APIUser: | ||
user = get_authenticated_user() | ||
return convert_user(user, get_dao_provider()) | ||
|
||
|
||
@users_router.get("/users") | ||
def get_users(admin: bool = Depends(is_user_admin)) -> list[APIUser]: | ||
if not admin: | ||
def get_users() -> list[APIUser]: | ||
if not is_user_admin(): | ||
raise HTTPException(status_code=403) | ||
users = SqlUserDAO().get_all() | ||
return [convert_user(user) for user in users] | ||
user_dao = get_dao_provider().get_user_dao() | ||
users = user_dao.get_all() | ||
return [convert_user(user, get_dao_provider()) for user in users] | ||
|
||
|
||
@users_router.get("/users/{uid}") | ||
def get_user(uid: int, admin: bool = Depends(is_user_admin)) -> APIUser: | ||
if not admin: | ||
def get_user(uid: int) -> APIUser: | ||
if not is_user_admin(): | ||
raise HTTPException(status_code=403) | ||
user_dao = get_dao_provider().get_user_dao() | ||
try: | ||
user = SqlUserDAO().get(uid) | ||
user = user_dao.get(uid) | ||
except ItemNotFoundError as err: | ||
raise HTTPException(status_code=404) from err | ||
return convert_user(user) | ||
return convert_user(user, get_dao_provider()) |