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
12 changed files
with
106 additions
and
42 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,12 +1,12 @@ | ||
import uvicorn | ||
from fastapi import FastAPI | ||
|
||
from routes.teachers import teachers_router | ||
from routes.users import users_router | ||
|
||
app = FastAPI() | ||
|
||
# Koppel routes uit andere modules. | ||
app.include_router(teachers_router) | ||
app.include_router(users_router, prefix="/api") | ||
|
||
if __name__ == "__main__": | ||
uvicorn.run("app:app") |
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
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,27 @@ | ||
from pydantic import BaseModel, EmailStr | ||
|
||
from db.implementation.SqlAdminDAO import SqlAdminDAO | ||
from db.implementation.SqlStudentDAO import SqlStudentDAO | ||
from db.implementation.SqlTeacherDAO import SqlTeacherDAO | ||
from domain.models.UserDataclass import UserDataclass | ||
|
||
|
||
class APIUser(BaseModel): | ||
id: int | ||
name: str | ||
email: EmailStr | ||
roles: list[str] | ||
|
||
|
||
def convert_user(user: UserDataclass) -> APIUser: | ||
result = APIUser(id=user.id, name=user.name, email=user.email, roles=[]) | ||
teacher_dao = SqlTeacherDAO() | ||
admin_dao = SqlAdminDAO() | ||
student_dao = SqlStudentDAO() | ||
if teacher_dao.is_user_teacher(user.id): | ||
result.roles.append("teacher") | ||
if admin_dao.is_user_admin(user.id): | ||
result.roles.append("admin") | ||
if student_dao.is_user_student(user.id): | ||
result.roles.append("student") | ||
return result |
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,13 @@ | ||
from fastapi import Depends | ||
|
||
from db.implementation.SqlAdminDAO import SqlAdminDAO | ||
from db.implementation.SqlUserDAO import SqlUserDAO | ||
from domain.models.UserDataclass import UserDataclass | ||
|
||
|
||
def get_authenticated_user() -> UserDataclass: | ||
return SqlUserDAO().get(1) # Actually authenticate user | ||
|
||
|
||
def is_user_admin(user: UserDataclass = Depends(get_authenticated_user)) -> bool: | ||
return SqlAdminDAO().is_user_admin(user.id) |
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,33 @@ | ||
from fastapi import APIRouter, Depends, 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.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) | ||
|
||
|
||
@users_router.get("/users") | ||
def get_users(admin: bool = Depends(is_user_admin)) -> list[APIUser]: | ||
if not admin: | ||
raise HTTPException(status_code=403) | ||
users = SqlUserDAO().get_all() | ||
return [convert_user(user) for user in users] | ||
|
||
|
||
@users_router.get("/users/{uid}") | ||
def get_user(uid: int, admin: bool = Depends(is_user_admin)) -> APIUser: | ||
if not admin: | ||
raise HTTPException(status_code=403) | ||
try: | ||
user = SqlUserDAO().get(uid) | ||
except ItemNotFoundError as err: | ||
raise HTTPException(status_code=404) from err | ||
return convert_user(user) |