feat(panel): gestión de múltiples administradores en MySQL y simplificación de UI
This commit is contained in:
@@ -9,8 +9,12 @@ from fastapi import Request
|
||||
from fastapi.responses import RedirectResponse
|
||||
from pydantic import BaseModel
|
||||
|
||||
from passlib.hash import pbkdf2_sha256 as hasher
|
||||
from botdiscord.config import load_config, get_web_config, get_libretranslate_url, get_db_type
|
||||
from botdiscord.database import get_ui_translation, save_ui_translation
|
||||
from botdiscord.database import (
|
||||
get_ui_translation, save_ui_translation,
|
||||
get_admins, get_admin_by_username, add_admin, delete_admin
|
||||
)
|
||||
from botdiscord.translate import translate_text
|
||||
|
||||
app = FastAPI(title="Panel de Configuración - Bots de Traducción")
|
||||
@@ -74,9 +78,21 @@ def get_config():
|
||||
}
|
||||
|
||||
def verify_admin(username: str, password: str) -> bool:
|
||||
# 1. Primero intentamos con el SuperAdmin del .env (backup)
|
||||
web_config = get_web_config()
|
||||
return (username == web_config.get("admin_username", "") and
|
||||
password == web_config.get("admin_password", ""))
|
||||
if username == web_config.get("admin_username", "") and \
|
||||
password == web_config.get("admin_password", ""):
|
||||
return True
|
||||
|
||||
# 2. Si no es el SuperAdmin, buscamos en la base de datos MySQL
|
||||
try:
|
||||
admin = get_admin_by_username(username)
|
||||
if admin and hasher.verify(password, admin['password_hash']):
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error verifying admin: {e}")
|
||||
|
||||
return False
|
||||
|
||||
@app.get("/")
|
||||
async def root(request: Request):
|
||||
@@ -217,6 +233,72 @@ async def logout():
|
||||
response.delete_cookie("auth")
|
||||
return response
|
||||
|
||||
@app.get("/admins")
|
||||
async def admins_page(request: Request):
|
||||
if request.cookies.get("auth") != "ok":
|
||||
return RedirectResponse(url="/login")
|
||||
|
||||
admins = get_admins()
|
||||
return templates.TemplateResponse("admins.html", {
|
||||
"request": request,
|
||||
"admins": admins
|
||||
})
|
||||
|
||||
@app.post("/admins/add")
|
||||
async def add_admin_post(request: Request):
|
||||
if request.cookies.get("auth") != "ok":
|
||||
raise HTTPException(status_code=401)
|
||||
|
||||
form = await request.form()
|
||||
username = form.get("username", "")
|
||||
password = form.get("password", "")
|
||||
|
||||
if not username or not password:
|
||||
return RedirectResponse(url="/admins?error=missing_fields", status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
try:
|
||||
password_hash = hasher.hash(password)
|
||||
add_admin(username, password_hash)
|
||||
return RedirectResponse(url="/admins?success=1", status_code=status.HTTP_303_SEE_OTHER)
|
||||
except Exception as e:
|
||||
print(f"CRITICAL ERROR adding admin: {e}")
|
||||
# Redirigimos con error a la misma página
|
||||
return RedirectResponse(url="/admins?error=" + str(e), status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
@app.post("/admins/delete")
|
||||
async def delete_admin_post(request: Request):
|
||||
if request.cookies.get("auth") != "ok":
|
||||
raise HTTPException(status_code=401)
|
||||
|
||||
form = await request.form()
|
||||
admin_id = form.get("admin_id")
|
||||
if admin_id:
|
||||
try:
|
||||
delete_admin(int(admin_id))
|
||||
except Exception as e:
|
||||
print(f"Error deleting admin: {e}")
|
||||
|
||||
return RedirectResponse(url="/admins", status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
@app.post("/admins/update")
|
||||
async def update_admin_post(request: Request):
|
||||
if request.cookies.get("auth") != "ok":
|
||||
raise HTTPException(status_code=401)
|
||||
|
||||
form = await request.form()
|
||||
admin_id = form.get("admin_id")
|
||||
new_password = form.get("new_password")
|
||||
|
||||
if admin_id and new_password:
|
||||
from botdiscord.database import update_admin_password
|
||||
password_hash = hasher.hash(new_password)
|
||||
try:
|
||||
update_admin_password(int(admin_id), password_hash)
|
||||
except Exception as e:
|
||||
print(f"Error updating admin password: {e}")
|
||||
|
||||
return RedirectResponse(url="/admins?success=1", status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
web_config = get_web_config()
|
||||
|
||||
Reference in New Issue
Block a user