Spaces:
Build error
Build error
File size: 3,889 Bytes
b7a7f32 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
from typing import Generator
from typing import Optional
from fastapi import Cookie
from fastapi import Depends, HTTPException, status
from sqlalchemy.orm import Session
import cruds
import models
from core import settings
from core.db import SessionLocal
from core.db import redis_session_client
def get_db() -> Generator:
try:
db = SessionLocal()
yield db
finally:
db.close()
async def get_current_user(
db: Session = Depends(get_db),
session: str = Cookie(None),
) -> models.User:
if not session:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="Error ID: 137"
) # Invalid Session Token!
user_id = await redis_session_client.client.get(session, encoding="utf-8")
if not user_id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="Error ID: 138"
) # Invalid Session Token!
user = cruds.crud_user.get(db, id=user_id)
if not user:
raise HTTPException(status_code=404, detail="Error ID: 139") # User not found
return user
def get_current_active_user(
current_user: models.User = Depends(get_current_user),
) -> models.User:
if not cruds.crud_user.is_active(current_user):
raise HTTPException(status_code=400, detail="Error ID: 140") # Inactive user
return current_user
def get_current_active_ws_users(
current_user: models.User = Depends(get_current_user),
) -> models.User:
if not cruds.crud_user.is_active(current_user):
raise HTTPException(status_code=400, detail="Error ID: 140") # Inactive user
return current_user
async def auth_token(token: Optional[str] = None):
if token:
return {"token": token}
else:
return None
async def get_current_active_ws_user(
db: Session = Depends(get_db),
params: dict = Depends(auth_token),
session: str = Cookie(None),
) -> models.User:
if not (session or params):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="Error ID: 137"
) # Invalid Session Token!
if session:
session_token = session
else:
session_token = params.get("token")
user_id = await redis_session_client.client.get(session_token, encoding="utf-8")
if not user_id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="Error ID: 138"
) # Invalid Session Token!
user = cruds.crud_user.get(db, id=user_id)
if not user:
raise HTTPException(status_code=404, detail="Error ID: 139") # User not found
return user
def get_current_active_teacher(
current_user: models.User = Depends(get_current_active_user),
) -> models.User:
if current_user.user_type == settings.UserType.TEACHER.value:
return current_user
else:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
def get_current_active_teacher_or_above(
current_user: models.User = Depends(get_current_active_user),
) -> models.User:
if current_user.user_type <= settings.UserType.TEACHER.value:
return current_user
else:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
def get_current_active_superuser(
current_user: models.User = Depends(get_current_active_user),
) -> models.User:
if not cruds.crud_user.is_superuser(current_user):
raise HTTPException(
status_code=400, detail="Error ID: 141"
) # The user doesn't have enough privileges
return current_user
def get_current_admin_or_above(
current_user: models.User = Depends(get_current_active_user),
) -> models.User:
if not current_user.user_type <= settings.UserType.ADMIN.value:
raise HTTPException(
status_code=400, detail="Error ID: 142"
) # The user doesn't have enough privileges
return current_user
|