-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(admin-endpoints)!: refactor admin endpoints and add new two …
…get admin depends
- Loading branch information
Showing
3 changed files
with
114 additions
and
83 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,29 @@ | ||
from typing import Optional | ||
from app.models.admin import AdminInDB, AdminValidationResult | ||
from app.db import Session, crud, get_db | ||
from config import SUDOERS | ||
from fastapi import Depends, HTTPException | ||
|
||
|
||
def validate_admin(db: Session, username: str, password: str) -> Optional[AdminValidationResult]: | ||
""" | ||
Validate admin credentials with environment variables or database. | ||
""" | ||
if SUDOERS.get(username) == password: | ||
return AdminValidationResult(username, True) | ||
|
||
dbadmin = crud.get_admin(db, username) | ||
if dbadmin and AdminInDB.from_orm(dbadmin).verify_password(password): | ||
return AdminValidationResult(dbadmin.username, dbadmin.is_sudo) | ||
|
||
return None | ||
|
||
|
||
def get_admin_by_username(username: str, db: Session = Depends(get_db)): | ||
""" | ||
Fetch an admin by username from the database. | ||
""" | ||
dbadmin = crud.get_admin(db, username) | ||
if not dbadmin: | ||
raise HTTPException(status_code=404, detail="Admin not found") | ||
return dbadmin |
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