1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00

Testing workable

This commit is contained in:
MoonTestUse1
2025-02-07 00:43:33 +06:00
parent 8543d7fe88
commit 6db95a5eb0
37 changed files with 911 additions and 454 deletions

View File

@@ -3,10 +3,10 @@ from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from passlib.context import CryptContext
from sqlalchemy.orm import Session
import re
from .jwt import verify_token
from ..database import get_db
from ..crud import employees
from ..models.employee import Employee
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@@ -34,20 +34,16 @@ def get_current_admin(
try:
token = credentials.credentials
token_data = verify_token(token, db)
if not token_data:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
payload = verify_token(token, db)
employee_id = int(payload.get("sub"))
# Проверяем, что это админ
employee = employees.get_employee(db, token_data.employee_id)
# Получаем сотрудника из БД
from ..crud.employees import get_employee
employee = get_employee(db, employee_id)
if not employee or not employee.is_admin:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not enough permissions",
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
@@ -55,7 +51,7 @@ def get_current_admin(
except Exception:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
@@ -73,20 +69,16 @@ def get_current_employee(
try:
token = credentials.credentials
token_data = verify_token(token, db)
if not token_data:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
payload = verify_token(token, db)
employee_id = int(payload.get("sub"))
# Проверяем существование сотрудника
employee = employees.get_employee(db, token_data.employee_id)
# Получаем сотрудника из БД
from ..crud.employees import get_employee
employee = get_employee(db, employee_id)
if not employee:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Employee not found",
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
@@ -94,6 +86,6 @@ def get_current_employee(
except Exception:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)